vgritsenko 2003/12/25 11:34:32
Modified: java/src/org/apache/xindice/xml/sax SAXEventGenerator.java SetContentHandler.java Log: minor refactorings Revision Changes Path 1.27 +28 -41 xml-xindice/java/src/org/apache/xindice/xml/sax/SAXEventGenerator.java Index: SAXEventGenerator.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/xml/sax/SAXEventGenerator.java,v retrieving revision 1.26 retrieving revision 1.27 diff -u -r1.26 -r1.27 --- SAXEventGenerator.java 9 Aug 2003 21:19:53 -0000 1.26 +++ SAXEventGenerator.java 25 Dec 2003 19:34:32 -0000 1.27 @@ -94,6 +94,8 @@ private static final Log log = LogFactory.getLog(SAXEventGenerator.class); + private static final int XMLNS_MAP_INCREMENT = 5; + /** * This is a SAX feature that controls how namespaces are reported in SAX. * In accordance with the SAX2 specification by default this feature is <em>on</em>. @@ -126,30 +128,25 @@ * * For SAX2 the default is off. */ - private boolean hasSaxNamespacesPrefixes = false; - + private boolean hasSaxNamespacesPrefixes; - private SymbolTable symbols = null; - private byte[] data = null; - private int pos = 0; - private int len = 0; + private SymbolTable symbols; + private byte[] data; + private int pos; + private int len; private Map properties = new HashMap(); - private ContentHandler content = null; - private CompressionHandler comp = null; - private ErrorHandler errors = null; - private EntityResolver entities = null; - private DTDHandler dtd = null; - - private boolean interrupt = false; + private ContentHandler content; + private CompressionHandler comp; + private ErrorHandler errors; + private EntityResolver entities; + private DTDHandler dtd; - static final int XMLNS_MAP_INCREMENT = 5; + private boolean interrupt; public SAXEventGenerator(SymbolTable symbols, byte[] data) { - this.symbols = symbols; - this.data = data; - this.len = data.length; + this(symbols, data, 0, data.length); } public SAXEventGenerator(SymbolTable symbols, byte[] data, int pos, int len) { @@ -162,8 +159,7 @@ public SAXEventGenerator(Document doc) { try { if (doc instanceof CompressedDocument) { - doc = new DocumentImpl(doc); - CompressedDocument cDoc = (CompressedDocument) doc; + CompressedDocument cDoc = new DocumentImpl(doc); symbols = cDoc.getSymbols(); data = cDoc.getDataBytes(); pos = cDoc.getDataPos(); @@ -171,6 +167,7 @@ } else { symbols = new SymbolTable(); data = DOMCompressor.Compress(doc, symbols); + pos = 0; len = data.length; } } catch (Exception e) { @@ -283,7 +280,6 @@ } private final boolean isNSAttr(final String qName) { - return (("xmlns".equals(qName)) || qName.startsWith("xmlns:")); } @@ -329,37 +325,29 @@ // check the buffer's capacity if (nsMapCount >= mappedPrefixes.length) { - String[] newBuf = new String[mappedPrefixes.length + XMLNS_MAP_INCREMENT]; System.arraycopy(mappedPrefixes, 0, newBuf, 0, mappedPrefixes.length); mappedPrefixes = newBuf; } - /* The prefix is the attr "local name", unless this is the - * default namespace, in which case the prefix is the empty - * string - */ + // The prefix is the attr "local name", unless this is the + // default namespace, in which case the prefix is the empty + // string String prefix = ("xmlns".equals(attrName) ? "" : lclName); - /* - * Prefix mappings MAY always be reported, regardless of - * feature settings. - */ + // Prefix mappings MAY always be reported, regardless of + // feature settings. content.startPrefixMapping(prefix, attrValue); mappedPrefixes[nsMapCount++] = prefix; if (hasSaxNamespacesPrefixes) { - - /* - * According to SAX, the local name should be EMPTY - */ + // According to SAX, the local name should be EMPTY attrs.addAttribute("", "", attrName, "CDATA", attrValue); } } else { - - /* Regular attribute */ - attrs.addAttribute(attrURI != null ? - attrURI : "", lclName, attrName, "", attrValue); + // Regular attribute + attrs.addAttribute(attrURI != null ? attrURI : "", + lclName, attrName, "", attrValue); } } @@ -374,14 +362,13 @@ while (!interrupt && bis.available() > 0) { pos = bis.getPos(); - /* TODO why is it used for? byte signature = */ in.readSignature(); + in.readSignature(); // Read signature len = in.readContentSize(); if (len == 0) { len = 1; } int type = in.getNodeType(); - switch (type) { case Node.ELEMENT_NODE: 1.5 +35 -45 xml-xindice/java/src/org/apache/xindice/xml/sax/SetContentHandler.java Index: SetContentHandler.java =================================================================== RCS file: /home/cvs/xml-xindice/java/src/org/apache/xindice/xml/sax/SetContentHandler.java,v retrieving revision 1.4 retrieving revision 1.5 diff -u -r1.4 -r1.5 --- SetContentHandler.java 7 Aug 2003 20:13:26 -0000 1.4 +++ SetContentHandler.java 25 Dec 2003 19:34:32 -0000 1.5 @@ -65,29 +65,41 @@ package org.apache.xindice.xml.sax; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; import org.xmldb.api.modules.XMLResource; -import java.util.Enumeration; -import java.util.Hashtable; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; /** * Simple ContentHandler that just converts the SAX event stream into a text * representation of the document and stores it in the associated resource. * + * <small> + * The only place this class currently used is in + * [EMAIL PROTECTED] org.apache.xindice.client.xmldb.resources.XMLResourceImpl#setContentAsSAX}. + * Instead of this class, null-transform using TrAX API can be used. + * </small> + * * @version CVS $Revision$, $Date$ */ public class SetContentHandler extends DefaultHandler { + private static final Log log = LogFactory.getLog(SetContentHandler.class); + protected XMLResource resource = null; protected StringBuffer newContent = null; - protected Hashtable namespaces = null; + protected Map namespaces = null; public SetContentHandler(XMLResource resource) { this.resource = resource; - namespaces = new Hashtable(); + namespaces = new HashMap(); } /** @@ -99,7 +111,6 @@ public void startDocument() throws SAXException { newContent = new StringBuffer(); - // TODO: what is the proper way to set this? newContent.append("<?xml version=\"1.0\"?>"); } @@ -115,7 +126,8 @@ try { resource.setContent(newContent.toString()); } catch (Exception e) { - e.printStackTrace(); + log.warn("Exception in endDocument", e); + throw new SAXException(e); } } @@ -146,57 +158,47 @@ namespaces.remove(prefix); } - private String getLocalName(String qn) { + private String getLocalName(String qn) { if (qn.indexOf(':') != -1) { - return qn.substring(qn.indexOf(':') + 1); } else { - return qn; } } - private String getQNameAtt(String uri, String localName) throws SAXException { String prefix = null; if ("".equals(uri)) { - return localName; } /* Look for prefix */ - Enumeration prefixes = namespaces.keys(); - while (prefixes.hasMoreElements()) { - - String key = (String) prefixes.nextElement(); + Iterator prefixes = namespaces.keySet().iterator(); + while (prefixes.hasNext()) { + String key = (String) prefixes.next(); if ((!("".equals(key))) && namespaces.get(key).equals(uri)) { - prefix = key; break; } } if (prefix == null) { - - throw new SAXException("No declared prefix for namespace '" - + uri + "'."); + throw new SAXException("No declared prefix for namespace '" + + uri + "'."); } - return (prefix + ":" + localName); + return prefix + ":" + localName; } private String getQNameElement(String uri, String localName) throws SAXException { - String prefix = null; if ("".equals(uri)) { - if (namespaces.get("") != null) { - throw new SAXException("default namespace is declared here!"); } else { @@ -205,19 +207,16 @@ } /* Look for prefix */ - Enumeration prefixes = namespaces.keys(); - while (prefixes.hasMoreElements()) { - - String key = (String) prefixes.nextElement(); + Iterator prefixes = namespaces.keySet().iterator(); + while (prefixes.hasNext()) { + String key = (String) prefixes.next(); if (namespaces.get(key).equals(uri)) { - prefix = key; break; } } if (prefix == null) { - throw new SAXException("No declared prefix for namespace '" + uri + "'."); } @@ -244,20 +243,16 @@ /* Make up a correct qName if necessary */ if ("".equals(qName)) { - newContent.append(getQNameElement(uri, localName)); } else { - newContent.append(qName); } - for (int i = 0; i < attributes.getLength(); i++) { String qn = attributes.getQName(i); - /* Make up a correct qName if necessary */ + // Make up a correct qName if necessary if ("".equals(qn)) { - qn = getQNameAtt(attributes.getURI(i), attributes.getLocalName(i)); } newContent.append(" "); @@ -267,22 +262,20 @@ newContent.append(attributes.getValue(i)); newContent.append("\""); - //Avoid duplicate namespace declarations + // Avoid duplicate namespace declarations if (qn.equals("xmlns")) { - namespaces.remove(""); } if (qn.startsWith("xmlns:")) { - String ln = getLocalName(qn); namespaces.remove(ln); } } - Enumeration enum = namespaces.keys(); - while (enum.hasMoreElements()) { - String key = (String) enum.nextElement(); + Iterator enum = namespaces.keySet().iterator(); + while (enum.hasNext()) { + String key = (String) enum.next(); newContent.append(" xmlns"); if (key.length() > 0) { newContent.append(":"); @@ -313,7 +306,6 @@ newContent.append("</"); if ("".equals(qName)) { - qName = getQNameElement(uri, localName); } newContent.append(qName); @@ -421,6 +413,4 @@ throws SAXException { // no op } - } -