From: Wolfgang M. M. <wol...@us...> - 2004-05-03 13:09:24
|
Update of /cvsroot/exist/eXist-1.0/src/org/exist/util/serializer In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv19067/src/org/exist/util/serializer Modified Files: DOMStreamerObjectFactory.java SAXSerializer.java DOMStreamerPool.java XMLWriter.java Added Files: ExtendedDOMSerializer.java ExtendedDOMStreamer.java Log Message: Implemented lazy evaluation for XQuery enclosed expressions. --- NEW FILE: ExtendedDOMStreamer.java --- /* * eXist Open Source Native XML Database * Copyright (C) 2001-04 Wolfgang M. Meier * wol...@ex... * http://exist-db.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: ExtendedDOMStreamer.java,v 1.1 2004/05/03 13:08:45 wolfgang_m Exp $ */ package org.exist.util.serializer; import org.exist.memtree.NodeImpl; import org.exist.memtree.ReferenceNode; import org.exist.storage.serializers.Serializer; import org.w3c.dom.Node; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; import org.xml.sax.ext.LexicalHandler; /** * @author wolf */ public class ExtendedDOMStreamer extends DOMStreamer { private Serializer xmlSerializer; public ExtendedDOMStreamer() { super(); } /** * */ public ExtendedDOMStreamer(Serializer xmlSerializer) { super(); this.xmlSerializer = xmlSerializer; } /** * @param contentHandler * @param lexicalHandler */ public ExtendedDOMStreamer(Serializer xmlSerializer, ContentHandler contentHandler, LexicalHandler lexicalHandler) { super(contentHandler, lexicalHandler); this.xmlSerializer = xmlSerializer; } public void setSerializer(Serializer serializer) { this.xmlSerializer = serializer; } /* (non-Javadoc) * @see org.exist.util.serializer.DOMStreamer#startNode(org.w3c.dom.Node) */ protected void startNode(Node node) throws SAXException { if(node.getNodeType() == NodeImpl.REFERENCE_NODE) { if(xmlSerializer == null) throw new SAXException("Cannot serialize node reference. Serializer is undefined."); xmlSerializer.toSAX(((ReferenceNode)node).getReference()); } else super.startNode(node); } /* (non-Javadoc) * @see org.exist.util.serializer.DOMStreamer#reset() */ public void reset() { super.reset(); xmlSerializer = null; } } Index: DOMStreamerObjectFactory.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/util/serializer/DOMStreamerObjectFactory.java,v retrieving revision 1.2 retrieving revision 1.3 diff -C2 -d -r1.2 -r1.3 *** DOMStreamerObjectFactory.java 22 Dec 2003 18:48:45 -0000 1.2 --- DOMStreamerObjectFactory.java 3 May 2004 13:08:44 -0000 1.3 *************** *** 38,42 **** */ public Object makeObject() throws Exception { ! return new DOMStreamer(); } --- 38,42 ---- */ public Object makeObject() throws Exception { ! return new ExtendedDOMStreamer(); } Index: XMLWriter.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/util/serializer/XMLWriter.java,v retrieving revision 1.7 retrieving revision 1.8 diff -C2 -d -r1.7 -r1.8 *** XMLWriter.java 27 Apr 2004 15:47:00 -0000 1.7 --- XMLWriter.java 3 May 2004 13:08:45 -0000 1.8 *************** *** 22,25 **** --- 22,26 ---- import java.io.IOException; import java.io.Writer; + import java.util.Arrays; import java.util.Properties; import java.util.Stack; *************** *** 55,58 **** --- 56,81 ---- private char[] charref = new char[10]; + private static boolean[] textSpecialChars; + private static boolean[] attrSpecialChars; + + static { + textSpecialChars = new boolean[127]; + Arrays.fill(textSpecialChars, false); + textSpecialChars['<'] = true; + textSpecialChars['>'] = true; + textSpecialChars['\r'] = true; + textSpecialChars['&'] = true; + + attrSpecialChars = new boolean[127]; + Arrays.fill(attrSpecialChars, false); + attrSpecialChars['<'] = true; + attrSpecialChars['>'] = true; + attrSpecialChars['\r'] = true; + attrSpecialChars['\n'] = true; + attrSpecialChars['\t'] = true; + attrSpecialChars['&'] = true; + attrSpecialChars['"'] = true; + } + public XMLWriter() { } *************** *** 147,151 **** } writer.write("=\""); ! writeChars(nsURI); writer.write('"'); } catch (IOException e) { --- 170,174 ---- } writer.write("=\""); ! writeChars(nsURI, true); writer.write('"'); } catch (IOException e) { *************** *** 162,166 **** writer.write(qname); writer.write("=\""); ! writeAttrChars(value); writer.write('"'); } catch (IOException e) { --- 185,189 ---- writer.write(qname); writer.write("=\""); ! writeChars(value, true); writer.write('"'); } catch (IOException e) { *************** *** 175,179 **** if (tagIsOpen) closeStartTag(false); ! writeChars(chars); } catch (IOException e) { throw new TransformerException(e.getMessage(), e); --- 198,202 ---- if (tagIsOpen) closeStartTag(false); ! writeChars(chars, false); } catch (IOException e) { throw new TransformerException(e.getMessage(), e); *************** *** 201,205 **** if (data != null && data.length() > 0) { writer.write(' '); ! writeChars(data); } writer.write("?>"); --- 224,228 ---- if (data != null && data.length() > 0) { writer.write(' '); ! writeChars(data, false); } writer.write("?>"); *************** *** 216,220 **** closeStartTag(false); writer.write("<!--"); ! writeChars(data); writer.write("-->"); } catch (IOException e) { --- 239,243 ---- closeStartTag(false); writer.write("<!--"); ! writeChars(data, false); writer.write("-->"); } catch (IOException e) { *************** *** 269,304 **** } ! private final void writeChars(CharSequence s) throws IOException { ! char ch; ! for (int i = 0; i < s.length(); i++) { ! ch = s.charAt(i); ! switch (ch) { ! case '<' : ! writer.write("<"); ! break; ! case '>' : ! writer.write(">"); ! break; ! case '&' : ! writer.write("&"); ! break; ! case '\r' : ! writer.write("
"); ! break; ! default : ! if (charSet.inCharacterSet(ch)) ! writer.write(ch); ! else ! writeCharacterReference(ch); ! break; ! } ! } ! } ! ! protected void writeAttrChars(CharSequence s) throws IOException { ! char ch; ! for (int i = 0; i < s.length(); i++) { ! ch = s.charAt(i); ! switch (ch) { case '<' : writer.write("<"); --- 292,318 ---- } ! private final void writeChars(CharSequence s, boolean inAttribute) throws IOException { ! boolean[] specialChars = inAttribute ? attrSpecialChars : textSpecialChars; ! char ch = 0; ! final int len = s.length(); ! int pos = 0, i; ! while(pos < len) { ! i = pos; ! while(i < len) { ! ch = s.charAt(i); ! if(ch < 128) { ! if(specialChars[ch]) ! break; ! else ! i++; ! } else if(!charSet.inCharacterSet(ch)) ! break; ! else ! i++; ! } ! writer.write(s.subSequence(pos, i).toString()); ! if(i >= len) ! return; ! switch (ch) { case '<' : writer.write("<"); *************** *** 322,331 **** writer.write("""); break; ! default : ! if(charSet.inCharacterSet(ch)) ! writer.write(ch); ! else ! writeCharacterReference(ch); ! } } } --- 336,343 ---- writer.write("""); break; ! default: ! writeCharacterReference(ch); ! } ! pos = ++i; } } *************** *** 344,346 **** writer.write(charref, 0, o); } ! } \ No newline at end of file --- 356,358 ---- writer.write(charref, 0, o); } ! } --- NEW FILE: ExtendedDOMSerializer.java --- /* * eXist Open Source Native XML Database * Copyright (C) 2001-04 Wolfgang M. Meier * wol...@ex... * http://exist-db.org * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: ExtendedDOMSerializer.java,v 1.1 2004/05/03 13:08:45 wolfgang_m Exp $ */ package org.exist.util.serializer; import java.io.Writer; import java.util.Properties; import javax.xml.transform.TransformerException; import org.exist.dom.NodeProxy; import org.exist.memtree.NodeImpl; import org.exist.memtree.ReferenceNode; import org.exist.storage.DBBroker; import org.exist.storage.serializers.Serializer; import org.w3c.dom.Node; import org.xml.sax.SAXException; import org.xml.sax.SAXNotRecognizedException; import org.xml.sax.SAXNotSupportedException; /** * @author wolf */ public class ExtendedDOMSerializer extends DOMSerializer { private DBBroker broker; /** * */ public ExtendedDOMSerializer(DBBroker broker) { super(); this.broker = broker; } /** * @param writer * @param outputProperties */ public ExtendedDOMSerializer(DBBroker broker, Writer writer, Properties outputProperties) { super(writer, outputProperties); this.broker = broker; } /* (non-Javadoc) * @see org.exist.util.serializer.DOMSerializer#startNode(org.w3c.dom.Node) */ protected void startNode(Node node) throws TransformerException { if(node.getNodeType() == NodeImpl.REFERENCE_NODE) { SAXSerializer handler = SAXSerializerPool.getInstance().borrowSAXSerializer(); handler.setReceiver(receiver); Serializer serializer = broker.getSerializer(); serializer.setContentHandler(handler); try { serializer.setProperties(outputProperties); } catch (SAXNotRecognizedException e) { } catch (SAXNotSupportedException e) { } try { serializer.toSAX((NodeProxy)((ReferenceNode)node).getReference()); } catch (SAXException e) { throw new TransformerException(e.getMessage(), e); } } else super.startNode(node); } } Index: SAXSerializer.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/util/serializer/SAXSerializer.java,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** SAXSerializer.java 23 Apr 2004 13:07:46 -0000 1.6 --- SAXSerializer.java 3 May 2004 13:08:45 -0000 1.7 *************** *** 81,84 **** --- 81,88 ---- } + public void setReceiver(XMLWriter receiver) { + this.receiver = receiver; + } + public void reset() { nsSupport.reset(); Index: DOMStreamerPool.java =================================================================== RCS file: /cvsroot/exist/eXist-1.0/src/org/exist/util/serializer/DOMStreamerPool.java,v retrieving revision 1.4 retrieving revision 1.5 diff -C2 -d -r1.4 -r1.5 *** DOMStreamerPool.java 25 Feb 2004 15:31:58 -0000 1.4 --- DOMStreamerPool.java 3 May 2004 13:08:45 -0000 1.5 *************** *** 25,28 **** --- 25,29 ---- import org.apache.commons.pool.PoolableObjectFactory; import org.apache.commons.pool.impl.StackObjectPool; + import org.exist.storage.serializers.Serializer; /** *************** *** 50,53 **** --- 51,64 ---- } + public DOMStreamer borrowDOMStreamer(Serializer delegate) { + try { + ExtendedDOMStreamer serializer = (ExtendedDOMStreamer)borrowObject(); + serializer.setSerializer(delegate); + return serializer; + } catch (Exception e) { + throw new RuntimeException(e); + } + } + public void returnDOMStreamer(DOMStreamer streamer) { if(streamer == null) |