@Override public void startElement(String uri, String localName, String qName, Attributes origAttrs) throws SAXException { // If we have an image tag, re-write the src attribute // if required if ("img".equals(localName)) { AttributesImpl attrs; if (origAttrs instanceof AttributesImpl) { attrs = (AttributesImpl) origAttrs; } else { attrs = new AttributesImpl(origAttrs); } for (int i = 0; i < attrs.getLength(); i++) { if ("src".equals(attrs.getLocalName(i))) { String src = attrs.getValue(i); if (src.startsWith("embedded:")) { String newSrc = ""; if (imageFolder != null) newSrc += imageFolder + "/"; if (imagePrefix != null) newSrc += imagePrefix; newSrc += src.substring(src.indexOf(':') + 1); attrs.setValue(i, newSrc); } } } super.startElement(uri, localName, qName, attrs); } else { // For any other tag, pass through as-is super.startElement(uri, localName, qName, origAttrs); } }
@Override public void startElement(String uri, String localName, String name, Attributes atts) throws SAXException { super.startElement(uri, localName, name, atts); if (delta < -1) { super.characters(LINE_SEPARATOR, 0, LINE_SEPARATOR.length); for (int i = 0; i < level; ++i) { super.characters(new char[] {'\t'}, 0, 1); } delta = 0; } delta += 1; level += 1; indent = true; }
@Override public void startDocument() throws SAXException { super.startDocument(); level = 0; delta = 0; indent = false; }
@Override public void endElement(String uri, String localName, String name) throws SAXException { super.endElement(uri, localName, name); if (delta > 0) { delta = 0; } delta -= 1; level -= 1; indent = true; }
/** * Print characters if they have other content than spaces, newlines, or tabs and put them on a * new, indented line if they are from another element than the characters printed before. */ @Override public void characters(char[] ch, int start, int length) throws SAXException { boolean content = false; for (int i = start + length - 1; i >= start; --i) { if (ch[i] != ' ' && ch[i] != '\n' && ch[i] != '\t' && ch[i] != '\r') { content = true; break; } } if (content) { if (indent) { super.characters(LINE_SEPARATOR, 0, LINE_SEPARATOR.length); for (int i = 0; i < level; ++i) { super.characters(new char[] {'\t'}, 0, 1); } indent = false; } super.characters(ch, start, length); } }
@Override public void endDocument() throws SAXException { super.endDocument(); super.characters(LINE_SEPARATOR, 0, LINE_SEPARATOR.length); }