package org.biomediator.xml; import java.io.*; import java.util.*; import java.io.PrintWriter; import javax.xml.parsers.SAXParserFactory; import javax.xml.parsers.SAXParser; import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.*; public class XMLAttributeParser extends DefaultHandler implements XMLParserHandler { public Stack locStack = new Stack(); public String content = null; public HashMap attributes = null; public InputStream inputStream = null; public boolean unprocessedContent = false; public XMLParserHandler parserHandler = null; public StringBuffer strBuf = null; public XMLAttributeParser(InputStream input, StringBuffer lineBuf) { inputStream = input; strBuf = lineBuf; parserHandler = this; } public StringBuffer parseData() { DefaultHandler handler = this; SAXParserFactory factory = SAXParserFactory.newInstance(); try { factory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); SAXParser saxParser = factory.newSAXParser(); saxParser.parse(inputStream, handler); inputStream.close(); } catch (Throwable t) { System.err.println(t); t.printStackTrace(); } return strBuf; } public void processCharacters(String content) { } public void processEnd(String path) { } public void processStart(String path) { } public void startElement(String namespaceURI, String lName, String qName, Attributes attrs) throws SAXException { boolean isElement = false; // Enter new scope. locStack.push(qName); // Read any attributes that are part of the new scope's open tag. int attrsLength = attrs.getLength(); if (attrsLength == 0) { isElement = true; } attributes = new HashMap(); strBuf.append("<" + qName + ">" + "\n"); if (!isElement){ strBuf.append("" + "\n"); } for (int i = 0; i < attrsLength; i++) { attributes.put(attrs.getQName(i), attrs.getValue(i)); strBuf.append("<"); strBuf.append(attrs.getQName(i)); strBuf.append(">" + "\n"); strBuf.append(""); strBuf.append("\n"); strBuf.append("" + "\n"); } if (!isElement){ strBuf.append("" + "\n"); } // Process any START key rules intended for this new scope. processStart(getLocation()); } public void characters(char buf[], int offset, int len) throws SAXException { // Read character data from the CDATA portion of an XML tag. // Note that this method may be called more than once per CDATA tag! if (content == null) { content = (new String(buf, offset, len)).trim(); unprocessedContent = true; } else { content = content + (new String(buf, offset, len)); } } public void endElement(String namespaceURI, String sName, String qName) throws SAXException { // Make sure to process any leftover character data before existing current scope. if (unprocessedContent) { content = content.trim(); //strBuf.append("length is " + content.length() + "\n"); if (content.length()>0){ strBuf.append("\n"); } } // Process any END key rules intended for the current scope. processEnd(getLocation()); // Clear the character buffer. unprocessedContent = false; content = null; strBuf.append("" + "\n"); // Exit current scope. locStack.pop(); } public String getLocation() { StringBuffer locBuf = new StringBuffer(); for (int i = 0; i < locStack.size(); i++) locBuf.append("/" + (String)locStack.elementAt(i)); return(locBuf.toString()); } }