1 16 19 package org.apache.xml.dtm.ref; 20 21 import javax.xml.parsers.DocumentBuilder ; 22 import javax.xml.parsers.DocumentBuilderFactory ; 23 import javax.xml.transform.Source ; 24 import javax.xml.transform.dom.DOMSource ; 25 import javax.xml.transform.sax.SAXSource ; 26 import javax.xml.transform.stream.StreamSource ; 27 28 import org.apache.xml.dtm.DTM; 29 import org.apache.xml.dtm.DTMException; 30 import org.apache.xml.dtm.DTMFilter; 31 import org.apache.xml.dtm.DTMIterator; 32 import org.apache.xml.dtm.DTMManager; 33 import org.apache.xml.dtm.DTMWSFilter; 34 import org.apache.xml.dtm.ref.dom2dtm.DOM2DTM; 35 import org.apache.xml.dtm.ref.sax2dtm.SAX2DTM; 36 import org.apache.xml.dtm.ref.sax2dtm.SAX2RTFDTM; 37 import org.apache.xml.res.XMLErrorResources; 38 import org.apache.xml.res.XMLMessages; 39 import org.apache.xml.utils.PrefixResolver; 40 import org.apache.xml.utils.SystemIDResolver; 41 import org.apache.xml.utils.XMLReaderManager; 42 import org.apache.xml.utils.XMLStringFactory; 43 44 import org.w3c.dom.Document ; 45 import org.w3c.dom.Node ; 46 47 import org.xml.sax.InputSource ; 48 import org.xml.sax.SAXException ; 49 import org.xml.sax.SAXNotRecognizedException ; 50 import org.xml.sax.SAXNotSupportedException ; 51 import org.xml.sax.XMLReader ; 52 import org.xml.sax.helpers.XMLReaderFactory ; 53 54 71 public class DTMManagerDefault extends DTMManager 72 { 73 75 76 private static final boolean DUMPTREE = false; 77 78 79 private static final boolean DEBUG = false; 80 81 92 protected DTM m_dtms[] = new DTM[256]; 93 94 107 int m_dtm_offsets[] = new int[256]; 108 109 113 protected XMLReaderManager m_readerManager = null; 114 115 123 synchronized public void addDTM(DTM dtm, int id) { addDTM(dtm,id,0); } 124 125 126 136 synchronized public void addDTM(DTM dtm, int id, int offset) 137 { 138 if(id>=IDENT_MAX_DTMS) 139 { 140 throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NO_DTMIDS_AVAIL, null)); } 143 144 int oldlen=m_dtms.length; 150 if(oldlen<=id) 151 { 152 int newlen=Math.min((id+256),IDENT_MAX_DTMS); 158 159 DTM new_m_dtms[] = new DTM[newlen]; 160 System.arraycopy(m_dtms,0,new_m_dtms,0,oldlen); 161 m_dtms=new_m_dtms; 162 int new_m_dtm_offsets[] = new int[newlen]; 163 System.arraycopy(m_dtm_offsets,0,new_m_dtm_offsets,0,oldlen); 164 m_dtm_offsets=new_m_dtm_offsets; 165 } 166 167 m_dtms[id] = dtm; 168 m_dtm_offsets[id]=offset; 169 dtm.documentRegistration(); 170 } 173 174 177 synchronized public int getFirstFreeDTMID() 178 { 179 int n = m_dtms.length; 180 for (int i = 1; i < n; i++) 181 { 182 if(null == m_dtms[i]) 183 { 184 return i; 185 } 186 } 187 return n; } 189 190 193 private ExpandedNameTable m_expandedNameTable = 194 new ExpandedNameTable(); 195 196 200 public DTMManagerDefault(){} 201 202 203 229 synchronized public DTM getDTM(Source source, boolean unique, 230 DTMWSFilter whiteSpaceFilter, 231 boolean incremental, boolean doIndexing) 232 { 233 234 if(DEBUG && null != source) 235 System.out.println("Starting "+ 236 (unique ? "UNIQUE" : "shared")+ 237 " source: "+source.getSystemId() 238 ); 239 240 XMLStringFactory xstringFactory = m_xsf; 241 int dtmPos = getFirstFreeDTMID(); 242 int documentID = dtmPos << IDENT_DTM_NODE_BITS; 243 244 if ((null != source) && source instanceof DOMSource ) 245 { 246 DOM2DTM dtm = new DOM2DTM(this, (DOMSource ) source, documentID, 247 whiteSpaceFilter, xstringFactory, doIndexing); 248 249 addDTM(dtm, dtmPos, 0); 250 251 256 return dtm; 257 } 258 else 259 { 260 boolean isSAXSource = (null != source) 261 ? (source instanceof SAXSource ) : true; 262 boolean isStreamSource = (null != source) 263 ? (source instanceof StreamSource ) : false; 264 265 if (isSAXSource || isStreamSource) { 266 XMLReader reader = null; 267 268 try { 269 InputSource xmlSource; 270 271 if (null == source) { 272 xmlSource = null; 273 } else { 274 reader = getXMLReader(source); 275 xmlSource = SAXSource.sourceToInputSource(source); 276 277 String urlOfSource = xmlSource.getSystemId(); 278 279 if (null != urlOfSource) { 280 try { 281 urlOfSource = SystemIDResolver.getAbsoluteURI(urlOfSource); 282 } catch (Exception e) { 283 System.err.println("Can not absolutize URL: " + urlOfSource); 285 } 286 287 xmlSource.setSystemId(urlOfSource); 288 } 289 } 290 291 SAX2DTM dtm; 292 if (source==null && unique && !incremental && !doIndexing) { 293 dtm = new SAX2RTFDTM(this, source, documentID, whiteSpaceFilter, 301 xstringFactory, doIndexing); 302 } 303 310 else { 312 dtm = new SAX2DTM(this, source, documentID, whiteSpaceFilter, 313 xstringFactory, doIndexing); 314 } 315 316 addDTM(dtm, dtmPos, 0); 320 321 322 boolean haveXercesParser = 323 (null != reader) 324 && (reader.getClass() 325 .getName() 326 .equals("org.apache.xerces.parsers.SAXParser") ); 327 328 if (haveXercesParser) { 329 incremental = true; } 331 332 if (m_incremental && incremental 335 ) { 336 IncrementalSAXSource coParser=null; 337 338 if (haveXercesParser) { 339 try { 341 coParser =(IncrementalSAXSource) 342 Class.forName("org.apache.xml.dtm.ref.IncrementalSAXSource_Xerces").newInstance(); 343 } catch( Exception ex ) { 344 ex.printStackTrace(); 345 coParser=null; 346 } 347 } 348 349 if (coParser==null ) { 350 if (null == reader) { 352 coParser = new IncrementalSAXSource_Filter(); 353 } else { 354 IncrementalSAXSource_Filter filter = 355 new IncrementalSAXSource_Filter(); 356 filter.setXMLReader(reader); 357 coParser=filter; 358 } 359 } 360 361 362 379 380 dtm.setIncrementalSAXSource(coParser); 382 383 if (null == xmlSource) { 384 385 return dtm; 387 } 388 389 if (null == reader.getErrorHandler()) { 390 reader.setErrorHandler(dtm); 391 } 392 reader.setDTDHandler(dtm); 393 394 try { 395 398 coParser.startParse(xmlSource); 399 } catch (RuntimeException re) { 400 401 dtm.clearCoRoutine(); 402 403 throw re; 404 } catch (Exception e) { 405 406 dtm.clearCoRoutine(); 407 408 throw new org.apache.xml.utils.WrappedRuntimeException(e); 409 } 410 } else { 411 if (null == reader) { 412 413 return dtm; 415 } 416 417 reader.setContentHandler(dtm); 419 reader.setDTDHandler(dtm); 420 if (null == reader.getErrorHandler()) { 421 reader.setErrorHandler(dtm); 422 } 423 424 try { 425 reader.setProperty( 426 "http://xml.org/sax/properties/lexical-handler", 427 dtm); 428 } catch (SAXNotRecognizedException e){} 429 catch (SAXNotSupportedException e){} 430 431 try { 432 reader.parse(xmlSource); 433 } catch (RuntimeException re) { 434 dtm.clearCoRoutine(); 435 436 throw re; 437 } catch (Exception e) { 438 dtm.clearCoRoutine(); 439 440 throw new org.apache.xml.utils.WrappedRuntimeException(e); 441 } 442 } 443 444 if (DUMPTREE) { 445 System.out.println("Dumping SAX2DOM"); 446 dtm.dumpDTM(System.err); 447 } 448 449 return dtm; 450 } finally { 451 releaseXMLReader(reader); 452 } 453 } else { 454 455 throw new DTMException(XMLMessages.createXMLMessage(XMLErrorResources.ER_NOT_SUPPORTED, new Object []{source})); } 459 } 460 } 461 462 471 synchronized public int getDTMHandleFromNode(org.w3c.dom.Node node) 472 { 473 if(null == node) 474 throw new IllegalArgumentException (XMLMessages.createXMLMessage(XMLErrorResources.ER_NODE_NON_NULL, null)); 476 if (node instanceof org.apache.xml.dtm.ref.DTMNodeProxy) 477 return ((org.apache.xml.dtm.ref.DTMNodeProxy) node).getDTMNodeNumber(); 478 479 else 480 { 481 int max = m_dtms.length; 503 for(int i = 0; i < max; i++) 504 { 505 DTM thisDTM=m_dtms[i]; 506 if((null != thisDTM) && thisDTM instanceof DOM2DTM) 507 { 508 int handle=((DOM2DTM)thisDTM).getHandleOfNode(node); 509 if(handle!=DTM.NULL) return handle; 510 } 511 } 512 513 529 Node root = node; 533 Node p = (root.getNodeType() == Node.ATTRIBUTE_NODE) ? ((org.w3c.dom.Attr )root).getOwnerElement() : root.getParentNode(); 534 for (; p != null; p = p.getParentNode()) 535 { 536 root = p; 537 } 538 539 DOM2DTM dtm = (DOM2DTM) getDTM(new javax.xml.transform.dom.DOMSource (root), 540 false, null, true, true); 541 542 int handle; 543 544 if(node instanceof org.apache.xml.dtm.ref.dom2dtm.DOM2DTMdefaultNamespaceDeclarationNode) 545 { 546 handle=dtm.getHandleOfNode(((org.w3c.dom.Attr )node).getOwnerElement()); 550 handle=dtm.getAttributeNode(handle,node.getNamespaceURI(),node.getLocalName()); 551 } 552 else 553 handle = ((DOM2DTM)dtm).getHandleOfNode(node); 554 555 if(DTM.NULL == handle) 556 throw new RuntimeException (XMLMessages.createXMLMessage(XMLErrorResources.ER_COULD_NOT_RESOLVE_NODE, null)); 558 return handle; 559 } 560 } 561 562 576 synchronized public XMLReader getXMLReader(Source inputSource) 577 { 578 579 try 580 { 581 XMLReader reader = (inputSource instanceof SAXSource ) 582 ? ((SAXSource ) inputSource).getXMLReader() : null; 583 584 if (null == reader) { 586 if (m_readerManager == null) { 587 m_readerManager = XMLReaderManager.getInstance(); 588 } 589 590 reader = m_readerManager.getXMLReader(); 591 } 592 593 return reader; 594 595 } catch (SAXException se) { 596 throw new DTMException(se.getMessage(), se); 597 } 598 } 599 600 610 synchronized public void releaseXMLReader(XMLReader reader) { 611 if (m_readerManager != null) { 612 m_readerManager.releaseXMLReader(reader); 613 } 614 } 615 616 623 synchronized public DTM getDTM(int nodeHandle) 624 { 625 try 626 { 627 return m_dtms[nodeHandle >>> IDENT_DTM_NODE_BITS]; 629 } 630 catch(java.lang.ArrayIndexOutOfBoundsException e) 631 { 632 if(nodeHandle==DTM.NULL) 633 return null; else 635 throw e; } 637 } 638 639 649 synchronized public int getDTMIdentity(DTM dtm) 650 { 651 if(dtm instanceof DTMDefaultBase) 654 { 655 DTMDefaultBase dtmdb=(DTMDefaultBase)dtm; 656 if(dtmdb.getManager()==this) 657 return dtmdb.getDTMIDs().elementAt(0); 658 else 659 return -1; 660 } 661 662 int n = m_dtms.length; 663 664 for (int i = 0; i < n; i++) 665 { 666 DTM tdtm = m_dtms[i]; 667 668 if (tdtm == dtm && m_dtm_offsets[i]==0) 669 return i << IDENT_DTM_NODE_BITS; 670 } 671 672 return -1; 673 } 674 675 690 synchronized public boolean release(DTM dtm, boolean shouldHardDelete) 691 { 692 if(DEBUG) 693 { 694 System.out.println("Releasing "+ 695 (shouldHardDelete ? "HARD" : "soft")+ 696 " dtm="+ 697 dtm.getDocumentBaseURI() 700 ); 701 } 702 703 if (dtm instanceof SAX2DTM) 704 { 705 ((SAX2DTM) dtm).clearCoRoutine(); 706 } 707 708 if(dtm instanceof DTMDefaultBase) 717 { 718 org.apache.xml.utils.SuballocatedIntVector ids=((DTMDefaultBase)dtm).getDTMIDs(); 719 for(int i=ids.size()-1;i>=0;--i) 720 m_dtms[ids.elementAt(i)>>>DTMManager.IDENT_DTM_NODE_BITS]=null; 721 } 722 else 723 { 724 int i = getDTMIdentity(dtm); 725 if (i >= 0) 726 { 727 m_dtms[i >>> DTMManager.IDENT_DTM_NODE_BITS] = null; 728 } 729 } 730 731 dtm.documentRelease(); 732 return true; 733 } 734 735 741 synchronized public DTM createDocumentFragment() 742 { 743 744 try 745 { 746 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 747 748 dbf.setNamespaceAware(true); 749 750 DocumentBuilder db = dbf.newDocumentBuilder(); 751 Document doc = db.newDocument(); 752 Node df = doc.createDocumentFragment(); 753 754 return getDTM(new DOMSource (df), true, null, false, false); 755 } 756 catch (Exception e) 757 { 758 throw new DTMException(e); 759 } 760 } 761 762 772 synchronized public DTMIterator createDTMIterator(int whatToShow, DTMFilter filter, 773 boolean entityReferenceExpansion) 774 { 775 776 777 return null; 778 } 779 780 789 synchronized public DTMIterator createDTMIterator(String xpathString, 790 PrefixResolver presolver) 791 { 792 793 794 return null; 795 } 796 797 805 synchronized public DTMIterator createDTMIterator(int node) 806 { 807 808 809 return null; 810 } 811 812 821 synchronized public DTMIterator createDTMIterator(Object xpathCompiler, int pos) 822 { 823 824 825 return null; 826 } 827 828 835 public ExpandedNameTable getExpandedNameTable(DTM dtm) 836 { 837 return m_expandedNameTable; 838 } 839 } 840 | Popular Tags |