This commit is contained in:
Lana Steuck 2014-04-01 17:27:49 -07:00
commit 592997da4f
7 changed files with 923 additions and 946 deletions

@ -3,11 +3,12 @@
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 2001, 2002,2004 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.
* You may obtain a copy of the License at
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@ -20,6 +21,7 @@
package com.sun.org.apache.xerces.internal.dom;
import java.util.ArrayList;
import java.util.Vector;
import org.w3c.dom.DOMStringList;
@ -35,47 +37,54 @@ import org.w3c.dom.DOMStringList;
*/
public class DOMStringListImpl implements DOMStringList {
//A collection of DOMString values
private Vector fStrings;
// A collection of DOMString values
private final ArrayList fStrings;
/**
* Construct an empty list of DOMStringListImpl
*/
public DOMStringListImpl() {
fStrings = new Vector();
fStrings = new ArrayList();
}
/**
* Construct an empty list of DOMStringListImpl
* Construct a DOMStringListImpl from an ArrayList
*/
public DOMStringListImpl(Vector params) {
public DOMStringListImpl(ArrayList params) {
fStrings = params;
}
/**
* @see org.w3c.dom.DOMStringList#item(int)
*/
public String item(int index) {
try {
return (String) fStrings.elementAt(index);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
/**
* Construct a DOMStringListImpl from a Vector
*/
public DOMStringListImpl(Vector params) {
fStrings = new ArrayList(params);
}
/**
* @see org.w3c.dom.DOMStringList#getLength()
*/
public int getLength() {
return fStrings.size();
/**
* @see org.w3c.dom.DOMStringList#item(int)
*/
public String item(int index) {
final int length = getLength();
if (index >= 0 && index < length) {
return (String) fStrings.get(index);
}
return null;
}
/**
* @see org.w3c.dom.DOMStringList#contains(String)
*/
public boolean contains(String param) {
return fStrings.contains(param) ;
}
/**
* @see org.w3c.dom.DOMStringList#getLength()
*/
public int getLength() {
return fStrings.size();
}
/**
* @see org.w3c.dom.DOMStringList#contains(String)
*/
public boolean contains(String param) {
return fStrings.contains(param);
}
/**
* DOM Internal:

@ -1,13 +1,14 @@
/*
* Copyright (c) 2003, 2013, Oracle and/or its affiliates. All rights reserved.
* reserved comment block
* DO NOT REMOVE OR ALTER!
*/
/*
* Copyright 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.
* You may obtain a copy of the License at
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@ -52,7 +53,7 @@ import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Stack;
import javax.xml.XMLConstants;
import java.util.StringTokenizer;
/**
@ -1810,7 +1811,7 @@ public class XMLEntityManager implements XMLComponent, XMLEntityResolver {
userDir = userDir.replace(separator, '/');
int len = userDir.length(), ch;
StringBuffer buffer = new StringBuffer(len*3);
StringBuilder buffer = new StringBuilder(len*3);
// change C:/blah to /C:/blah
if (len >= 2 && userDir.charAt(1) == ':') {
ch = Character.toUpperCase(userDir.charAt(0));
@ -1880,6 +1881,61 @@ public class XMLEntityManager implements XMLComponent, XMLEntityResolver {
return gUserDirURI;
}
public static OutputStream createOutputStream(String uri) throws IOException {
// URI was specified. Handle relative URIs.
final String expanded = XMLEntityManager.expandSystemId(uri, null, true);
final URL url = new URL(expanded != null ? expanded : uri);
OutputStream out = null;
String protocol = url.getProtocol();
String host = url.getHost();
// Use FileOutputStream if this URI is for a local file.
if (protocol.equals("file")
&& (host == null || host.length() == 0 || host.equals("localhost"))) {
File file = new File(getPathWithoutEscapes(url.getPath()));
if (!file.exists()) {
File parent = file.getParentFile();
if (parent != null && !parent.exists()) {
parent.mkdirs();
}
}
out = new FileOutputStream(file);
}
// Try to write to some other kind of URI. Some protocols
// won't support this, though HTTP should work.
else {
URLConnection urlCon = url.openConnection();
urlCon.setDoInput(false);
urlCon.setDoOutput(true);
urlCon.setUseCaches(false); // Enable tunneling.
if (urlCon instanceof HttpURLConnection) {
// The DOM L3 REC says if we are writing to an HTTP URI
// it is to be done with an HTTP PUT.
HttpURLConnection httpCon = (HttpURLConnection) urlCon;
httpCon.setRequestMethod("PUT");
}
out = urlCon.getOutputStream();
}
return out;
}
private static String getPathWithoutEscapes(String origPath) {
if (origPath != null && origPath.length() != 0 && origPath.indexOf('%') != -1) {
// Locate the escape characters
StringTokenizer tokenizer = new StringTokenizer(origPath, "%");
StringBuilder result = new StringBuilder(origPath.length());
int size = tokenizer.countTokens();
result.append(tokenizer.nextToken());
for(int i = 1; i < size; ++i) {
String token = tokenizer.nextToken();
// Decode the 2 digit hexadecimal number following % in '%nn'
result.append((char)Integer.valueOf(token.substring(0, 2), 16).intValue());
result.append(token.substring(2));
}
return result.toString();
}
return origPath;
}
/**
* Absolutizes a URI using the current value
* of the "user.dir" property as the base URI. If

@ -3,11 +3,12 @@
* DO NOT REMOVE OR ALTER!
*/
/*
* 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.
* You may obtain a copy of the License at
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@ -351,7 +352,7 @@ final class RangeToken extends Token implements java.io.Serializable {
// src2: o----o
// src2: o----o
// src2: o------------o
if (src2begin <= src2begin && src1end <= src2end) {
if (src2begin <= src1begin && src1end <= src2end) {
// src1: o--------o
// src2: o------------o
// res: o--------o
@ -384,6 +385,7 @@ final class RangeToken extends Token implements java.io.Serializable {
result[wp++] = src2begin;
result[wp++] = src2end;
this.ranges[src1] = src2end+1;
src2 += 2;
}
} else if (src2end < src1begin) {
// Not overlapped
@ -399,10 +401,6 @@ final class RangeToken extends Token implements java.io.Serializable {
+"]");
}
}
while (src1 < this.ranges.length) {
result[wp++] = this.ranges[src1++];
result[wp++] = this.ranges[src1++];
}
this.ranges = new int[wp];
System.arraycopy(result, 0, this.ranges, 0, wp);
// this.ranges is sorted and compacted.
@ -464,8 +462,8 @@ final class RangeToken extends Token implements java.io.Serializable {
if (ch > 0xffff)
lowers.addRange(ch, ch);
else {
char uch = Character.toUpperCase((char)ch);
lowers.addRange(uch, uch);
char lch = Character.toLowerCase((char)ch);
lowers.addRange(lch, lch);
}
}
}
@ -479,8 +477,10 @@ final class RangeToken extends Token implements java.io.Serializable {
void dumpRanges() {
System.err.print("RANGE: ");
if (this.ranges == null)
if (this.ranges == null) {
System.err.println(" NULL");
return;
}
for (int i = 0; i < this.ranges.length; i += 2) {
System.err.print("["+this.ranges[i]+","+this.ranges[i+1]+"] ");
}
@ -552,10 +552,10 @@ final class RangeToken extends Token implements java.io.Serializable {
else if (this == Token.token_spaces)
ret = "\\s";
else {
StringBuffer sb = new StringBuffer();
sb.append("[");
StringBuilder sb = new StringBuilder();
sb.append('[');
for (int i = 0; i < this.ranges.length; i += 2) {
if ((options & RegularExpression.SPECIAL_COMMA) != 0 && i > 0) sb.append(",");
if ((options & RegularExpression.SPECIAL_COMMA) != 0 && i > 0) sb.append(',');
if (this.ranges[i] == this.ranges[i+1]) {
sb.append(escapeCharInCharClass(this.ranges[i]));
} else {
@ -564,7 +564,7 @@ final class RangeToken extends Token implements java.io.Serializable {
sb.append(escapeCharInCharClass(this.ranges[i+1]));
}
}
sb.append("]");
sb.append(']');
ret = sb.toString();
}
} else {
@ -578,7 +578,7 @@ final class RangeToken extends Token implements java.io.Serializable {
StringBuffer sb = new StringBuffer();
sb.append("[^");
for (int i = 0; i < this.ranges.length; i += 2) {
if ((options & RegularExpression.SPECIAL_COMMA) != 0 && i > 0) sb.append(",");
if ((options & RegularExpression.SPECIAL_COMMA) != 0 && i > 0) sb.append(',');
if (this.ranges[i] == this.ranges[i+1]) {
sb.append(escapeCharInCharClass(this.ranges[i]));
} else {
@ -587,7 +587,7 @@ final class RangeToken extends Token implements java.io.Serializable {
sb.append(escapeCharInCharClass(this.ranges[i+1]));
}
}
sb.append("]");
sb.append(']');
ret = sb.toString();
}
}

@ -3,11 +3,12 @@
* DO NOT REMOVE OR ALTER!
*/
/*
* 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.
* You may obtain a copy of the License at
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@ -18,7 +19,6 @@
* limitations under the License.
*/
// Sep 14, 2000:
// Fixed comments to preserve whitespaces and add a line break
// when indenting. Reported by Gervase Markham <gerv@gerv.net>
@ -57,17 +57,13 @@ import com.sun.org.apache.xerces.internal.dom.DOMErrorImpl;
import com.sun.org.apache.xerces.internal.dom.DOMLocatorImpl;
import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
import com.sun.org.apache.xerces.internal.util.XMLChar;
import org.w3c.dom.DOMImplementation;
import org.w3c.dom.DOMError;
import org.w3c.dom.DOMErrorHandler;
import org.w3c.dom.Document;
import org.w3c.dom.DocumentFragment;
import org.w3c.dom.DocumentType;
import org.w3c.dom.DOMError;
import org.w3c.dom.DOMErrorHandler;
import org.w3c.dom.Element;
import org.w3c.dom.Entity;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Notation;
import org.w3c.dom.ls.LSException;
import org.w3c.dom.ls.LSSerializerFilter;
import org.w3c.dom.traversal.NodeFilter;
@ -126,7 +122,7 @@ import org.xml.sax.ext.LexicalHandler;
* @author Elena Litani, IBM
* @author Sunitha Reddy, Sun Microsystems
* @see Serializer
* @see LSSerializer
* @see org.w3c.dom.ls.LSSerializer
*/
public abstract class BaseMarkupSerializer
implements ContentHandler, DocumentHandler, LexicalHandler,
@ -337,6 +333,9 @@ public abstract class BaseMarkupSerializer
return true;
}
protected void cleanup() {
fCurrentNode = null;
}
protected void prepare()
throws IOException
@ -409,6 +408,7 @@ public abstract class BaseMarkupSerializer
reset();
prepare();
serializeNode( elem );
cleanup();
_printer.flush();
if ( _printer.getException() != null )
throw _printer.getException();
@ -438,7 +438,7 @@ public abstract class BaseMarkupSerializer
* writer and output format. Throws an exception only if
* an I/O exception occured while serializing.
*
* @param elem The element to serialize
* @param frag The document fragment to serialize
* @throws IOException An I/O exception occured while
* serializing
*/
@ -448,6 +448,7 @@ public abstract class BaseMarkupSerializer
reset();
prepare();
serializeNode( frag );
cleanup();
_printer.flush();
if ( _printer.getException() != null )
throw _printer.getException();
@ -470,6 +471,7 @@ public abstract class BaseMarkupSerializer
prepare();
serializeNode( doc );
serializePreRoot();
cleanup();
_printer.flush();
if ( _printer.getException() != null )
throw _printer.getException();
@ -530,22 +532,22 @@ public abstract class BaseMarkupSerializer
if (!XMLChar.isValid(ch)) {
// check if it is surrogate
if (++index < end) {
surrogates(ch, chars[index]);
surrogates(ch, chars[index],true);
}
else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
} else {
if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||
ch == '\n' || ch == '\r' || ch == '\t' ) {
_printer.printText((char)ch);
} else {
// The character is not printable -- split CDATA section
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(ch));
_printer.printText(";<![CDATA[");
}
}
if ( ( ch >= ' ' && _encodingInfo.isPrintable(ch) && ch != 0x7F ) ||
ch == '\n' || ch == '\r' || ch == '\t' ) {
_printer.printText(ch);
}
else {
// The character is not printable -- split CDATA section
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(ch));
_printer.printText(";<![CDATA[");
}
}
_printer.setNextIndent( saveIndent );
@ -1195,11 +1197,6 @@ public abstract class BaseMarkupSerializer
}
case Node.DOCUMENT_NODE : {
DocumentType docType;
DOMImplementation domImpl;
NamedNodeMap map;
Entity entity;
Notation notation;
int i;
serializeDocument();
@ -1208,7 +1205,6 @@ public abstract class BaseMarkupSerializer
docType = ( (Document) node ).getDoctype();
if (docType != null) {
// DOM Level 2 (or higher)
domImpl = ( (Document) node ).getImplementation();
try {
String internal;
@ -1372,7 +1368,7 @@ public abstract class BaseMarkupSerializer
* state with <tt>empty</tt> and <tt>afterElement</tt> set to false.
*
* @return The current element state
* @throws IOException An I/O exception occured while
* @throws IOException An I/O exception occurred while
* serializing
*/
protected ElementState content()
@ -1415,7 +1411,6 @@ public abstract class BaseMarkupSerializer
* whether the text is printed as CDATA or unescaped.
*
* @param text The text to print
* @param unescaped True is should print unescaped
* @throws IOException An I/O exception occured while
* serializing
*/
@ -1430,9 +1425,6 @@ public abstract class BaseMarkupSerializer
// state) or whether we are inside a CDATA section or entity.
if ( state.inCData || state.doCData ) {
int index;
int saveIndent;
// Print a CDATA section. The text is not escaped, but ']]>'
// appearing in the code must be identified and dealt with.
// The contents of a text node is considered space preserving.
@ -1440,7 +1432,7 @@ public abstract class BaseMarkupSerializer
_printer.printText("<![CDATA[");
state.inCData = true;
}
saveIndent = _printer.getNextIndent();
int saveIndent = _printer.getNextIndent();
_printer.setNextIndent( 0 );
printCDATAText( text);
_printer.setNextIndent( saveIndent );
@ -1543,12 +1535,10 @@ public abstract class BaseMarkupSerializer
fDOMErrorHandler.handleError(fDOMError);
throw new LSException(LSException.SERIALIZE_ERR, msg);
}
else {
// issue error
modifyDOMError(msg, DOMError.SEVERITY_ERROR, "cdata-section-not-splitted", fCurrentNode);
if (!fDOMErrorHandler.handleError(fDOMError)) {
throw new LSException(LSException.SERIALIZE_ERR, msg);
}
// issue error
modifyDOMError(msg, DOMError.SEVERITY_ERROR, "cdata-section-not-splitted", fCurrentNode);
if (!fDOMErrorHandler.handleError(fDOMError)) {
throw new LSException(LSException.SERIALIZE_ERR, msg);
}
} else {
// issue warning
@ -1573,29 +1563,29 @@ public abstract class BaseMarkupSerializer
if (!XMLChar.isValid(ch)) {
// check if it is surrogate
if (++index <length) {
surrogates(ch, text.charAt(index));
surrogates(ch, text.charAt(index),true);
}
else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
} else {
if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||
ch == '\n' || ch == '\r' || ch == '\t' ) {
_printer.printText((char)ch);
} else {
}
if ( ( ch >= ' ' && _encodingInfo.isPrintable(ch) && ch != 0x7F ) ||
ch == '\n' || ch == '\r' || ch == '\t' ) {
_printer.printText(ch);
}
else {
// The character is not printable -- split CDATA section
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(ch));
_printer.printText(";<![CDATA[");
}
// The character is not printable -- split CDATA section
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(ch));
_printer.printText(";<![CDATA[");
}
}
}
protected void surrogates(int high, int low) throws IOException{
protected void surrogates(int high, int low, boolean inContent) throws IOException{
if (XMLChar.isHighSurrogate(high)) {
if (!XMLChar.isLowSurrogate(low)) {
//Invalid XML
@ -1608,7 +1598,7 @@ public abstract class BaseMarkupSerializer
fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
}
else {
if (content().inCData ) {
if (inContent && content().inCData) {
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(supplemental));
_printer.printText(";<![CDATA[");
@ -1633,7 +1623,9 @@ public abstract class BaseMarkupSerializer
* Multiple spaces are printed as such, but spaces at beginning
* of line are removed.
*
* @param text The text to print
* @param chars The text to print
* @param start The start offset
* @param length The number of characters
* @param preserveSpace Space preserving flag
* @param unescaped Print unescaped
*/
@ -1641,8 +1633,6 @@ public abstract class BaseMarkupSerializer
boolean preserveSpace, boolean unescaped )
throws IOException
{
int index;
char ch;
if ( preserveSpace ) {
// Preserving spaces: the text must print exactly as it is,
@ -1650,12 +1640,14 @@ public abstract class BaseMarkupSerializer
// consolidating spaces. If a line terminator is used, a line
// break will occur.
while ( length-- > 0 ) {
ch = chars[ start ];
char ch = chars[ start ];
++start;
if ( ch == '\n' || ch == '\r' || unescaped )
if ( ch == '\n' || ch == '\r' || unescaped ) {
_printer.printText( ch );
else
}
else {
printEscaped( ch );
}
}
} else {
// Not preserving spaces: print one part at a time, and
@ -1664,14 +1656,17 @@ public abstract class BaseMarkupSerializer
// by printing mechanism. Line terminator is treated
// no different than other text part.
while ( length-- > 0 ) {
ch = chars[ start ];
char ch = chars[ start ];
++start;
if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' ) {
_printer.printSpace();
else if ( unescaped )
}
else if ( unescaped ) {
_printer.printText( ch );
else
}
else {
printEscaped( ch );
}
}
}
}
@ -1703,12 +1698,15 @@ public abstract class BaseMarkupSerializer
// no different than other text part.
for ( index = 0 ; index < text.length() ; ++index ) {
ch = text.charAt( index );
if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' )
if ( ch == ' ' || ch == '\f' || ch == '\t' || ch == '\n' || ch == '\r' ) {
_printer.printSpace();
else if ( unescaped )
}
else if ( unescaped ) {
_printer.printText( ch );
else
}
else {
printEscaped( ch );
}
}
}
}
@ -1751,7 +1749,7 @@ public abstract class BaseMarkupSerializer
_printer.printText( '&' );
_printer.printText( charRef );
_printer.printText( ';' );
} else if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0xF7 ) ||
} else if ( ( ch >= ' ' && _encodingInfo.isPrintable((char)ch) && ch != 0x7F ) ||
ch == '\n' || ch == '\r' || ch == '\t' ) {
// Non printables are below ASCII space but not tab or line
// terminator, ASCII delete, or above a certain Unicode threshold.
@ -1872,14 +1870,13 @@ public abstract class BaseMarkupSerializer
{
if ( _elementStateCount > 0 ) {
/*Corrected by David Blondeau (blondeau@intalio.com)*/
_prefixes = null;
//_prefixes = _elementStates[ _elementStateCount ].prefixes;
_prefixes = null;
//_prefixes = _elementStates[ _elementStateCount ].prefixes;
-- _elementStateCount;
return _elementStates[ _elementStateCount ];
} else {
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "Internal", null);
throw new IllegalStateException(msg);
}
String msg = DOMMessageFormatter.formatMessage(DOMMessageFormatter.SERIALIZER_DOMAIN, "Internal", null);
throw new IllegalStateException(msg);
}
@ -1890,11 +1887,14 @@ public abstract class BaseMarkupSerializer
*
* @return True if in the state of the document
*/
protected boolean isDocumentState()
{
protected boolean isDocumentState() {
return _elementStateCount == 0;
}
/** Clears document state. **/
final void clearDocumentState() {
_elementStateCount = 0;
}
/**
* Returns the namespace prefix for the specified URI.
@ -1913,15 +1913,14 @@ public abstract class BaseMarkupSerializer
if ( prefix != null )
return prefix;
}
if ( _elementStateCount == 0 )
if ( _elementStateCount == 0 ) {
return null;
else {
for ( int i = _elementStateCount ; i > 0 ; --i ) {
if ( _elementStates[ i ].prefixes != null ) {
prefix = (String) _elementStates[ i ].prefixes.get( namespaceURI );
if ( prefix != null )
return prefix;
}
}
for ( int i = _elementStateCount ; i > 0 ; --i ) {
if ( _elementStates[ i ].prefixes != null ) {
prefix = (String) _elementStates[ i ].prefixes.get( namespaceURI );
if ( prefix != null )
return prefix;
}
}
return null;

@ -3,11 +3,12 @@
* DO NOT REMOVE OR ALTER!
*/
/*
* 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.
* You may obtain a copy of the License at
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@ -18,8 +19,6 @@
* limitations under the License.
*/
// Sep 14, 2000:
// Fixed problem with namespace handling. Contributed by
// David Blondeau <blondeau@intalio.com>
@ -33,22 +32,20 @@
// Aug 21, 2000:
// Added ability to omit DOCTYPE declaration.
package com.sun.org.apache.xml.internal.serialize;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
import com.sun.org.apache.xerces.internal.impl.Constants;
import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
import com.sun.org.apache.xerces.internal.util.SymbolTable;
import com.sun.org.apache.xerces.internal.util.XML11Char;
import com.sun.org.apache.xerces.internal.util.XMLChar;
import org.xml.sax.SAXException;
import org.w3c.dom.DOMError;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
/**
* Implements an XML serializer supporting both DOM and SAX pretty
@ -62,9 +59,9 @@ import org.w3c.dom.DOMError;
* The serializer supports both DOM and SAX. SAX serializing is done by firing
* SAX events and using the serializer as a document handler. DOM serializing is done
* by calling {@link #serialize(Document)} or by using DOM Level 3
* {@link org.w3c.dom.ls.DOMSerializer} and
* serializing with {@link org.w3c.dom.ls.DOMSerializer#write},
* {@link org.w3c.dom.ls.DOMSerializer#writeToString}.
* {@link org.w3c.dom.ls.LSSerializer} and
* serializing with {@link org.w3c.dom.ls.LSSerializer#write},
* {@link org.w3c.dom.ls.LSSerializer#writeToString}.
* <p>
* If an I/O exception occurs while serializing, the serializer
* will not throw an exception directly, but only throw it
@ -122,10 +119,6 @@ extends XMLSerializer {
*/
protected boolean fNamespaces = false;
private boolean fPreserveSpace;
/**
* Constructs a new serializer. The serializer cannot be used without
* calling {@link #setOutputCharStream} or {@link #setOutputByteStream}
@ -217,26 +210,27 @@ extends XMLSerializer {
if (!XML11Char.isXML11Valid(ch)) {
// check if it is surrogate
if (++index < end) {
surrogates(ch, chars[index]);
surrogates(ch, chars[index], true);
}
else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
} else {
if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) {
_printer.printText((char)ch);
} else {
// The character is not printable -- split CDATA section
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(ch));
_printer.printText(";<![CDATA[");
}
}
if ( _encodingInfo.isPrintable(ch) && XML11Char.isXML11ValidLiteral(ch)) {
_printer.printText(ch);
}
else {
// The character is not printable -- split CDATA section
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(ch));
_printer.printText(";<![CDATA[");
}
}
_printer.setNextIndent( saveIndent );
} else {
}
else {
int saveIndent;
@ -249,16 +243,17 @@ extends XMLSerializer {
_printer.setNextIndent( 0 );
printText( chars, start, length, true, state.unescaped );
_printer.setNextIndent( saveIndent );
} else {
}
else {
printText( chars, start, length, false, state.unescaped );
}
}
} catch ( IOException except ) {
}
catch ( IOException except ) {
throw new SAXException( except );
}
}
//
// overwrite printing functions to make sure serializer prints out valid XML
//
@ -268,25 +263,31 @@ extends XMLSerializer {
int ch = source.charAt(i);
if (!XML11Char.isXML11Valid(ch)) {
if (++i <length) {
surrogates(ch, source.charAt(i));
} else {
surrogates(ch, source.charAt(i), false);
}
else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
}
continue;
}
if (ch == '\n' || ch == '\r' || ch == '\t' || ch == 0x0085 || ch == 0x2028){
printHex(ch);
} else if (ch == '<') {
_printer.printText("&lt;");
} else if (ch == '&') {
_printer.printText("&amp;");
} else if (ch == '"') {
_printer.printText("&quot;");
} else if ((ch >= ' ' && _encodingInfo.isPrintable((char) ch))) {
_printer.printText((char) ch);
} else {
printHex(ch);
}
if (ch == '\n' || ch == '\r' || ch == '\t' || ch == 0x0085 || ch == 0x2028) {
printHex(ch);
}
else if (ch == '<') {
_printer.printText("&lt;");
}
else if (ch == '&') {
_printer.printText("&amp;");
}
else if (ch == '"') {
_printer.printText("&quot;");
}
else if ((ch >= ' ' && _encodingInfo.isPrintable((char) ch))) {
_printer.printText((char) ch);
}
else {
printHex(ch);
}
}
}
@ -344,54 +345,55 @@ extends XMLSerializer {
if (!XML11Char.isXML11Valid(ch)) {
// check if it is surrogate
if (++index < length) {
surrogates(ch, text.charAt(index));
} else {
fatalError(
"The character '"
+ (char) ch
+ "' is an invalid XML character");
surrogates(ch, text.charAt(index), true);
}
else {
fatalError("The character '" + ch + "' is an invalid XML character");
}
continue;
} else {
if (_encodingInfo.isPrintable((char) ch)
&& XML11Char.isXML11ValidLiteral(ch)) {
_printer.printText((char) ch);
} else {
// The character is not printable -- split CDATA section
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(ch));
_printer.printText(";<![CDATA[");
}
}
if (_encodingInfo.isPrintable(ch)
&& XML11Char.isXML11ValidLiteral(ch)) {
_printer.printText(ch);
}
else {
// The character is not printable -- split CDATA section
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(ch));
_printer.printText(";<![CDATA[");
}
}
}
// note that this "int" should, in all cases, be a char.
// REVISIT: make it a char...
protected final void printXMLChar( int ch ) throws IOException {
if (ch == '\r' || ch == 0x0085 || ch == 0x2028) {
printHex(ch);
} else if ( ch == '<') {
printHex(ch);
}
else if ( ch == '<') {
_printer.printText("&lt;");
} else if (ch == '&') {
}
else if (ch == '&') {
_printer.printText("&amp;");
} else if (ch == '>'){
// character sequence "]]>" can't appear in content, therefore
// we should escape '>'
_printer.printText("&gt;");
} else if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) {
}
else if (ch == '>'){
// character sequence "]]>" can't appear in content, therefore
// we should escape '>'
_printer.printText("&gt;");
}
else if ( _encodingInfo.isPrintable((char)ch) && XML11Char.isXML11ValidLiteral(ch)) {
_printer.printText((char)ch);
} else {
printHex(ch);
}
else {
printHex(ch);
}
}
protected final void surrogates(int high, int low) throws IOException{
protected final void surrogates(int high, int low, boolean inContent) throws IOException{
if (XMLChar.isHighSurrogate(high)) {
if (!XMLChar.isLowSurrogate(low)) {
//Invalid XML
@ -404,7 +406,7 @@ extends XMLSerializer {
fatalError("The character '"+(char)supplemental+"' is an invalid XML character");
}
else {
if (content().inCData ) {
if (inContent && content().inCData) {
_printer.printText("]]>&#x");
_printer.printText(Integer.toHexString(supplemental));
_printer.printText(";<![CDATA[");
@ -414,7 +416,8 @@ extends XMLSerializer {
}
}
}
} else {
}
else {
fatalError("The character '"+(char)high+"' is an invalid XML character");
}
@ -436,18 +439,21 @@ extends XMLSerializer {
if (!XML11Char.isXML11Valid(ch)) {
// check if it is surrogate
if (++index <length) {
surrogates(ch, text.charAt(index));
surrogates(ch, text.charAt(index), true);
} else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
}
if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) {
_printer.printText( ch );
} else
}
else {
printXMLChar( ch );
}
}
} else {
}
else {
// Not preserving spaces: print one part at a time, and
// use spaces between parts to break them into different
// lines. Spaces at beginning of line will be stripped
@ -458,27 +464,25 @@ extends XMLSerializer {
if (!XML11Char.isXML11Valid(ch)) {
// check if it is surrogate
if (++index <length) {
surrogates(ch, text.charAt(index));
surrogates(ch, text.charAt(index), true);
} else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
}
if ( unescaped && XML11Char.isXML11ValidLiteral(ch) )
if ( unescaped && XML11Char.isXML11ValidLiteral(ch) ) {
_printer.printText( ch );
else
printXMLChar( ch);
}
else {
printXMLChar( ch );
}
}
}
}
protected void printText( char[] chars, int start, int length,
boolean preserveSpace, boolean unescaped ) throws IOException {
int index;
char ch;
if ( preserveSpace ) {
// Preserving spaces: the text must print exactly as it is,
@ -486,52 +490,55 @@ extends XMLSerializer {
// consolidating spaces. If a line terminator is used, a line
// break will occur.
while ( length-- > 0 ) {
ch = chars[start++];
char ch = chars[start++];
if (!XML11Char.isXML11Valid(ch)) {
// check if it is surrogate
if ( length-- > 0) {
surrogates(ch, chars[start++]);
surrogates(ch, chars[start++], true);
} else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
}
if ( unescaped && XML11Char.isXML11ValidLiteral(ch))
if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) {
_printer.printText( ch );
else
}
else {
printXMLChar( ch );
}
}
} else {
}
else {
// Not preserving spaces: print one part at a time, and
// use spaces between parts to break them into different
// lines. Spaces at beginning of line will be stripped
// by printing mechanism. Line terminator is treated
// no different than other text part.
while ( length-- > 0 ) {
ch = chars[start++];
char ch = chars[start++];
if (!XML11Char.isXML11Valid(ch)) {
// check if it is surrogate
if ( length-- > 0) {
surrogates(ch, chars[start++]);
surrogates(ch, chars[start++], true);
} else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
}
if ( unescaped && XML11Char.isXML11ValidLiteral(ch))
if ( unescaped && XML11Char.isXML11ValidLiteral(ch)) {
_printer.printText( ch );
else
}
else {
printXMLChar( ch );
}
}
}
}
public boolean reset() {
super.reset();
return true;
}
}

@ -3,11 +3,12 @@
* DO NOT REMOVE OR ALTER!
*/
/*
* 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.
* You may obtain a copy of the License at
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
@ -18,8 +19,6 @@
* limitations under the License.
*/
// Sep 14, 2000:
// Fixed problem with namespace handling. Contributed by
// David Blondeau <blondeau@intalio.com>
@ -33,14 +32,13 @@
// Aug 21, 2000:
// Added ability to omit DOCTYPE declaration.
package com.sun.org.apache.xml.internal.serialize;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;
import com.sun.org.apache.xerces.internal.dom.DOMMessageFormatter;
import com.sun.org.apache.xerces.internal.util.NamespaceSupport;
@ -50,6 +48,7 @@ import com.sun.org.apache.xerces.internal.util.XMLSymbols;
import com.sun.org.apache.xerces.internal.xni.NamespaceContext;
import org.w3c.dom.Attr;
import org.w3c.dom.DOMError;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
@ -71,9 +70,9 @@ import org.xml.sax.helpers.AttributesImpl;
* The serializer supports both DOM and SAX. SAX serializing is done by firing
* SAX events and using the serializer as a document handler. DOM serializing is done
* by calling {@link #serialize(Document)} or by using DOM Level 3
* {@link org.w3c.dom.ls.DOMSerializer} and
* serializing with {@link org.w3c.dom.ls.DOMSerializer#write},
* {@link org.w3c.dom.ls.DOMSerializer#writeToString}.
* {@link org.w3c.dom.ls.LSSerializer} and
* serializing with {@link org.w3c.dom.ls.LSSerializer#write},
* {@link org.w3c.dom.ls.LSSerializer#writeToString}.
* <p>
* If an I/O exception occurs while serializing, the serializer
* will not throw an exception directly, but only throw it
@ -195,7 +194,7 @@ extends BaseMarkupSerializer {
/**
* This methods turns on namespace fixup algorithm during
* DOM serialization.
* @see org.w3c.dom.ls.DOMSerializer
* @see org.w3c.dom.ls.LSSerializer
*
* @param namespaces
*/
@ -222,7 +221,6 @@ extends BaseMarkupSerializer {
ElementState state;
String name;
String value;
boolean addNSAttr = false;
if (DEBUG) {
System.out.println("==>startElement("+namespaceURI+","+localName+
@ -277,13 +275,16 @@ extends BaseMarkupSerializer {
if (namespaceURI != null && ! namespaceURI.equals( "" )) {
String prefix;
prefix = getPrefix( namespaceURI );
if (prefix != null && prefix.length() > 0)
if (prefix != null && prefix.length() > 0) {
rawName = prefix + ":" + localName;
else
}
else {
rawName = localName;
} else
}
}
else {
rawName = localName;
addNSAttr = true;
}
}
_printer.printText( '<' );
@ -334,18 +335,18 @@ extends BaseMarkupSerializer {
}
if (_prefixes != null) {
Enumeration keys;
keys = _prefixes.keys();
while (keys.hasMoreElements()) {
Iterator entries = _prefixes.entrySet().iterator();
while (entries.hasNext()) {
_printer.printSpace();
value = (String) keys.nextElement();
name = (String) _prefixes.get( value );
Map.Entry entry = (Map.Entry) entries.next();
value = (String) entry.getKey();
name = (String) entry.getValue();
if (name.length() == 0) {
_printer.printText( "xmlns=\"" );
printEscaped( value );
_printer.printText( '"' );
} else {
}
else {
_printer.printText( "xmlns:" );
_printer.printText( name );
_printer.printText( "=\"" );
@ -770,13 +771,11 @@ extends BaseMarkupSerializer {
// xmlns:foo = ""
}
continue;
} else { // xmlns
// empty prefix is always bound ("" or some string)
value = fSymbolTable.addSymbol(value);
fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, value);
continue;
}
// xmlns --- empty prefix is always bound ("" or some string)
value = fSymbolTable.addSymbol(value);
fNSBinder.declarePrefix(XMLSymbols.EMPTY_STRING, value);
continue;
} // end-else: valid declaration
} // end-if: namespace declaration
} // end-for
@ -958,22 +957,20 @@ extends BaseMarkupSerializer {
// xmlns:foo = ""
}
continue;
} else { // xmlns
// empty prefix is always bound ("" or some string)
uri = fNSBinder.getURI(XMLSymbols.EMPTY_STRING);
localUri=fLocalNSBinder.getURI(XMLSymbols.EMPTY_STRING);
value = fSymbolTable.addSymbol(value);
if (localUri == null ){
// declaration was not printed while fixing element namespace binding
if (fNamespacePrefixes) {
printNamespaceAttr(XMLSymbols.EMPTY_STRING, value);
}
// case 4 does not apply here since attributes can't use
// default namespace
}
continue;
}
// xmlns --- empty prefix is always bound ("" or some string)
uri = fNSBinder.getURI(XMLSymbols.EMPTY_STRING);
localUri= fLocalNSBinder.getURI(XMLSymbols.EMPTY_STRING);
value = fSymbolTable.addSymbol(value);
if (localUri == null ) {
// declaration was not printed while fixing element namespace binding
if (fNamespacePrefixes) {
printNamespaceAttr(XMLSymbols.EMPTY_STRING, value);
}
// case 4 does not apply here since attributes can't use
// default namespace
}
continue;
}
uri = fSymbolTable.addSymbol(uri);
@ -1195,8 +1192,6 @@ extends BaseMarkupSerializer {
AttributesImpl attrsOnly;
String rawName;
int i;
int indexColon;
String prefix;
int length;
if (attrs == null) {
@ -1233,7 +1228,7 @@ extends BaseMarkupSerializer {
int ch = source.charAt(i);
if (!XMLChar.isValid(ch)) {
if (++i < length) {
surrogates(ch, source.charAt(i));
surrogates(ch, source.charAt(i), false);
} else {
fatalError("The character '" + (char) ch + "' is an invalid XML character");
}
@ -1291,16 +1286,17 @@ extends BaseMarkupSerializer {
if (!XMLChar.isValid(ch)) {
// check if it is surrogate
if (++index <length) {
surrogates(ch, text.charAt(index));
surrogates(ch, text.charAt(index), true);
} else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
}
if ( unescaped ) {
_printer.printText( ch );
} else
} else {
printXMLChar( ch );
}
}
} else {
// Not preserving spaces: print one part at a time, and
@ -1313,17 +1309,18 @@ extends BaseMarkupSerializer {
if (!XMLChar.isValid(ch)) {
// check if it is surrogate
if (++index <length) {
surrogates(ch, text.charAt(index));
surrogates(ch, text.charAt(index), true);
} else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
}
if ( unescaped )
if ( unescaped ) {
_printer.printText( ch );
else
printXMLChar( ch);
} else {
printXMLChar( ch );
}
}
}
}
@ -1332,8 +1329,6 @@ extends BaseMarkupSerializer {
protected void printText( char[] chars, int start, int length,
boolean preserveSpace, boolean unescaped ) throws IOException {
int index;
char ch;
if ( preserveSpace ) {
// Preserving spaces: the text must print exactly as it is,
@ -1341,13 +1336,13 @@ extends BaseMarkupSerializer {
// consolidating spaces. If a line terminator is used, a line
// break will occur.
while ( length-- > 0 ) {
ch = chars[start++];
char ch = chars[start++];
if (!XMLChar.isValid(ch)) {
// check if it is surrogate
if ( length-- > 0 ) {
surrogates(ch, chars[start++]);
surrogates(ch, chars[start++], true);
} else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
}
@ -1363,13 +1358,13 @@ extends BaseMarkupSerializer {
// by printing mechanism. Line terminator is treated
// no different than other text part.
while ( length-- > 0 ) {
ch = chars[start++];
char ch = chars[start++];
if (!XMLChar.isValid(ch)) {
// check if it is surrogate
if ( length-- > 0 ) {
surrogates(ch, chars[start++]);
surrogates(ch, chars[start++], true);
} else {
fatalError("The character '"+(char)ch+"' is an invalid XML character");
fatalError("The character '"+ch+"' is an invalid XML character");
}
continue;
}