santiagopg 02/05/29 13:00:45
Modified: java/src/org/apache/xalan/xsltc/runtime/output
StreamHTMLOutput.java StreamOutput.java
StreamXMLOutput.java
Log:
Bug fixes after testing.
Revision Changes Path
1.5 +64 -43
xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamHTMLOutput.java
Index: StreamHTMLOutput.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamHTMLOutput.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -r1.4 -r1.5
--- StreamHTMLOutput.java 28 May 2002 20:57:13 -0000 1.4
+++ StreamHTMLOutput.java 29 May 2002 20:00:45 -0000 1.5
@@ -1,5 +1,5 @@
/*
- * @(#)$Id: StreamHTMLOutput.java,v 1.4 2002/05/28 20:57:13 santiagopg Exp $
+ * @(#)$Id: StreamHTMLOutput.java,v 1.5 2002/05/29 20:00:45 santiagopg Exp $
*
* The Apache Software License, Version 1.1
*
@@ -63,6 +63,8 @@
package org.apache.xalan.xsltc.runtime.output;
+import java.util.Vector;
+
import java.io.Writer;
import java.io.IOException;
import java.io.OutputStream;
@@ -120,10 +122,6 @@
}
public void endDocument() throws TransletException {
- if (_startTagOpen) {
- _buffer.append("/>");
- }
-
// Finally, output buffer to writer
outputBuffer();
}
@@ -164,30 +162,44 @@
}
}
- public void endElement(String elementName) throws TransletException {
- if (_startTagOpen) {
- closeStartTag();
+ public void endElement(String elementName)
+ throws TransletException
+ {
+ if (_inStyleScript &&
+ (elementName.equalsIgnoreCase("style") ||
+ elementName.equalsIgnoreCase("script")))
+ {
+ _inStyleScript = false;
}
- if (_indent) {
- _indentLevel --;
- if (_indentNextEndTag) {
- indent(_indentNextEndTag);
+ if (_startTagOpen) {
+ appendAttributes();
+ if (_emptyElements.containsKey(elementName.toLowerCase())) {
+ _buffer.append('>');
+ }
+ else {
+ _buffer.append("></").append(elementName).append('>');
+ }
+ _startTagOpen = false;
+
+ if (_indent) {
+ _indentLevel--;
_indentNextEndTag = true;
}
}
+ else {
+ if (_indent) {
+ _indentLevel--;
- // Empty elements may not have closing tags
- if (!_emptyElements.containsKey(elementName.toLowerCase())) {
+ if (_indentNextEndTag) {
+ indent(_indentNextEndTag);
+ _indentNextEndTag = true;
+ _lineFeedNextStartTag = true;
+ }
+ }
_buffer.append("</").append(elementName).append('>');
_indentNextEndTag = true;
}
- else if (_inStyleScript &&
- (elementName.equalsIgnoreCase("style") ||
- elementName.equalsIgnoreCase("script")))
- {
- _inStyleScript = false;
- }
}
public void characters(String characters)
@@ -220,20 +232,29 @@
}
}
- public void attribute(String attributeName, String attributeValue)
+ public void attribute(String name, String value)
throws TransletException
{
+// System.out.println("attribute = " + name + " " + value);
if (_startTagOpen) {
- _buffer.append(' ').append(attributeName).append("=\"");
+ int k;
+ Attribute attr;
- if (attributeName.equalsIgnoreCase(HREF_STR) ||
- attributeName.equalsIgnoreCase(SRC_STR) ||
- attributeName.equals(CITE_STR))
+ if (name.equalsIgnoreCase(HREF_STR) ||
+ name.equalsIgnoreCase(SRC_STR) ||
+ name.equals(CITE_STR))
{
- appendEncodedURL(attributeValue).append('"');
+ attr = new Attribute(name, encodeURL(value));
+ }
+ else {
+ attr = new Attribute(name, escapeNonURL(value));
+ }
+
+ if ((k = _attributes.indexOf(attr)) >= 0) {
+ _attributes.setElementAt(attr, k);
}
else {
- appendNonURL(attributeValue).append('"');
+ _attributes.add(attr);
}
}
}
@@ -295,26 +316,28 @@
/**
* Replaces whitespaces in a URL with '%20'
*/
- private StringBuffer appendEncodedURL(String base) {
+ private String encodeURL(String base) {
final int length = base.length();
+ final StringBuffer result = new StringBuffer();
for (int i = 0; i < length; i++) {
final char ch = base.charAt(i);
if (ch == ' ') {
- _buffer.append("%20");
+ result.append("%20");
}
else {
- _buffer.append(ch);
+ result.append(ch);
}
}
- return _buffer;
+ return result.toString();
}
/**
* Escape non ASCII characters (> u007F) as &#XXX; entities.
*/
- private StringBuffer appendNonURL(String base) {
+ private String escapeNonURL(String base) {
final int length = base.length();
+ final StringBuffer result = new StringBuffer();
for (int i = 0; i < length; i++){
final char ch = base.charAt(i);
@@ -322,15 +345,15 @@
if ((ch >= '\u007F' && ch < '\u00A0') ||
(_is8859Encoded && ch > '\u00FF'))
{
- _buffer.append(CHAR_ESC_START)
- .append(Integer.toString((int) ch))
- .append(';');
+ result.append(CHAR_ESC_START)
+ .append(Integer.toString((int) ch))
+ .append(';');
}
else {
- _buffer.append(ch);
+ result.append(ch);
}
}
- return _buffer;
+ return result.toString();
}
/**
@@ -338,13 +361,12 @@
*/
private void appendHeader() {
_buffer.append("<meta http-equiv=\"Content-Type\" content=\"")
- .append(_mediaType).append(" charset=\"")
- .append(_encoding).append("/>");
+ .append(_mediaType).append("; charset=")
+ .append(_encoding).append("\">");
}
- private void closeStartTag() {
- _buffer.append('>');
- _startTagOpen = false;
+ protected void closeStartTag() {
+ super.closeStartTag();
// Insert <META> tag directly after <HEAD> element in HTML output
if (_headTagOpen) {
@@ -352,5 +374,4 @@
_headTagOpen = false;
}
}
-
}
1.7 +50 -1
xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamOutput.java
Index: StreamOutput.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamOutput.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- StreamOutput.java 23 May 2002 18:13:04 -0000 1.6
+++ StreamOutput.java 29 May 2002 20:00:45 -0000 1.7
@@ -1,5 +1,5 @@
/*
- * @(#)$Id: StreamOutput.java,v 1.6 2002/05/23 18:13:04 santiagopg Exp $
+ * @(#)$Id: StreamOutput.java,v 1.7 2002/05/29 20:00:45 santiagopg Exp $
*
* The Apache Software License, Version 1.1
*
@@ -69,6 +69,8 @@
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
+import java.util.Vector;
+
class StreamOutput extends OutputBase {
protected static final String AMP = "&";
@@ -110,6 +112,31 @@
protected String _doctypeSystem = null;
protected String _doctypePublic = null;
+ // protected HashSet _attributes = new HashSet();
+ protected Vector _attributes = new Vector();
+
+ static class Attribute {
+ public String name, value;
+
+ Attribute(String name, String value) {
+ this.name = name;
+ this.value = value;
+ }
+
+ public int hashCode() {
+ return name.hashCode();
+ }
+
+ public boolean equals(Object obj) {
+ try {
+ return name.equalsIgnoreCase(((Attribute) obj).name);
+ }
+ catch (ClassCastException e) {
+ return false;
+ }
+ }
+ }
+
protected StreamOutput(StreamOutput output) {
_writer = output._writer;
_encoding = output._encoding;
@@ -256,5 +283,27 @@
if (offset < limit) {
_buffer.append(ch, offset, limit - offset);
}
+ }
+
+ protected void appendAttributes() {
+ // Append attributes to output buffer
+ if (!_attributes.isEmpty()) {
+ int i = 0;
+ final int length = _attributes.size();
+
+ do {
+ final Attribute attr = (Attribute) _attributes.elementAt(i);
+ _buffer.append(' ').append(attr.name).append("=\"")
+ .append(attr.value).append('"');
+ } while (++i < length);
+
+ _attributes.clear();
+ }
+ }
+
+ protected void closeStartTag() {
+ appendAttributes();
+ _buffer.append('>');
+ _startTagOpen = false;
}
}
1.6 +52 -59
xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamXMLOutput.java
Index: StreamXMLOutput.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/runtime/output/StreamXMLOutput.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- StreamXMLOutput.java 23 May 2002 20:59:38 -0000 1.5
+++ StreamXMLOutput.java 29 May 2002 20:00:45 -0000 1.6
@@ -1,5 +1,5 @@
/*
- * @(#)$Id: StreamXMLOutput.java,v 1.5 2002/05/23 20:59:38 santiagopg Exp $
+ * @(#)$Id: StreamXMLOutput.java,v 1.6 2002/05/29 20:00:45 santiagopg Exp $
*
* The Apache Software License, Version 1.1
*
@@ -64,8 +64,7 @@
package org.apache.xalan.xsltc.runtime.output;
import java.util.Stack;
-import java.util.HashSet;
-import java.util.Iterator;
+import java.util.Vector;
import java.io.Writer;
import java.io.IOException;
@@ -121,30 +120,6 @@
private boolean _cdataTagOpen = false;
- private HashSet _attributes = new HashSet();
-
- static class Attribute {
- public String name, value;
-
- Attribute(String name, String value) {
- this.name = name;
- this.value = value;
- }
-
- public int hashCode() {
- return name.hashCode();
- }
-
- public boolean equals(Object obj) {
- try {
- return name.equalsIgnoreCase(((Attribute) obj).name);
- }
- catch (ClassCastException e) {
- return false;
- }
- }
- }
-
public StreamXMLOutput(Writer writer, String encoding) {
super(writer, encoding);
init();
@@ -201,13 +176,6 @@
public void endDocument() throws TransletException {
// System.out.println("endDocument");
- if (_startTagOpen) {
- _buffer.append("/>");
- }
- else if (_cdataTagOpen) {
- closeCDATA();
- }
-
// Finally, output buffer to writer
outputBuffer();
}
@@ -215,7 +183,7 @@
public void startElement(String elementName) throws TransletException {
// System.out.println("startElement = " + elementName);
if (_startTagOpen) {
- _buffer.append('>');
+ closeStartTag();
}
else if (_cdataTagOpen) {
closeCDATA();
@@ -244,7 +212,6 @@
_depth++;
_startTagOpen = true;
- _attributes.clear();
}
public void endElement(String elementName) throws TransletException {
@@ -254,10 +221,14 @@
}
if (_startTagOpen) {
- _startTagOpen = false;
+ appendAttributes();
_buffer.append("/>");
- _indentLevel--;
- _indentNextEndTag = true;
+ _startTagOpen = false;
+
+ if (_indent) {
+ _indentLevel--;
+ _indentNextEndTag = true;
+ }
}
else {
if (_indent) {
@@ -270,6 +241,7 @@
}
}
_buffer.append("</").append(elementName).append('>');
+ _indentNextEndTag = true;
}
popNamespaces();
@@ -279,8 +251,7 @@
public void characters(String characters) throws TransletException {
// System.out.println("characters() '" + characters + "'");
if (_startTagOpen) {
- _buffer.append('>');
- _startTagOpen = false;
+ closeStartTag();
}
final Integer I = (Integer) _cdataStack.peek();
@@ -305,29 +276,33 @@
public void characters(char[] characters, int offset, int length)
throws TransletException
{
- if (_startTagOpen) {
- _buffer.append('>');
- _startTagOpen = false;
- }
+ if (length > 0) {
+ if (_startTagOpen) {
+ closeStartTag();
+ }
- if (_escaping) {
- escapeCharacters(characters, offset, length);
- }
- else {
- _buffer.append(characters, offset, length);
+ if (_escaping) {
+ escapeCharacters(characters, offset, length);
+ }
+ else {
+ _buffer.append(characters, offset, length);
+ }
}
}
public void attribute(String name, String value)
throws TransletException
{
-// System.out.println("attribute = " + name);
+// System.out.println("attribute = " + name + " " + value);
if (_startTagOpen) {
- final Attribute attr = new Attribute(name, value);
+ int k;
+ final Attribute attr =
+ new Attribute(patchName(name), value);
- if (!_attributes.contains(attr)) {
- _buffer.append(' ').append(name).append("=\"")
- .append(value).append('"');
+ if ((k = _attributes.indexOf(attr)) >= 0) {
+ _attributes.setElementAt(attr, k);
+ }
+ else {
_attributes.add(attr);
}
}
@@ -335,8 +310,7 @@
public void comment(String comment) throws TransletException {
if (_startTagOpen) {
- _buffer.append('>');
- _startTagOpen = false;
+ closeStartTag();
}
else if (_cdataTagOpen) {
closeCDATA();
@@ -350,8 +324,7 @@
{
// System.out.println("PI target = " + target + " data = " + data);
if (_startTagOpen) {
- _buffer.append('>');
- _startTagOpen = false;
+ closeStartTag();
}
else if (_cdataTagOpen) {
closeCDATA();
@@ -507,5 +480,25 @@
if (offset < limit) {
_buffer.append(ch, offset, limit - offset);
}
+ }
+
+ /**
+ * TODO: This method is a HACK! Since XSLTC does not have access to the
+ * XML file, it sometimes generates a NS prefix of the form "ns?" for
+ * an attribute. If at runtime, when the qname of the attribute is
+ * known, another prefix is specified for the attribute, then we can get
+ * a qname of the form "ns?:otherprefix:name". This function patches the
+ * name by simply ignoring "otherprefix".
+ */
+ private static String patchName(String qname) {
+ final int lastColon = qname.lastIndexOf(':');
+ if (lastColon > 0) {
+ final int firstColon = qname.indexOf(':');
+ if (firstColon != lastColon) {
+ return qname.substring(0, firstColon) +
+ qname.substring(lastColon);
+ }
+ }
+ return qname;
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]