1 9 package org.ozoneDB.xml.util; 10 11 import java.io.IOException ; 12 13 14 15 import org.w3c.dom.Document ; 16 17 import org.w3c.dom.Node ; 18 19 import org.w3c.dom.NodeList ; 20 21 22 23 import org.xml.sax.SAXException ; 24 25 26 27 import org.ozoneDB.OzoneObject; 28 29 import org.ozoneDB.xml.dom.DocumentProxy; 30 31 32 33 import org.infozone.tools.xml.queries.XObject; 34 35 import org.infozone.tools.xml.queries.XPathQuery; 36 37 import org.infozone.tools.xml.queries.XUpdateQuery; 38 39 import org.infozone.tools.xml.queries.XPathQueryFactory; 40 41 import org.infozone.tools.xml.queries.XUpdateQueryFactory; 42 43 44 51 public final class XMLContainerHelperImpl 52 extends OzoneObject 53 implements XMLContainerHelper { 54 55 private static final long serialVersionUID = 4L; 56 57 private static final boolean debug = XMLContainer.debug; 58 59 private static XPathQueryFactory xpathQueryFactory; 60 61 private static XUpdateQueryFactory xupdateQueryFactory; 62 63 64 private Document document; 65 66 static { 67 if (System.getProperties().get( "org.infozone.tools.xml.queries.XPathQueryFactory" ) == null) { 69 System.getProperties().put( "org.infozone.tools.xml.queries.XPathQueryFactory", 70 "org.infozone.tools.xml.queries.xt.XPathQueryFactoryImpl"); 71 } 72 if (System.getProperties().get( "org.infozone.tools.xml.queries.XUpdateQueryFactory" ) == null ) { 73 System.getProperties().put( "org.infozone.tools.xml.queries.XUpdateQueryFactory", 74 "org.infozone.lexus.XUpdateQueryFactoryImpl"); 75 } 76 77 xpathQueryFactory = XPathQueryFactory.newInstance(); 78 xupdateQueryFactory = XUpdateQueryFactory.newInstance(); 79 } 80 81 public XMLContainerHelperImpl() { 82 if (debug) { 83 System.out.println( "helper: ctor..." ); 84 } 85 } 86 87 public final void onCreate() { 88 if (debug) { 89 System.out.println( "helper: onCreate()..." ); 90 } 91 document = (Document )database().createObject( org.ozoneDB.xml.dom.DocumentImpl.class.getName() ); 92 ((DocumentProxy)document).setContainer( this ); 93 } 94 95 public final void onDelete() { 96 if (debug) { 97 System.out.println( "helper: onDelete()..." ); 98 } 99 clearDocument(); 100 } 101 102 public void clearDocument() { 103 108 if (document != null) { 110 database().deleteObject( (DocumentProxy)document ); 111 } 112 document = (Document )database().createObject( org.ozoneDB.xml.dom.DocumentImpl.class.getName() ); 113 } 114 115 public final void setDocument( Document _pdoc ) { 116 if (debug) { 117 System.out.println( "helper: setDocument..." ); 118 } 119 120 if (document != null) { 121 throw new IllegalStateException ( "Container helper is already assigned to a document." ); 122 } 123 124 document = _pdoc; 125 ((DocumentProxy)document).setContainer( this ); 126 } 127 128 public final Document getDocument() { 129 if (debug) { 130 System.out.println( "helper: getDocument..." ); 131 } 132 return document; 133 } 134 135 public final SAXChunkConsumer beginInputSequence( Node _pNode ) throws Exception { 136 if (debug) { 137 System.out.println("helper: beginInputSequence..."); 138 } 139 if (_pNode == null) { 140 clearDocument(); 141 } 142 SAXChunkConsumer consumer = new SAXChunkConsumer( getDocument(), _pNode ); 143 return consumer; 144 } 145 146 public final SAXChunkConsumer putChunk( byte[] _chunkData, SAXChunkConsumer _consumer ) 147 throws SAXException , IOException { 148 _consumer.processChunk( _chunkData ); 149 return _consumer; 150 } 151 152 public final void endInputSequence() throws Exception { 153 if (debug) { 154 System.out.println( "helper: endInputSequence..." ); 155 } 156 } 157 158 167 public final SAXChunkProducer beginOutputSequence( NodeList _pnodes, int _depth ) throws Exception { 168 if (_pnodes == null) { 169 _pnodes = new ModifiableNodeList(1); 170 ((ModifiableNodeList)_pnodes).addNode(getDocument()); 171 } 172 return new SAXChunkProducer( _pnodes, _depth ); 173 } 174 175 public final SAXChunkProducer createNextChunk( SAXChunkProducer producer ) throws SAXException { 176 producer.createNextChunk(); 177 return producer; 178 } 179 180 public final void endOutputSequence() throws Exception { 181 if (debug) { 182 System.out.println( "helper: endOutputSequence..." ); 183 } 184 } 185 186 public final XObject executeXPath( OzoneXPathQuery _query ) throws Exception { 187 if (_query.rootNode == null) { 188 _query.rootNode = getDocument(); 189 } 190 XPathQuery query = xpathQueryFactory.newXPathQuery(); 191 query.setQString( _query.qstring ); 192 if (_query.filter != null) { 193 query.setNodeFilter( _query.filter ); 194 } 195 if (_query.namespace != null) { 196 query.setNamespace( _query.namespace ); 197 } 198 return query.execute( _query.rootNode ); 199 } 200 201 public final void executeXUpdate( OzoneXUpdateQuery _query ) throws Exception { 202 if (_query.rootNode == null) { 203 _query.rootNode = getDocument(); 204 } 205 XUpdateQuery query = xupdateQueryFactory.newXUpdateQuery(); 206 query.setQString( _query.qstring ); 207 if (_query.filter != null) { 208 query.setNodeFilter( _query.filter ); 209 } 210 if (_query.namespace != null) { 211 query.setNamespace( _query.namespace ); 212 } 213 System.out.println( query.getClass().getName() ); 214 System.out.println( _query.qstring ); 215 System.out.println( _query.rootNode ); 216 query.execute( _query.rootNode ); 217 } 218 219 224 public final String xpathForNode( Node _pnode ) { 225 if (_pnode == null) { 226 throw new IllegalArgumentException ( "_pnode == null." ); 227 } 228 StringBuffer xpath = new StringBuffer (); 229 xpath = iterateBack( _pnode, xpath ); 230 return xpath.toString(); 231 } 232 233 235 private final StringBuffer iterateBack( Node node, StringBuffer buffer ) { 236 int nthChild = 1; 237 String nodeName = node.getNodeName(); 238 Node prevNode = node; 239 while ((prevNode = prevNode.getPreviousSibling()) != null) { 240 if (prevNode.getNodeName().equals( nodeName ) && prevNode.getNodeType() == node.getNodeType()) { 241 nthChild++; 242 } 243 } 244 if (node.getNodeType() == Node.TEXT_NODE) { 245 nodeName = "text()"; 246 } else if (node.getNodeType() == Node.COMMENT_NODE) { 247 nodeName = "comment()"; 248 } else { 249 if (node.getNodeType() == Node.DOCUMENT_NODE) { 250 nodeName = ""; 251 } 252 } 253 buffer.insert( 0, "/" + nodeName + (nodeName.length() > 0 ? "[" + nthChild + "]" : "") ); 254 node = node.getParentNode(); 255 if (node != null && node.getNodeType() != Node.DOCUMENT_NODE) { 256 buffer = iterateBack( node, buffer ); 257 } 258 return buffer; 259 } 260 } | Popular Tags |