@Override public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { super.startElement(uri, localName, qName, atts); if (TITLE_TAG.equalsIgnoreCase(localName) && XHTMLContentHandler.XHTML.equals(uri)) { isTitleTagOpen = true; } }
@Override public void endElement(String uri, String localName, String name) throws SAXException { // TODO: enable this, but some parsers currently // trip it //assert verifyEndElement(name); super.endElement(uri, localName, name); }
@Override public void characters(char[] ch, int start, int length) throws SAXException { if (isTitleTagOpen && length == 0) { // Hack to close the title tag try { super.characters(new char[0], 0, 1); } catch (ArrayIndexOutOfBoundsException e) { // Expected, just wanted to close the title tag } } else { super.characters(ch, start, length); } }
@Override public void endElement(String uri, String localName, String qName) throws SAXException { // Swallow the event if closing an embedded image tag. if (isEmbeddedImgTagOpen && IMG_TAG.equalsIgnoreCase(localName) && XHTML.equals(uri)) { return; } // Error state. Output this event and the previous one. if (isEmbeddedImgTagOpen) { super.startElement(uri, IMG_TAG, IMG_TAG, imgAttributes); isEmbeddedImgTagOpen = false; imgAttributes = null; } if (isEmbeddedAnchorTagOpen && ANCHOR_TAG.equalsIgnoreCase(localName) && XHTML.equals(uri)) { isEmbeddedAnchorTagOpen = false; } else { super.endElement(uri, localName, qName); } } }
@Override public void characters(char[] ch, int start, int length) throws SAXException { try { handler.characters(ch, start, length); } catch (SAXException e) { handleException(e); } }
@Override public void endDocument() throws SAXException { // TODO: enable this, but some parsers currently // trip it //assert verifyEndDocument(); super.endDocument(); }
@Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { if (writeLimit == -1 || writeCount + length <= writeLimit) { super.ignorableWhitespace(ch, start, length); writeCount += length; } else { super.ignorableWhitespace(ch, start, writeLimit - writeCount); writeCount = writeLimit; throw new WriteLimitReachedException( "Your document contained more than " + writeLimit + " characters, and so your requested limit has been" + " reached. To receive the full text of the document," + " increase your limit. (Text up to the limit is" + " however available).", tag); } }
/** * do not load any DTDs (may be requested by parser). Fake the DTD by * returning a empty string as InputSource */ @Override public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException { if ((systemId != null && systemId.toLowerCase(Locale.ROOT).endsWith(".dtd")) || DTD_PUBLIC_ID.equals(publicId)) { return new InputSource(new StringReader("")); } else { return super.resolveEntity(publicId, systemId); } }
@Override public void endPrefixMapping(String prefix) throws SAXException { try { handler.endPrefixMapping(prefix); } catch (SAXException e) { handleException(e); } }
public void reallyEndDocument() throws SAXException { super.endDocument(); }
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { if (matcher.matchesText()) { super.ignorableWhitespace(ch, start, length); } }
/** * do not load any DTDs (may be requested by parser). Fake the DTD by * returning a empty string as InputSource */ @Override public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException { if ((systemId != null && systemId.toLowerCase(Locale.ROOT).endsWith(".dtd")) || DTD_PUBLIC_ID.equals(publicId)) { return new InputSource(new StringReader("")); } else { return super.resolveEntity(publicId, systemId); } }
@Override public void startElement( String namespaceURI, String localName, String qName, Attributes atts) throws SAXException { AttributesImpl natts = new AttributesImpl(); for (int i = 0; i < atts.getLength(); i++) { natts.addAttribute( mapOldNS(atts.getURI(i)), atts.getLocalName(i), atts.getQName(i), atts.getType(i), atts.getValue(i)); } super.startElement(mapOldNS(namespaceURI), localName, qName, atts); }
@Override public void endElement(String uri, String localName, String name) throws SAXException { String upper = localName.toUpperCase(Locale.ENGLISH); super.endElement(XMLConstants.NULL_NS_URI, upper, upper); }
/** * Writes the given characters to the given character stream. */ @Override public void characters(char[] ch, int start, int length) throws SAXException { if (writeLimit == -1 || writeCount + length <= writeLimit) { super.characters(ch, start, length); writeCount += length; } else { super.characters(ch, start, writeLimit - writeCount); writeCount = writeLimit; throw new WriteLimitReachedException( "Your document contained more than " + writeLimit + " characters, and so your requested limit has been" + " reached. To receive the full text of the document," + " increase your limit. (Text up to the limit is" + " however available).", tag); } }
@Override public void startElement( String uri, String localName, String name, Attributes atts) throws SAXException { try { handler.startElement(uri, localName, name, atts); } catch (SAXException e) { handleException(e); } }
/** * This method is called whenever the Parser is done parsing the file. So, * we check the output for any phone numbers. */ @Override public void endDocument() throws SAXException { super.endDocument(); List<String> numbers = CleanPhoneText.extractPhoneNumbers(stringBuilder.toString()); for (String number : numbers) { metadata.add(PHONE_NUMBERS, number); } } }
@Override public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { advance(length); super.ignorableWhitespace(ch, start, length); }
/** * do not load any DTDs (may be requested by parser). Fake the DTD by * returning a empty string as InputSource */ @Override public InputSource resolveEntity(String publicId, String systemId) throws IOException, SAXException { if ((systemId != null && systemId.toLowerCase(Locale.ROOT).endsWith(".dtd")) || DTD_PUBLIC_ID.equals(publicId)) { return new InputSource(new StringReader("")); } else { return super.resolveEntity(publicId, systemId); } }
@Override public void startElement( String uri, String localName, String name, Attributes atts) throws SAXException { String upper = localName.toUpperCase(Locale.ENGLISH); AttributesImpl attributes = new AttributesImpl(); for (int i = 0; i < atts.getLength(); i++) { String auri = atts.getURI(i); String local = atts.getLocalName(i); String qname = atts.getQName(i); if (XMLConstants.NULL_NS_URI.equals(auri) && !local.equals(XMLConstants.XMLNS_ATTRIBUTE) && !qname.startsWith(XMLConstants.XMLNS_ATTRIBUTE + ":")) { attributes.addAttribute( auri, local, qname, atts.getType(i), atts.getValue(i)); } } super.startElement(XMLConstants.NULL_NS_URI, upper, upper, attributes); }