You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by mi...@apache.org on 2005/02/21 19:34:56 UTC
cvs commit: xml-xalan/java/src/org/apache/xml/serializer EmptySerializer.java SerializationHandler.java SerializerBase.java ToStream.java
minchau 2005/02/21 10:34:56
Modified: java/src/org/apache/xml/serializer EmptySerializer.java
SerializationHandler.java SerializerBase.java
ToStream.java
Log:
Fix for XALANJ-2033 and XALANJ-2051 so that the serializer will serialize DTD
tags if desired. The patch was reviewed by Ilene Seeleman.
Revision Changes Path
1.10 +28 -1 xml-xalan/java/src/org/apache/xml/serializer/EmptySerializer.java
Index: EmptySerializer.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xml/serializer/EmptySerializer.java,v
retrieving revision 1.9
retrieving revision 1.10
diff -u -r1.9 -r1.10
--- EmptySerializer.java 18 Feb 2005 04:35:18 -0000 1.9
+++ EmptySerializer.java 21 Feb 2005 18:34:56 -0000 1.10
@@ -725,4 +725,31 @@
{
couldThrowSAXException();
}
+ /**
+ * @see org.xml.sax.DTDHandler#notationDecl(java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void notationDecl(String arg0, String arg1, String arg2) throws SAXException
+ {
+ couldThrowSAXException();
+ }
+
+ /**
+ * @see org.xml.sax.DTDHandler#unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void unparsedEntityDecl(
+ String arg0,
+ String arg1,
+ String arg2,
+ String arg3)
+ throws SAXException {
+ couldThrowSAXException();
+ }
+
+ /**
+ * @see org.apache.xml.serializer.SerializationHandler#setDTDEntityExpansion(boolean)
+ */
+ public void setDTDEntityExpansion(boolean expand) {
+ aMethodIsCalled();
+
+ }
}
1.8 +10 -1 xml-xalan/java/src/org/apache/xml/serializer/SerializationHandler.java
Index: SerializationHandler.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xml/serializer/SerializationHandler.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- SerializationHandler.java 24 Jan 2005 04:04:40 -0000 1.7
+++ SerializationHandler.java 21 Feb 2005 18:34:56 -0000 1.8
@@ -43,6 +43,7 @@
ExtendedLexicalHandler,
XSLOutputAttributes,
DeclHandler,
+ org.xml.sax.DTDHandler,
ErrorHandler,
DOMSerializer,
Serializer
@@ -109,6 +110,14 @@
* output.
*/
public void flushPending() throws SAXException;
+
+ /**
+ * Default behavior is to expand DTD entities,
+ * that is the initall default value is true.
+ * @param expand true if DTD entities are to be expanded,
+ * false if they are to be left as DTD entity references.
+ */
+ public void setDTDEntityExpansion(boolean expand);
}
1.19 +32 -1 xml-xalan/java/src/org/apache/xml/serializer/SerializerBase.java
Index: SerializerBase.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xml/serializer/SerializerBase.java,v
retrieving revision 1.18
retrieving revision 1.19
diff -u -r1.18 -r1.19
--- SerializerBase.java 18 Feb 2005 04:35:18 -0000 1.18
+++ SerializerBase.java 21 Feb 2005 18:34:56 -0000 1.19
@@ -1332,5 +1332,36 @@
addAttributeAlways(uri, localName, rawName, type, value, false);
}
}
+
+ /**
+ * @see org.xml.sax.DTDHandler#notationDecl(java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void notationDecl(String arg0, String arg1, String arg2)
+ throws SAXException {
+ // This method just provides a definition to satisfy the interface
+ // A particular sub-class of SerializerBase provides the implementation (if desired)
+ }
+
+ /**
+ * @see org.xml.sax.DTDHandler#unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void unparsedEntityDecl(
+ String arg0,
+ String arg1,
+ String arg2,
+ String arg3)
+ throws SAXException {
+ // This method just provides a definition to satisfy the interface
+ // A particular sub-class of SerializerBase provides the implementation (if desired)
+ }
+
+ /**
+ * If set to false the serializer does not expand DTD entities,
+ * but leaves them as is, the default value is true.
+ */
+ public void setDTDEntityExpansion(boolean expand) {
+ // This method just provides a definition to satisfy the interface
+ // A particular sub-class of SerializerBase provides the implementation (if desired)
+ }
}
1.39 +141 -41 xml-xalan/java/src/org/apache/xml/serializer/ToStream.java
Index: ToStream.java
===================================================================
RCS file: /home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToStream.java,v
retrieving revision 1.38
retrieving revision 1.39
diff -u -r1.38 -r1.39
--- ToStream.java 11 Feb 2005 06:18:13 -0000 1.38
+++ ToStream.java 21 Feb 2005 18:34:56 -0000 1.39
@@ -163,6 +163,13 @@
* remembers if we are in between the startCDATA() and endCDATA() callbacks
*/
protected boolean m_cdataStartCalled = false;
+
+ /**
+ * If this flag is true DTD entity references are not left as-is,
+ * which is exiting older behavior.
+ */
+ private boolean m_expandDTDEntities = true;
+
/**
* Default constructor
@@ -309,18 +316,7 @@
try
{
final java.io.Writer writer = m_writer;
- if (m_needToOutputDocTypeDecl)
- {
- outputDocTypeDecl(m_elemContext.m_elementName, false);
- m_needToOutputDocTypeDecl = false;
- }
- if (m_inDoctype)
- {
- writer.write(" [");
- writer.write(m_lineSep, 0, m_lineSepLen);
-
- m_inDoctype = false;
- }
+ DTDprolog();
writer.write("<!ELEMENT ");
writer.write(name);
@@ -357,20 +353,7 @@
return;
try
{
- if (m_needToOutputDocTypeDecl)
- {
- outputDocTypeDecl(m_elemContext.m_elementName, false);
- m_needToOutputDocTypeDecl = false;
- }
- if (m_inDoctype)
- {
- final java.io.Writer writer = m_writer;
- writer.write(" [");
- writer.write(m_lineSep, 0, m_lineSepLen);
-
- m_inDoctype = false;
- }
-
+ DTDprolog();
outputEntityDecl(name, value);
}
catch (IOException e)
@@ -799,18 +782,7 @@
try
{
final java.io.Writer writer = m_writer;
- if (m_needToOutputDocTypeDecl)
- {
- outputDocTypeDecl(m_elemContext.m_elementName, false);
- m_needToOutputDocTypeDecl = false;
- }
- if (m_inDoctype)
- {
- writer.write(" [");
- writer.write(m_lineSep, 0, m_lineSepLen);
-
- m_inDoctype = false;
- }
+ DTDprolog();
writer.write("<!ATTLIST ");
writer.write(eName);
@@ -867,6 +839,27 @@
String systemId)
throws SAXException
{
+ try {
+ DTDprolog();
+
+ m_writer.write("<!ENTITY ");
+ m_writer.write(name);
+ if (publicId != null) {
+ m_writer.write(" PUBLIC \"");
+ m_writer.write(publicId);
+
+ }
+ else {
+ m_writer.write(" SYSTEM \"");
+ m_writer.write(systemId);
+ }
+ m_writer.write("\" >");
+ m_writer.write(m_lineSep, 0, m_lineSepLen);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+
}
/**
@@ -1368,6 +1361,8 @@
public void characters(final char chars[], final int start, final int length)
throws org.xml.sax.SAXException
{
+ if (m_inEntityRef && !m_expandDTDEntities)
+ return;
if (m_elemContext.m_startTagOpen)
{
closeStartTag();
@@ -1564,6 +1559,8 @@
*/
public void characters(String s) throws org.xml.sax.SAXException
{
+ if (m_inEntityRef && !m_expandDTDEntities)
+ return;
final int length = s.length();
if (length > m_charsBuff.length)
{
@@ -2327,6 +2324,17 @@
{
if (name.equals("[dtd]"))
m_inExternalDTD = true;
+
+ if (!m_expandDTDEntities && !m_inExternalDTD) {
+ /* Only leave the entity as-is if
+ * we've been told not to expand them
+ * and this is not the magic [dtd] name.
+ */
+ startNonEscaping();
+ characters("&" + name + ';');
+ endNonEscaping();
+ }
+
m_inEntityRef = true;
}
@@ -2983,7 +2991,8 @@
this.m_triedToGetConverter = false;
this.m_lineSepUse = true;
// DON'T SET THE WRITER TO NULL, IT MAY BE REUSED !!
- // this.m_writer = null;
+ // this.m_writer = null;
+ this.m_expandDTDEntities = true;
}
@@ -3176,4 +3185,95 @@
m_values = newVector;
}
}
-}
+
+ // Implement DTDHandler
+ /**
+ * If this method is called, the serializer is used as a
+ * DTDHandler, which changes behavior how the serializer
+ * handles document entities.
+ * @see org.xml.sax.DTDHandler#notationDecl(java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void notationDecl(String name, String pubID, String sysID) throws SAXException {
+ // TODO Auto-generated method stub
+ try {
+ DTDprolog();
+
+ m_writer.write("<!NOTATION ");
+ m_writer.write(name);
+ if (pubID != null) {
+ m_writer.write(" PUBLIC \"");
+ m_writer.write(pubID);
+
+ }
+ else {
+ m_writer.write(" SYSTEM \"");
+ m_writer.write(sysID);
+ }
+ m_writer.write("\" >");
+ m_writer.write(m_lineSep, 0, m_lineSepLen);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * If this method is called, the serializer is used as a
+ * DTDHandler, which changes behavior how the serializer
+ * handles document entities.
+ * @see org.xml.sax.DTDHandler#unparsedEntityDecl(java.lang.String, java.lang.String, java.lang.String, java.lang.String)
+ */
+ public void unparsedEntityDecl(String name, String pubID, String sysID, String notationName) throws SAXException {
+ // TODO Auto-generated method stub
+ try {
+ DTDprolog();
+
+ m_writer.write("<!ENTITY ");
+ m_writer.write(name);
+ if (pubID != null) {
+ m_writer.write(" PUBLIC \"");
+ m_writer.write(pubID);
+
+ }
+ else {
+ m_writer.write(" SYSTEM \"");
+ m_writer.write(sysID);
+ }
+ m_writer.write("\" NDATA ");
+ m_writer.write(notationName);
+ m_writer.write(" >");
+ m_writer.write(m_lineSep, 0, m_lineSepLen);
+ } catch (IOException e) {
+ // TODO Auto-generated catch block
+ e.printStackTrace();
+ }
+ }
+
+ /**
+ * A private helper method to output the
+ * @throws SAXException
+ * @throws IOException
+ */
+ private void DTDprolog() throws SAXException, IOException {
+ final java.io.Writer writer = m_writer;
+ if (m_needToOutputDocTypeDecl)
+ {
+ outputDocTypeDecl(m_elemContext.m_elementName, false);
+ m_needToOutputDocTypeDecl = false;
+ }
+ if (m_inDoctype)
+ {
+ writer.write(" [");
+ writer.write(m_lineSep, 0, m_lineSepLen);
+ m_inDoctype = false;
+ }
+ }
+
+ /**
+ * If set to false the serializer does not expand DTD entities,
+ * but leaves them as is, the default value is true;
+ */
+ public void setDTDEntityExpansion(boolean expand) {
+ m_expandDTDEntities = expand;
+ }
+}
\ No newline at end of file
---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org