You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xerces.apache.org by el...@apache.org on 2001/11/21 17:30:13 UTC
cvs commit: xml-xerces/java/src/org/apache/xml/serialize BaseMarkupSerializer.java XMLSerializer.java
elena 01/11/21 08:30:13
Modified: java/src/org/apache/xerces/dom DOMImplementationImpl.java
DocumentImpl.java
java/src/org/apache/xml/serialize BaseMarkupSerializer.java
XMLSerializer.java
Log:
DOM L3 DOMWriter implementation.
Submitted by: Rahul Srivastava
Reviewed by: Elena Litani
Revision Changes Path
1.17 +23 -1 xml-xerces/java/src/org/apache/xerces/dom/DOMImplementationImpl.java
Index: DOMImplementationImpl.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/DOMImplementationImpl.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- DOMImplementationImpl.java 2001/11/02 20:32:53 1.16
+++ DOMImplementationImpl.java 2001/11/21 16:30:13 1.17
@@ -63,6 +63,13 @@
import org.w3c.dom.DocumentType;
import org.w3c.dom.Element;
+// DOM L3 LS
+import org.apache.xerces.dom3.ls.DOMImplementationLS;
+import org.apache.xerces.dom3.ls.DOMBuilder;
+import org.apache.xerces.dom3.ls.DOMWriter;
+import org.apache.xerces.dom3.ls.DOMInputSource;
+import org.apache.xml.serialize.XMLSerializer;
+
/**
* The DOMImplementation class is description of a particular
* implementation of the Document Object Model. As such its data is
@@ -76,7 +83,7 @@
* @since PR-DOM-Level-1-19980818.
*/
public class DOMImplementationImpl
- implements DOMImplementation {
+ implements DOMImplementation, DOMImplementationLS {
//
// Data
@@ -201,6 +208,21 @@
Element e = doc.createElementNS( namespaceURI, qualifiedName);
doc.appendChild(e);
return doc;
+ }
+
+ // DOM L3 LS
+
+ public DOMBuilder createDOMBuilder(short mode)
+ throws DOMException {
+ return null;
+ }
+
+ public DOMWriter createDOMWriter() {
+ return new XMLSerializer();
+ }
+
+ public DOMInputSource createDOMInputSource() {
+ return null;
}
} // class DOMImplementationImpl
1.62 +37 -1 xml-xerces/java/src/org/apache/xerces/dom/DocumentImpl.java
Index: DocumentImpl.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xerces/dom/DocumentImpl.java,v
retrieving revision 1.61
retrieving revision 1.62
diff -u -r1.61 -r1.62
--- DocumentImpl.java 2001/08/23 00:35:20 1.61
+++ DocumentImpl.java 2001/11/21 16:30:13 1.62
@@ -88,7 +88,12 @@
import org.apache.xerces.dom.events.EventImpl;
import org.apache.xerces.dom.events.MutationEventImpl;
+// DOM L3 LS
+import org.apache.xerces.dom3.ls.DOMWriter;
+import org.apache.xerces.dom3.ls.DocumentLS;
+import org.apache.xerces.dom3.ls.DOMImplementationLS;
+
/**
* The Document interface represents the entire HTML or XML document.
* Conceptually, it is the root of the document tree, and provides the
@@ -121,7 +126,7 @@
*/
public class DocumentImpl
extends CoreDocumentImpl
- implements DocumentTraversal, DocumentEvent, DocumentRange {
+ implements DocumentTraversal, DocumentEvent, DocumentRange, DocumentLS {
//
// Constants
@@ -1274,6 +1279,37 @@
// Attr's subtree has not been changed by this operation.
dispatchAggregateEvents(oldOwner, null, null, (short) 0);
}
+ }
+
+ // DOM L3 LS
+
+
+ public boolean getAsync() {
+ return false;
+ }
+
+ public void setAsync(boolean async) {
+ }
+
+ public void abort() {
+ }
+
+ public boolean load(String uri) {
+ return false;
+ }
+
+ public boolean loadXML(String source) {
+ return false;
+ }
+
+ public String saveXML(Node snode)
+ throws DOMException {
+ if ( snode != null &&
+ getOwnerDocument() != snode.getOwnerDocument() )
+ throw new DOMException(DOMException.WRONG_DOCUMENT_ERR,"Node "+snode.getNodeName()+" does not belongs to this Document.");
+ DOMImplementationLS domImplLS = (DOMImplementationLS)DOMImplementationImpl.getDOMImplementation();
+ DOMWriter xmlWriter = domImplLS.createDOMWriter();
+ return xmlWriter.writeToString(snode);
}
} // class DocumentImpl
1.27 +21 -2 xml-xerces/java/src/org/apache/xml/serialize/BaseMarkupSerializer.java
Index: BaseMarkupSerializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/BaseMarkupSerializer.java,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -r1.26 -r1.27
--- BaseMarkupSerializer.java 2001/07/11 15:40:10 1.26
+++ BaseMarkupSerializer.java 2001/11/21 16:30:13 1.27
@@ -94,6 +94,10 @@
import java.util.StringTokenizer;
import org.w3c.dom.*;
+
+import org.apache.xerces.dom3.DOMErrorHandler;
+
+
import org.xml.sax.DocumentHandler;
import org.xml.sax.DTDHandler;
import org.xml.sax.Locator;
@@ -121,7 +125,7 @@
* as specified in the output format.
* <p>
* The serializer supports both DOM and SAX. DOM serializing is done
- * by calling {@link #serialize} and SAX serializing is done by firing
+ * by calling {@link #serialize(Document)} and SAX serializing is done by firing
* SAX events and using the serializer as a document handler.
* This also applies to derived class.
* <p>
@@ -143,8 +147,9 @@
* another element.
*
*
- * @version $Revision: 1.26 $ $Date: 2001/07/11 15:40:10 $
+ * @version $Revision: 1.27 $ $Date: 2001/11/21 16:30:13 $
* @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
+ * @author <a href="mailto:rahul.srivastava@sun.com">Rahul Srivastava</a>
* @see Serializer
* @see DOMSerializer
*/
@@ -153,6 +158,9 @@
DTDHandler, DeclHandler, DOMSerializer, Serializer
{
+ // DOM L3 implementation
+ protected Hashtable fFeatures;
+ protected DOMErrorHandler fDOMErrorHandler;
private EncodingInfo _encodingInfo;
@@ -1009,9 +1017,17 @@
endCDATA();
content();
child = node.getFirstChild();
+ if ( child == null ||
+ !((Boolean)fFeatures.get("expand-entity-references")).booleanValue() ) {
+ _printer.printText("&");
+ _printer.printText(node.getNodeName());
+ _printer.printText(";");
+ }
+ else {
while ( child != null ) {
serializeNode( child );
child = child.getNextSibling();
+ }
}
break;
}
@@ -1179,6 +1195,9 @@
state.inCData = true;
}
index = text.indexOf( "]]>" );
+ if (index >=0 && !((Boolean)fFeatures.get("split-cdata-sections")).booleanValue()) {
+ // REVISIT: issue fatal error
+ }
while ( index >= 0 ) {
buffer.append( text.substring( 0, index + 2 ) ).append( "]]><![CDATA[" );
text = text.substring( index + 2 );
1.24 +156 -1 xml-xerces/java/src/org/apache/xml/serialize/XMLSerializer.java
Index: XMLSerializer.java
===================================================================
RCS file: /home/cvs/xml-xerces/java/src/org/apache/xml/serialize/XMLSerializer.java,v
retrieving revision 1.23
retrieving revision 1.24
diff -u -r1.23 -r1.24
--- XMLSerializer.java 2001/11/05 15:31:56 1.23
+++ XMLSerializer.java 2001/11/21 16:30:13 1.24
@@ -78,6 +78,8 @@
import java.io.UnsupportedEncodingException;
import java.io.OutputStream;
import java.io.Writer;
+import java.io.StringWriter;
+import java.util.Hashtable;
import java.util.Enumeration;
import org.w3c.dom.*;
@@ -88,6 +90,8 @@
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
+import org.apache.xerces.dom3.ls.DOMWriter;
+import org.apache.xerces.dom3.DOMErrorHandler;
/**
* Implements an XML serializer supporting both DOM and SAX pretty
@@ -114,15 +118,20 @@
* spaces at beginning of line will be stripped.
*
*
- * @version $Revision: 1.23 $ $Date: 2001/11/05 15:31:56 $
+ * @version $Revision: 1.24 $ $Date: 2001/11/21 16:30:13 $
* @author <a href="mailto:arkin@intalio.com">Assaf Arkin</a>
+ * @author <a href="mailto:rahul.srivastava@sun.com">Rahul Srivastava</a>
* @see Serializer
*/
public class XMLSerializer
extends BaseMarkupSerializer
+ implements DOMWriter
{
+ private String fEncoding;
+ private String fLastEncoding;
+
/**
* Constructs a new serializer. The serializer cannot be used without
* calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
@@ -131,6 +140,8 @@
public XMLSerializer()
{
super( new OutputFormat( Method.XML, null, false ) );
+ fFeatures = new Hashtable();
+ initFeatures();
}
@@ -143,6 +154,8 @@
{
super( format != null ? format : new OutputFormat( Method.XML, null, false ) );
_format.setMethod( Method.XML );
+ fFeatures = new Hashtable();
+ initFeatures();
}
@@ -159,6 +172,8 @@
super( format != null ? format : new OutputFormat( Method.XML, null, false ) );
_format.setMethod( Method.XML );
setOutputCharStream( writer );
+ fFeatures = new Hashtable();
+ initFeatures();
}
@@ -175,6 +190,8 @@
super( format != null ? format : new OutputFormat( Method.XML, null, false ) );
_format.setMethod( Method.XML );
setOutputByteStream( output );
+ fFeatures = new Hashtable();
+ initFeatures();
}
@@ -753,6 +770,144 @@
}
return attrsOnly;
}
+
+ // *****************************
+
+ private void initFeatures() {
+ fFeatures.put("normalize-characters",new Boolean(false));
+ fFeatures.put("split-cdata-sections",new Boolean(true));
+ fFeatures.put("validation",new Boolean(false));
+ fFeatures.put("expand-entity-references",new Boolean(false));
+ fFeatures.put("whitespace-in-element-content",new Boolean(true));
+ fFeatures.put("discard-default-content",new Boolean(true));
+ fFeatures.put("format-canonical",new Boolean(false));
+ fFeatures.put("format-pretty-print",new Boolean(false));
+ }
+
+ private void checkAllFeatures() {
+ if (getFeature("whitespace-in-element-content"))
+ _format.setPreserveSpace(true);
+ else
+ _format.setPreserveSpace(false);
+ }
+
+ public void setFeature(String name,
+ boolean state)
+ throws DOMException {
+ if (name != null && fFeatures.containsKey(name))
+ if (canSetFeature(name,state))
+ fFeatures.put(name,new Boolean(state));
+ else
+ throw new DOMException(DOMException.NOT_SUPPORTED_ERR,"Feature "+name+" cannot be set as "+state);
+ else
+ throw new DOMException(DOMException.NOT_FOUND_ERR,"Feature "+name+" not found");
+ }
+
+ public boolean canSetFeature(String name, boolean state) {
+ if (name.equals("normalize-characters") && state)
+ return false;
+ else if (name.equals("validation") && state)
+ return false;
+ else if (name.equals("whitespace-in-element-content") && !state)
+ return false;
+ else if (name.equals("format-canonical") && state)
+ return false;
+ else if (name.equals("format-pretty-print") && state)
+ return false;
+ else
+ return true;
+ }
+
+ public boolean getFeature(String name)
+ throws DOMException {
+ Boolean state = (Boolean)fFeatures.get(name);
+ if (state == null)
+ throw new DOMException(DOMException.NOT_FOUND_ERR,"Feature "+name+" not found");
+ return state.booleanValue();
+ }
+
+ public String getEncoding() {
+ return fEncoding;
+ }
+
+ public void setEncoding(String encoding) {
+ _format.setEncoding(encoding);
+ fEncoding = _format.getEncoding();
+ }
+
+ public String getLastEncoding() {
+ return fLastEncoding;
+ }
+
+ public String getNewLine() {
+ return _format.getLineSeparator();
+ }
+
+ public void setNewLine(String newLine) {
+ _format.setLineSeparator(newLine);
+ }
+
+ public DOMErrorHandler getErrorHandler() {
+ return fDOMErrorHandler;
+ }
+
+ public void setErrorHandler(DOMErrorHandler errorHandler) {
+ fDOMErrorHandler = errorHandler;
+ }
+
+ public boolean writeNode(java.io.OutputStream destination,
+ Node wnode)
+ throws Exception {
+ checkAllFeatures();
+ try {
+ setOutputByteStream(destination);
+ //REVISIT: do we serialize complete Document when wnode is null??
+ if (wnode == null)
+ return false;
+ else if (wnode.getNodeType() == Node.DOCUMENT_NODE)
+ serialize((Document)wnode);
+ else if (wnode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
+ serialize((DocumentFragment)wnode);
+ else if (wnode.getNodeType() == Node.ELEMENT_NODE)
+ serialize((Element)wnode);
+ else
+ return false;
+ }
+ catch(NullPointerException npe) {
+ throw npe;
+ }
+ catch(IOException ioe) {
+ throw ioe;
+ }
+ fLastEncoding = getEncoding();
+ return true;
+ }
+
+ public String writeToString(Node wnode)
+ throws DOMException {
+ checkAllFeatures();
+ StringWriter destination = new StringWriter();
+ try {
+ setOutputCharStream(destination);
+ //REVISIT: do we serialize complete Document when wnode is null??
+ if (wnode == null)
+ return null;
+ else if (wnode.getNodeType() == Node.DOCUMENT_NODE)
+ serialize((Document)wnode);
+ else if (wnode.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE)
+ serialize((DocumentFragment)wnode);
+ else if (wnode.getNodeType() == Node.ELEMENT_NODE)
+ serialize((Element)wnode);
+ else
+ return null;
+ }
+ catch(IOException ioe) {
+ throw new DOMException(DOMException.DOMSTRING_SIZE_ERR,"The resulting string is too long to fit in a DOMString: "+ioe.getMessage());
+ }
+ fLastEncoding = getEncoding();
+ return destination.toString();
+ }
+
}
---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-cvs-help@xml.apache.org