You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by mr...@apache.org on 2005/12/08 00:06:25 UTC

svn commit: r354893 - in /xerces/java/trunk/src/org/apache/xerces/impl/dtd: XML11DTDProcessor.java XMLDTDLoader.java

Author: mrglavas
Date: Wed Dec  7 15:06:22 2005
New Revision: 354893

URL: http://svn.apache.org/viewcvs?rev=354893&view=rev
Log:
Added a method to the DTD loader to make it possible to fill an existing
DTD grammar owned by a validator with a specified internal subset and
external subset. The DOM normalizer requires such a method. Previously
the DOM normalizer was creating a grammar pool and tried to load the 
internal subset and external subset with separate calls to loadGrammar().
This approach doesn't work since it generates two DTDGrammar objects and
it also has the potential to overwrite the application's grammar pool
which was registered with the DOMConfiguration.

Modified:
    xerces/java/trunk/src/org/apache/xerces/impl/dtd/XML11DTDProcessor.java
    xerces/java/trunk/src/org/apache/xerces/impl/dtd/XMLDTDLoader.java

Modified: xerces/java/trunk/src/org/apache/xerces/impl/dtd/XML11DTDProcessor.java
URL: http://svn.apache.org/viewcvs/xerces/java/trunk/src/org/apache/xerces/impl/dtd/XML11DTDProcessor.java?rev=354893&r1=354892&r2=354893&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/dtd/XML11DTDProcessor.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/impl/dtd/XML11DTDProcessor.java Wed Dec  7 15:06:22 2005
@@ -1,5 +1,5 @@
 /*
- * Copyright 1999-2002,2004 The Apache Software Foundation.
+ * Copyright 1999-2002,2004,2005 The Apache Software Foundation.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -16,13 +16,16 @@
 
 package org.apache.xerces.impl.dtd;
 
+import org.apache.xerces.impl.Constants;
+import org.apache.xerces.impl.XML11DTDScannerImpl;
+import org.apache.xerces.impl.XMLDTDScannerImpl;
+import org.apache.xerces.impl.XMLEntityManager;
 import org.apache.xerces.impl.XMLErrorReporter;
 import org.apache.xerces.util.SymbolTable;
 import org.apache.xerces.util.XML11Char;
 import org.apache.xerces.xni.grammars.XMLGrammarPool;
 import org.apache.xerces.xni.parser.XMLEntityResolver;
 
-
 /**
  * This class extends XMLDTDProcessor by giving it
  * the ability to parse XML 1.1 documents correctly.  It can also be used
@@ -68,5 +71,14 @@
     protected boolean isValidName(String name) {
         return XML11Char.isXML11ValidName(name);
     } // isValidNmtoken(String):  boolean
+    
+    protected XMLDTDScannerImpl createDTDScanner(SymbolTable symbolTable,
+            XMLErrorReporter errorReporter, XMLEntityManager entityManager) {
+        return new XML11DTDScannerImpl(symbolTable, errorReporter, entityManager);
+    } // createDTDScanner(SymbolTable, XMLErrorReporter, XMLEntityManager) : XMLDTDScannerImpl
+    
+    protected short getScannerVersion() {
+        return Constants.XML_VERSION_1_1;
+    } // getScannerVersion() : short
     
 } // class XML11DTDProcessor

Modified: xerces/java/trunk/src/org/apache/xerces/impl/dtd/XMLDTDLoader.java
URL: http://svn.apache.org/viewcvs/xerces/java/trunk/src/org/apache/xerces/impl/dtd/XMLDTDLoader.java?rev=354893&r1=354892&r2=354893&view=diff
==============================================================================
--- xerces/java/trunk/src/org/apache/xerces/impl/dtd/XMLDTDLoader.java (original)
+++ xerces/java/trunk/src/org/apache/xerces/impl/dtd/XMLDTDLoader.java Wed Dec  7 15:06:22 2005
@@ -37,6 +37,7 @@
 import java.util.Locale;
 import java.io.IOException;
 import java.io.EOFException;
+import java.io.StringReader;
 
 /**
  * The DTD loader. The loader knows how to build grammars from XMLInputSources.
@@ -58,6 +59,7 @@
  * @xerces.internal
  *
  * @author Neil Graham, IBM
+ * @author Michael Glavassevich, IBM
  *
  * @version $Id$
  */
