+ this.parserListener = parserListener;
+ this.inputStream = inputStream;
+ InputStreamReader inputStreamReader = null;
+ BufferedReader htmlReader = null;
+ try {
+ inputStreamReader = new InputStreamReader(inputStream, charset);
+ htmlReader = new BufferedReader(inputStreamReader);
+ new ParserDelegator().parse(htmlReader, this, true);
+ } finally {
+ Closer.close(htmlReader);
+ Closer.close(inputStreamReader);
+ }
+ }
+
+ //
+ // METHODS FROM ParserCallback
+ //
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void handleStartTag(Tag tag, MutableAttributeSet attributeSet, int position) {
+ if (tag == Tag.TITLE) {
+ if (!attributeSet.containsAttribute(ParserCallback.IMPLIED, Boolean.TRUE)) {
+ inTitle = true;
+ }
+ } else if (tag == Tag.A) {
+ String href = (String) attributeSet.getAttribute(HTML.Attribute.HREF);
+ if (href != null) {
+ linkTarget = href;
+ inLink = true;
+ linkText = new StringBuilder();
+ linkTitle = null;
+ String title = (String) attributeSet.getAttribute(HTML.Attribute.TITLE);
+ if (title != null) {
+ linkTitle = title;
+ }
+ }
+ }
+ if (inLink) {
+ if (tag.breaksFlow()) {
+ linkText.append(' ');
+ }
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void handleText(char[] data, int pos) {
+ if (inTitle) {
+ titleText = new String(data);
+ } else if (inLink) {
+ linkText.append(data);
+ }
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void handleEndTag(Tag tag, int position) {
+ if (tag == Tag.TITLE) {
+ inTitle = false;
+ parserListener.parsedTitle(inputStream, titleText);
+ } else if (tag == Tag.A) {
+ inLink = false;
+ parserListener.parsedLink(inputStream, linkTarget, linkTitle, linkText.toString());
+ }