|
|
|
|
|
*/ |
|
/* |
|
* 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 |
|
* |
|
* Unless required by applicable law or agreed to in writing, software |
|
* distributed under the License is distributed on an "AS IS" BASIS, |
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
* See the License for the specific language governing permissions and |
|
* limitations under the License. |
|
*/ |
|
|
|
package com.sun.org.apache.xml.internal.serializer; |
|
|
|
import java.io.IOException; |
|
|
|
import javax.xml.transform.ErrorListener; |
|
import javax.xml.transform.Result; |
|
import javax.xml.transform.Transformer; |
|
import javax.xml.transform.TransformerException; |
|
|
|
import com.sun.org.apache.xml.internal.serializer.utils.MsgKey; |
|
import com.sun.org.apache.xml.internal.serializer.utils.Utils; |
|
import org.xml.sax.SAXException; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public final class ToXMLStream extends ToStream |
|
{ |
|
|
|
|
|
|
|
*/ |
|
boolean m_cdataTagOpen = false; |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private static CharInfo m_xmlcharInfo = |
|
|
|
CharInfo.getCharInfoInternal(CharInfo.XML_ENTITIES_RESOURCE, Method.XML); |
|
|
|
|
|
|
|
*/ |
|
public ToXMLStream() |
|
{ |
|
this(null); |
|
} |
|
|
|
|
|
|
|
*/ |
|
public ToXMLStream(ErrorListener l) |
|
{ |
|
super(l); |
|
m_charInfo = m_xmlcharInfo; |
|
|
|
initCDATA(); |
|
|
|
m_prefixMap = new NamespaceMappings(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void CopyFrom(ToXMLStream xmlListener) |
|
{ |
|
|
|
m_writer = xmlListener.m_writer; |
|
|
|
|
|
|
|
String encoding = xmlListener.getEncoding(); |
|
setEncoding(encoding); |
|
|
|
setOmitXMLDeclaration(xmlListener.getOmitXMLDeclaration()); |
|
|
|
m_ispreserveSpace = xmlListener.m_ispreserveSpace; |
|
m_preserveSpaces = xmlListener.m_preserveSpaces; |
|
m_childNodeNum = xmlListener.m_childNodeNum; |
|
m_childNodeNumStack = xmlListener.m_childNodeNumStack; |
|
m_charactersBuffer = xmlListener.m_charactersBuffer; |
|
m_inEntityRef = xmlListener.m_inEntityRef; |
|
m_isprevtext = xmlListener.m_isprevtext; |
|
m_doIndent = xmlListener.m_doIndent; |
|
setIndentAmount(xmlListener.getIndentAmount()); |
|
m_startNewLine = xmlListener.m_startNewLine; |
|
m_needToOutputDocTypeDecl = xmlListener.m_needToOutputDocTypeDecl; |
|
setDoctypeSystem(xmlListener.getDoctypeSystem()); |
|
setDoctypePublic(xmlListener.getDoctypePublic()); |
|
setStandalone(xmlListener.getStandalone()); |
|
setMediaType(xmlListener.getMediaType()); |
|
m_maxCharacter = xmlListener.m_maxCharacter; |
|
m_encodingInfo = xmlListener.m_encodingInfo; |
|
m_spaceBeforeClose = xmlListener.m_spaceBeforeClose; |
|
m_cdataStartCalled = xmlListener.m_cdataStartCalled; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void startDocumentInternal() throws org.xml.sax.SAXException |
|
{ |
|
|
|
if (m_needToCallStartDocument) |
|
{ |
|
super.startDocumentInternal(); |
|
m_needToCallStartDocument = false; |
|
|
|
if (isInEntityRef()) |
|
return; |
|
|
|
m_needToOutputDocTypeDecl = true; |
|
m_startNewLine = false; |
|
|
|
|
|
|
|
*/ |
|
if (getOmitXMLDeclaration() == false) |
|
{ |
|
String encoding = Encodings.getMimeEncoding(getEncoding()); |
|
String version = getVersion(); |
|
if (version == null) |
|
version = "1.0"; |
|
String standalone; |
|
|
|
if (m_standaloneWasSpecified) |
|
{ |
|
standalone = " standalone=\"" + getStandalone() + "\""; |
|
} |
|
else |
|
{ |
|
standalone = ""; |
|
} |
|
|
|
try |
|
{ |
|
final java.io.Writer writer = m_writer; |
|
writer.write("<?xml version=\""); |
|
writer.write(version); |
|
writer.write("\" encoding=\""); |
|
writer.write(encoding); |
|
writer.write('\"'); |
|
writer.write(standalone); |
|
writer.write("?>"); |
|
if (m_doIndent || m_isStandalone) { |
|
if (m_standaloneWasSpecified |
|
|| getDoctypePublic() != null |
|
|| getDoctypeSystem() != null |
|
|| m_isStandalone) { |
|
// We almost never put a newline after the XML |
|
// header because this XML could be used as |
|
// an extenal general parsed entity |
|
// and we don't know the context into which it |
|
// will be used in the future. Only when |
|
// standalone, or a doctype system or public is |
|
// specified are we free to insert a new line |
|
// after the header. Is it even worth bothering |
|
|
|
writer.write(m_lineSep, 0, m_lineSepLen); |
|
} |
|
} |
|
} |
|
catch(IOException e) |
|
{ |
|
throw new SAXException(e); |
|
} |
|
|
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void endDocument() throws org.xml.sax.SAXException |
|
{ |
|
if (m_doIndent) { |
|
flushCharactersBuffer(false); |
|
} |
|
flushPending(); |
|
if (m_doIndent && !m_isprevtext) |
|
{ |
|
try |
|
{ |
|
outputLineSep(); |
|
} |
|
catch(IOException e) |
|
{ |
|
throw new SAXException(e); |
|
} |
|
} |
|
|
|
flushWriter(); |
|
|
|
if (m_tracer != null) |
|
super.fireEndDoc(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void startPreserving() throws org.xml.sax.SAXException |
|
{ |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void endPreserving() throws org.xml.sax.SAXException |
|
{ |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void processingInstruction(String target, String data) |
|
throws org.xml.sax.SAXException |
|
{ |
|
if (isInEntityRef()) |
|
return; |
|
|
|
if (m_doIndent) { |
|
m_childNodeNum++; |
|
flushCharactersBuffer(false); |
|
} |
|
flushPending(); |
|
|
|
if (target.equals(Result.PI_DISABLE_OUTPUT_ESCAPING)) |
|
{ |
|
startNonEscaping(); |
|
} |
|
else if (target.equals(Result.PI_ENABLE_OUTPUT_ESCAPING)) |
|
{ |
|
endNonEscaping(); |
|
} |
|
else |
|
{ |
|
try |
|
{ |
|
if (m_elemContext.m_startTagOpen) |
|
{ |
|
closeStartTag(); |
|
m_elemContext.m_startTagOpen = false; |
|
} |
|
else if (m_needToCallStartDocument) |
|
startDocumentInternal(); |
|
|
|
if (shouldIndent()) |
|
indent(); |
|
|
|
final java.io.Writer writer = m_writer; |
|
writer.write("<?"); |
|
writer.write(target); |
|
|
|
if (data.length() > 0 |
|
&& !Character.isSpaceChar(data.charAt(0))) |
|
writer.write(' '); |
|
|
|
int indexOfQLT = data.indexOf("?>"); |
|
|
|
if (indexOfQLT >= 0) |
|
{ |
|
|
|
|
|
if (indexOfQLT > 0) |
|
{ |
|
writer.write(data.substring(0, indexOfQLT)); |
|
} |
|
|
|
writer.write("? >"); |
|
|
|
if ((indexOfQLT + 2) < data.length()) |
|
{ |
|
writer.write(data.substring(indexOfQLT + 2)); |
|
} |
|
} |
|
else |
|
{ |
|
writer.write(data); |
|
} |
|
|
|
writer.write('?'); |
|
writer.write('>'); |
|
|
|
|
|
|
|
|
|
*/ |
|
if (m_elemContext.m_currentElemDepth <= 0 && m_isStandalone) |
|
writer.write(m_lineSep, 0, m_lineSepLen); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
m_startNewLine = true; |
|
} |
|
catch(IOException e) |
|
{ |
|
throw new SAXException(e); |
|
} |
|
} |
|
|
|
if (m_tracer != null) |
|
super.fireEscapingEvent(target, data); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void entityReference(String name) throws org.xml.sax.SAXException |
|
{ |
|
if (m_elemContext.m_startTagOpen) |
|
{ |
|
closeStartTag(); |
|
m_elemContext.m_startTagOpen = false; |
|
} |
|
|
|
try |
|
{ |
|
if (shouldIndent()) |
|
indent(); |
|
|
|
final java.io.Writer writer = m_writer; |
|
writer.write('&'); |
|
writer.write(name); |
|
writer.write(';'); |
|
} |
|
catch(IOException e) |
|
{ |
|
throw new SAXException(e); |
|
} |
|
|
|
if (m_tracer != null) |
|
super.fireEntityReference(name); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
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, |
|
String localName, |
|
String rawName, |
|
String type, |
|
String value, |
|
boolean xslAttribute) |
|
throws SAXException |
|
{ |
|
if (m_elemContext.m_startTagOpen) |
|
{ |
|
boolean was_added = addAttributeAlways(uri, localName, rawName, type, value, xslAttribute); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
if (was_added && !xslAttribute && !rawName.startsWith("xmlns")) |
|
{ |
|
String prefixUsed = |
|
ensureAttributesNamespaceIsDeclared( |
|
uri, |
|
localName, |
|
rawName); |
|
if (prefixUsed != null |
|
&& rawName != null |
|
&& !rawName.startsWith(prefixUsed)) |
|
{ |
|
// use a different raw name, with the prefix used in the |
|
|
|
rawName = prefixUsed + ":" + localName; |
|
|
|
} |
|
} |
|
addAttributeAlways(uri, localName, rawName, type, value, xslAttribute); |
|
} |
|
else |
|
{ |
|
/* |
|
* The startTag is closed, yet we are adding an attribute? |
|
* |
|
* Section: 7.1.3 Creating Attributes Adding an attribute to an |
|
* element after a PI (for example) has been added to it is an |
|
* error. The attributes can be ignored. The spec doesn't explicitly |
|
* say this is disallowed, as it does for child elements, but it |
|
* makes sense to have the same treatment. |
|
* |
|
* We choose to ignore the attribute which is added too late. |
|
*/ |
|
// Generate a warning of the ignored attributes |
|
|
|
|
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_ILLEGAL_ATTRIBUTE_POSITION,new Object[]{ localName }); |
|
|
|
try { |
|
|
|
Transformer tran = super.getTransformer(); |
|
ErrorListener errHandler = tran.getErrorListener(); |
|
|
|
|
|
|
|
if (null != errHandler && m_sourceLocator != null) |
|
errHandler.warning(new TransformerException(msg, m_sourceLocator)); |
|
else |
|
System.out.println(msg); |
|
} |
|
catch (Exception e){} |
|
} |
|
} |
|
|
|
|
|
|
|
*/ |
|
public void endElement(String elemName) throws SAXException |
|
{ |
|
endElement(null, null, elemName); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public void namespaceAfterStartElement( |
|
final String prefix, |
|
final String uri) |
|
throws SAXException |
|
{ |
|
|
|
|
|
if (m_elemContext.m_elementURI == null) |
|
{ |
|
String prefix1 = getPrefixPart(m_elemContext.m_elementName); |
|
if (prefix1 == null && EMPTYSTRING.equals(prefix)) |
|
{ |
|
// the elements URI is not known yet, and it |
|
// doesn't have a prefix, and we are currently |
|
// setting the uri for prefix "", so we have |
|
|
|
m_elemContext.m_elementURI = uri; |
|
} |
|
} |
|
startPrefixMapping(prefix,uri,false); |
|
return; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
protected boolean pushNamespace(String prefix, String uri) |
|
{ |
|
try |
|
{ |
|
if (m_prefixMap.pushNamespace( |
|
prefix, uri, m_elemContext.m_currentElemDepth)) |
|
{ |
|
startPrefixMapping(prefix, uri); |
|
return true; |
|
} |
|
} |
|
catch (SAXException e) |
|
{ |
|
// falls through |
|
} |
|
return false; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
public boolean reset() |
|
{ |
|
boolean wasReset = false; |
|
if (super.reset()) |
|
{ |
|
resetToXMLStream(); |
|
wasReset = true; |
|
} |
|
return wasReset; |
|
} |
|
|
|
|
|
|
|
|
|
*/ |
|
private void resetToXMLStream() |
|
{ |
|
this.m_cdataTagOpen = false; |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
*/ |
|
private String getXMLVersion() |
|
{ |
|
String xmlVersion = getVersion(); |
|
if(xmlVersion == null || xmlVersion.equals(XMLVERSION10)) |
|
{ |
|
xmlVersion = XMLVERSION10; |
|
} |
|
else if(xmlVersion.equals(XMLVERSION11)) |
|
{ |
|
xmlVersion = XMLVERSION11; |
|
} |
|
else |
|
{ |
|
String msg = Utils.messages.createMessage( |
|
MsgKey.ER_XML_VERSION_NOT_SUPPORTED,new Object[]{ xmlVersion }); |
|
try |
|
{ |
|
|
|
Transformer tran = super.getTransformer(); |
|
ErrorListener errHandler = tran.getErrorListener(); |
|
|
|
if (null != errHandler && m_sourceLocator != null) |
|
errHandler.warning(new TransformerException(msg, m_sourceLocator)); |
|
else |
|
System.out.println(msg); |
|
} |
|
catch (Exception e){} |
|
xmlVersion = XMLVERSION10; |
|
} |
|
return xmlVersion; |
|
} |
|
} |