@@ -167,7 +169,7 @@
             fEntityManager = new XMLEntityManager();
         }
         fEntityManager.setProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.ERROR_REPORTER_PROPERTY, errorReporter);
-        fDTDScanner = new XMLDTDScannerImpl(fSymbolTable, fErrorReporter, fEntityManager);
+        fDTDScanner = createDTDScanner(fSymbolTable, fErrorReporter, fEntityManager);
         fDTDScanner.setDTDHandler(this);
         fDTDScanner.setDTDContentModelHandler(this);
         reset();
@@ -290,6 +292,7 @@
             fErrorReporter.setProperty(propertyId, value);
         } else if(propertyId.equals( ENTITY_RESOLVER)) {
             fEntityResolver = (XMLEntityResolver)value;
+            fEntityManager.setProperty(propertyId, value);
         } else if(propertyId.equals( GRAMMAR_POOL)) {
             fGrammarPool = (XMLGrammarPool)value;
         } else {
@@ -363,6 +366,7 @@
      */
     public void setEntityResolver(XMLEntityResolver entityResolver) {
         fEntityResolver = entityResolver;
+        fEntityManager.setProperty(ENTITY_RESOLVER, entityResolver);
     } // setEntityResolver(XMLEntityResolver)
 
     /** Returns the registered entity resolver.  */
@@ -414,6 +418,50 @@
         }
         return fDTDGrammar;
     } // loadGrammar(XMLInputSource):  Grammar
+    
+    /**
+     * Parse a DTD internal and/or external subset and insert the content
+     * into the existing DTD grammar owned by the given DTDValidator.
+     */
+    public void loadGrammarWithContext(XMLDTDValidator validator, String rootName,
+            String publicId, String systemId, String baseSystemId, String internalSubset) 
+        throws IOException, XNIException {
+        final DTDGrammarBucket grammarBucket = validator.getGrammarBucket();
+        final DTDGrammar activeGrammar = grammarBucket.getActiveGrammar();
+        if (activeGrammar != null && !activeGrammar.isImmutable()) {
+            fGrammarBucket = grammarBucket;
+            fEntityManager.setScannerVersion(getScannerVersion());
+            reset();
+            try {
+                // process internal subset
+                if (internalSubset != null) {
+                    // To get the DTD scanner to end at the right place we have to fool
+                    // it into thinking that it reached the end of the internal subset
+                    // in a real document.
+                    StringBuffer buffer = new StringBuffer(internalSubset.length() + 2);
+                    buffer.append(internalSubset).append("]>");
+                    XMLInputSource is = new XMLInputSource(null, baseSystemId, 
+                            null, new StringReader(buffer.toString()), null);
+                    fEntityManager.startDocumentEntity(is);
+                    fDTDScanner.scanDTDInternalSubset(true, false, systemId != null);
+                }
+                // process external subset
+                if (systemId != null) {
+                    XMLDTDDescription desc = new XMLDTDDescription(publicId, systemId, baseSystemId, null, rootName);
+                    XMLInputSource source = fEntityManager.resolveEntity(desc);
+                    fDTDScanner.setInputSource(source);
+                    fDTDScanner.scanDTDExternalSubset(true);
+                }
+            } 
+            catch (EOFException e) {
+                // expected behaviour...
+            }
+            finally {
+                // Close all streams opened by the parser.
+                fEntityManager.closeReaders();
+            }
+        }
+    } // loadGrammarWithContext(XMLDTDValidator, String, XMLInputSource)
 
     // reset all the components that we rely upon
     protected void reset() {
@@ -422,5 +470,14 @@
         fEntityManager.reset();
         fErrorReporter.setDocumentLocator(fEntityManager.getEntityScanner());
     }
+    
+    protected XMLDTDScannerImpl createDTDScanner(SymbolTable symbolTable,
+            XMLErrorReporter errorReporter, XMLEntityManager entityManager) {
+        return new XMLDTDScannerImpl(symbolTable, errorReporter, entityManager);
+    } // createDTDScanner(SymbolTable, XMLErrorReporter, XMLEntityManager) : XMLDTDScannerImpl
+    
+    protected short getScannerVersion() {
+        return Constants.XML_VERSION_1_0;
+    } // getScannerVersion() : short
 
 } // class XMLDTDLoader



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@xerces.apache.org
For additional commands, e-mail: commits-help@xerces.apache.org