minchau 2003/06/23 13:49:50
Modified: java/src/org/apache/xml/serializer ToUnknownStream.java
EmptySerializer.java CharInfo.java
ToHTMLStream.java ExtendedContentHandler.java
ToXMLStream.java ToSAXHandler.java
ToTextStream.java
Log:
Added addUniqueAttribute() method for optimization purposes.
The serializer may write out this attribute straight away with little
processing becuase the caller guarantees
that the value has these qualities:
1) Value is unique, i.e. will not be changed by other addAttribute() calls.
2) Value is ASCII
3) Value does not contain quot (34) amp (38) lt (60) gt (62)
Submitted by: Brian Minchau
Reviewed by: Morris Kwan
Revision Changes Path
1.4 +42 -30
xml-xalan/java/src/org/apache/xml/serializer/ToUnknownStream.java
Index: ToUnknownStream.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToUnknownStream.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ToUnknownStream.java 18 Jun 2003 14:59:45 -0000 1.3
+++ ToUnknownStream.java 23 Jun 2003 20:49:50 -0000 1.4
@@ -325,6 +325,20 @@
}
/**
+ * Adds a unique attribute to the currenly open tag
+ */
+ public void addUniqueAttribute(String rawName, String value, int flags)
+ throws SAXException
+ {
+ if (m_firstTagNotEmitted)
+ {
+ flush();
+ }
+ m_handler.addUniqueAttribute(rawName, value, flags);
+
+ }
+
+ /**
* Converts the String to a character array and calls the SAX method
* characters(char[],int,int);
*
@@ -504,7 +518,7 @@
if (m_tracer != null)
firePseudoElement(elementName);
-
+
/* we don't want to call our own addAttributes, which
* merely delegates to the wrapped handler, but we want to
* add these attributes to m_attributes. So me must call
super.
@@ -1263,14 +1277,14 @@
}
public void setTransformer(Transformer t)
- {
+ {
m_handler.setTransformer(t);
- if ((t instanceof SerializerTrace) &&
- (((SerializerTrace) t).hasTraceListeners())) {
- m_tracer = (SerializerTrace) t;
- } else {
- m_tracer = null;
- }
+ if ((t instanceof SerializerTrace) &&
+ (((SerializerTrace) t).hasTraceListeners())) {
+ m_tracer = (SerializerTrace) t;
+ } else {
+ m_tracer = null;
+ }
}
public Transformer getTransformer()
{
@@ -1296,26 +1310,24 @@
m_handler.setSourceLocator(locator);
}
- protected void firePseudoElement(String elementName)
- {
-
- if (m_tracer != null) {
- StringBuffer sb = new StringBuffer();
-
- sb.append('<');
- sb.append(elementName);
-
- // convert the StringBuffer to a char array and
- // emit the trace event that these characters "might"
- // be written
- char ch[] = sb.toString().toCharArray();
- m_tracer.fireGenerateEvent(
-
SerializerTrace.EVENTTYPE_OUTPUT_PSEUDO_CHARACTERS,
- ch,
- 0,
- ch.length);
- }
- }
-
-
+ protected void firePseudoElement(String elementName)
+ {
+
+ if (m_tracer != null) {
+ StringBuffer sb = new StringBuffer();
+
+ sb.append('<');
+ sb.append(elementName);
+
+ // convert the StringBuffer to a char array and
+ // emit the trace event that these characters "might"
+ // be written
+ char ch[] = sb.toString().toCharArray();
+ m_tracer.fireGenerateEvent(
+ SerializerTrace.EVENTTYPE_OUTPUT_PSEUDO_CHARACTERS,
+ ch,
+ 0,
+ ch.length);
+ }
+ }
}
1.2 +10 -0
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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- EmptySerializer.java 1 Apr 2003 19:24:54 -0000 1.1
+++ EmptySerializer.java 23 Jun 2003 20:49:50 -0000 1.2
@@ -263,6 +263,7 @@
{
throwUnimplementedException();
}
+
/**
* @see
org.apache.xml.serializer.ExtendedContentHandler#characters(java.lang.String)
*/
@@ -694,6 +695,7 @@
public void setNamespaceMappings(NamespaceMappings mappings) {
throwUnimplementedException();
}
+
/**
* @see
org.apache.xml.serializer.ExtendedContentHandler#setSourceLocator(javax.xml.transform.SourceLocator)
*/
@@ -702,4 +704,12 @@
throwUnimplementedException();
}
+ /**
+ * @see
org.apache.xml.serializer.ExtendedContentHandler#addUniqueAttribute(java.lang.String,
java.lang.String, int)
+ */
+ public void addUniqueAttribute(String name, String value, int flags)
+ throws SAXException
+ {
+ throwUnimplementedException();
+ }
}
1.5 +52 -12
xml-xalan/java/src/org/apache/xml/serializer/CharInfo.java
Index: CharInfo.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/CharInfo.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- CharInfo.java 9 Jun 2003 06:26:57 -0000 1.4
+++ CharInfo.java 23 Jun 2003 20:49:50 -0000 1.5
@@ -104,6 +104,13 @@
/** The carriage return character, which the parser should always
normalize. */
public static char S_CARRIAGERETURN = 0x0D;
+ /** This flag is an optimization for HTML entities. It false if entities
+ * other than quot (34), amp (38), lt (60) and gt (62) are defined
+ * in the range 0 to 127.
+ */
+
+ public final boolean onlyQuotAmpLtGt;
+
/** Copy the first 0,1 ... ASCII_MAX values into an array */
private static final int ASCII_MAX = 128;
@@ -146,14 +153,6 @@
*/
private int firstWordNotUsed;
- /**
- * This constructor is private, just to force the use
- * of the getCharInfo(entitiesResource) factory
- *
- */
- private CharInfo()
- {
- }
/**
* Constructor that reads in a resource file that describes the mapping
of
@@ -186,6 +185,8 @@
String value;
int code;
String line;
+
+ boolean noExtraEntities = true;
// Make various attempts to interpret the parameter as a properties
// file or resource file, as follows:
@@ -202,6 +203,8 @@
value = entities.getString(name);
code = Integer.parseInt(value);
defineEntity(name, (char) code);
+ if (extraEntity(code))
+ noExtraEntities = false;
}
set(S_LINEFEED);
set(S_CARRIAGERETURN);
@@ -293,6 +296,8 @@
code = Integer.parseInt(value);
defineEntity(name, (char) code);
+ if (extraEntity(code))
+ noExtraEntities = false;
}
}
@@ -318,6 +323,7 @@
}
}
}
+ onlyQuotAmpLtGt = noExtraEntities;
// initialize the array with a cache of the BitSet values
for (int i=0; i<ASCII_MAX; i++)
@@ -336,6 +342,7 @@
isCleanASCII[ch] = false;
}
+
/**
* Defines a new character reference. The reference's name and value are
* supplied. Nothing happens if the character reference is already
defined.
@@ -515,9 +522,6 @@
private static Hashtable m_getCharInfo_cache = new Hashtable();
}
-
-
-
/**
* Returns the array element holding the bit value for the
@@ -590,5 +594,41 @@
) != 0; // 0L for 64 bit words
return in_the_set;
}
-
+
+ /**
+ * This is private to force the use of the factory method:
+ * getCharInfo(String) instead.
+ */
+ private CharInfo()
+ {
+ // this constructor should never be called, the next line
+ // is only here to make the compiler happy.
+ onlyQuotAmpLtGt = true;
+ }
+
+ // record if there are any entities other than
+ // quot, amp, lt, gt (probably user defined)
+ /**
+ * @return true if the entity
+ * @param code The value of the character that has an entity defined
+ * for it.
+ */
+ private boolean extraEntity(int entityValue)
+ {
+ boolean extra = false;
+ if (entityValue < 128)
+ {
+ switch (entityValue)
+ {
+ case 34 : // quot
+ case 38 : // amp
+ case 60 : // lt
+ case 62 : // gt
+ break;
+ default : // other entity in range 0 to 127
+ extra = true;
+ }
+ }
+ return extra;
+ }
}
1.17 +56 -0
xml-xalan/java/src/org/apache/xml/serializer/ToHTMLStream.java
Index: ToHTMLStream.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToHTMLStream.java,v
retrieving revision 1.16
retrieving revision 1.17
diff -u -r1.16 -r1.17
--- ToHTMLStream.java 23 Jun 2003 06:26:51 -0000 1.16
+++ ToHTMLStream.java 23 Jun 2003 20:49:50 -0000 1.17
@@ -1828,6 +1828,62 @@
// The internal DTD subset is not serialized by the ToHTMLStream
serializer
}
+ /**
+ * This method is used to add an attribute to the currently open
element.
+ * The caller has guaranted that this attribute is unique, which means
that it
+ * not been seen before and will not be seen again.
+ *
+ * @param name the qualified name of the attribute
+ * @param value the value of the attribute which can contain only
+ * ASCII printable characters characters in the range 32 to 127
inclusive.
+ * @flags the bit values of this integer give optimization information.
+ */
+ public void addUniqueAttribute(String name, String value, int flags)
+ throws SAXException
+ {
+ try
+ {
+ final java.io.Writer writer = m_writer;
+ if ((flags & NO_BAD_CHARS) > 0 && m_htmlcharInfo.onlyQuotAmpLtGt)
+ {
+ // "flags" has indicated that the characters
+ // '>' '<' '&' and '"' are not in the value and
+ // m_htmlcharInfo has recorded that there are no other
+ // entities in the range 0 to 127 so we write out the
+ // value directly
+ writer.write(' ');
+ writer.write(name);
+ writer.write("=\"");
+ writer.write(value);
+ writer.write('"');
+ }
+ else if (
+ (flags & HTML_ATTREMPTY) > 0
+ && (value.length() == 0 || value.equalsIgnoreCase(name)))
+ {
+ writer.write(' ');
+ writer.write(name);
+ }
+ else
+ {
+ writer.write(' ');
+ writer.write(name);
+ writer.write("=\"");
+ if ((flags & HTML_ATTRURL) > 0)
+ {
+ writeAttrURI(writer, value, m_specialEscapeURLs);
+ }
+ else
+ {
+ writeAttrString(writer, value, this.getEncoding());
+ }
+ writer.write('"');
+ }
+ } catch (IOException e) {
+ throw new SAXException(e);
+ }
+ }
+
public void comment(char ch[], int start, int length)
throws SAXException
{
1.2 +26 -0
xml-xalan/java/src/org/apache/xml/serializer/ExtendedContentHandler.java
Index: ExtendedContentHandler.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ExtendedContentHandler.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ExtendedContentHandler.java 1 Apr 2003 19:24:54 -0000 1.1
+++ ExtendedContentHandler.java 23 Jun 2003 20:49:50 -0000 1.2
@@ -111,6 +111,7 @@
* @param value
*/
public void addAttribute(String qName, String value);
+
/**
* This method is used to notify of a character event, but passing the
data
* as a character String rather than the standard character array.
@@ -240,4 +241,29 @@
*/
public void setSourceLocator(SourceLocator locator);
+ // Bit constants for addUniqueAttribute().
+
+ // The attribute value contains no bad characters. A "bad" character is
one which
+ // is greater than 126 or it is one of '<', '>', '&' or '"'.
+ public static final int NO_BAD_CHARS = 0x1;
+
+ // An HTML empty attribute (e.g. <OPTION selected>).
+ public static final int HTML_ATTREMPTY = 0x2;
+
+ // An HTML URL attribute
+ public static final int HTML_ATTRURL = 0x4;
+
+ /**
+ * Add a unique attribute to the current element.
+ * The attribute is guaranteed to be unique here. The serializer can
write
+ * it out immediately without saving it in a table first. The integer
+ * flag contains information about the attribute, which helps the
serializer
+ * to decide whether a particular processing is needed.
+ *
+ * @param qName the fully qualified attribute name.
+ * @param value the attribute value
+ * @param flags a bitwise flag
+ */
+ public void addUniqueAttribute(String qName, String value, int flags)
+ throws SAXException;
}
1.7 +47 -0
xml-xalan/java/src/org/apache/xml/serializer/ToXMLStream.java
Index: ToXMLStream.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToXMLStream.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- ToXMLStream.java 23 Jun 2003 06:26:52 -0000 1.6
+++ ToXMLStream.java 23 Jun 2003 20:49:50 -0000 1.7
@@ -385,6 +385,53 @@
super.fireEntityReference(name);
}
+ /**
+ * This method is used to add an attribute to the currently open
element.
+ * The caller has guaranted that this attribute is unique, which means
that it
+ * not been seen before and will not be seen again.
+ *
+ * @param name the qualified name of the attribute
+ * @param value the value of the attribute which can contain only
+ * ASCII printable characters characters in the range 32 to 127
inclusive.
+ * @flags the bit values of this integer give optimization information.
+ */
+ public void addUniqueAttribute(String name, String value, int flags)
+ throws SAXException
+ {
+ if (m_elemContext.m_startTagOpen)
+ {
+
+ try
+ {
+ final String patchedName = patchName(name);
+ final java.io.Writer writer = m_writer;
+ if ((flags & NO_BAD_CHARS) > 0 &&
m_xmlcharInfo.onlyQuotAmpLtGt)
+ {
+ // "flags" has indicated that the characters
+ // '>' '<' '&' and '"' are not in the value and
+ // m_htmlcharInfo has recorded that there are no other
+ // entities in the range 32 to 127 so we write out the
+ // value directly
+
+ writer.write(' ');
+ writer.write(patchedName);
+ writer.write("=\"");
+ writer.write(value);
+ writer.write('"');
+ }
+ else
+ {
+ writer.write(' ');
+ writer.write(patchedName);
+ writer.write("=\"");
+ writeAttrString(writer, value, this.getEncoding());
+ writer.write('"');
+ }
+ } catch (IOException e) {
+ throw new SAXException(e);
+ }
+ }
+ }
public void addAttribute(
String uri,
1.5 +14 -13
xml-xalan/java/src/org/apache/xml/serializer/ToSAXHandler.java
Index: ToSAXHandler.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToSAXHandler.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- ToSAXHandler.java 23 Jun 2003 06:26:51 -0000 1.4
+++ ToSAXHandler.java 23 Jun 2003 20:49:50 -0000 1.5
@@ -69,14 +69,6 @@
import org.xml.sax.SAXParseException;
import org.xml.sax.ext.LexicalHandler;
-/**
- * @author minchau
- *
- * To change this generated comment edit the template variable "typecomment":
- * Window>Preferences>Java>Templates.
- * To enable and disable the creation of type comments go to
- * Window>Preferences>Java>Code Generation.
- */
abstract public class ToSAXHandler extends SerializerBase
{
public ToSAXHandler()
@@ -237,7 +229,7 @@
m_state.resetState(getTransformer());
}
- // fire off the start element event
+ // fire off the start element event
if (m_tracer != null)
super.fireStartElem(arg2);
}
@@ -321,7 +313,7 @@
public void setTransformState(TransformStateSetter ts) {
this.m_state = ts;
}
-
+
/**
* Receives notification that an element starts, but attributes are not
* fully known yet.
@@ -339,9 +331,9 @@
m_state.resetState(getTransformer());
}
- // fire off the start element event
+ // fire off the start element event
if (m_tracer != null)
- super.fireStartElem(qName);
+ super.fireStartElem(qName);
}
/**
@@ -355,7 +347,7 @@
if (m_state != null) {
m_state.resetState(getTransformer());
}
- // fire off the start element event
+ // fire off the start element event
if (m_tracer != null)
super.fireStartElem(qName);
}
@@ -443,4 +435,13 @@
this.m_saxHandler = null;
this.m_state = null;
}
+
+ /**
+ * Add a unique attribute
+ */
+ public void addUniqueAttribute(String qName, String value, int flags)
+ throws SAXException
+ {
+ addAttribute(qName, value);
+ }
}
1.8 +16 -7
xml-xalan/java/src/org/apache/xml/serializer/ToTextStream.java
Index: ToTextStream.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xml/serializer/ToTextStream.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- ToTextStream.java 23 Jun 2003 06:26:51 -0000 1.7
+++ ToTextStream.java 23 Jun 2003 20:49:50 -0000 1.8
@@ -196,7 +196,7 @@
throws org.xml.sax.SAXException
{
if (m_tracer != null)
- super.fireEndElem(name);
+ super.fireEndElem(name);
}
/**
@@ -234,8 +234,8 @@
try
{
writeNormalizedChars(ch, start, length, false);
- if (m_tracer != null)
- super.fireCharEvent(ch, start, length);
+ if (m_tracer != null)
+ super.fireCharEvent(ch, start, length);
}
catch(IOException ioe)
{
@@ -411,7 +411,7 @@
try
{
writeNormalizedChars(ch, start, length, false);
- if (m_tracer != null)
+ if (m_tracer != null)
super.fireCDATAEvent(ch, start, length);
}
catch(IOException ioe)
@@ -563,7 +563,7 @@
*/
public void endElement(String elemName) throws SAXException
{
- if (m_tracer != null)
+ if (m_tracer != null)
super.fireEndElem(elemName);
}
@@ -577,8 +577,8 @@
throws SAXException
{
if (m_needToCallStartDocument)
- startDocumentInternal();
- // time to fire off startlement event.
+ startDocumentInternal();
+ // time to fire off startlement event.
if (m_tracer != null) {
super.fireStartElem(elementName);
this.firePseudoAttributes();
@@ -604,6 +604,15 @@
public void addAttribute(String name, String value)
{
// do nothing, forget about the attribute
+ }
+
+ /**
+ * Add a unique attribute
+ */
+ public void addUniqueAttribute(String qName, String value, int flags)
+ throws SAXException
+ {
+ // do nothing, forget about the attribute
}
public boolean startPrefixMapping(
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]