📄 xmlwriter.java
字号:
namespaceStack.pop();
}
lastOutputNodeType = Node.ELEMENT_NODE;
}
/**
* Determines if element is a special case of XML elements where it contains
* an xml:space attribute of "preserve". If it does, then retain whitespace.
*
* @param element
* DOCUMENT ME!
*
* @return DOCUMENT ME!
*/
protected final boolean isElementSpacePreserved(Element element) {
final Attribute attr = (Attribute) element.attribute("space");
boolean preserveFound = preserve; // default to global state
if (attr != null) {
if ("xml".equals(attr.getNamespacePrefix())
&& "preserve".equals(attr.getText())) {
preserveFound = true;
} else {
preserveFound = false;
}
}
return preserveFound;
}
/**
* Outputs the content of the given element. If whitespace trimming is
* enabled then all adjacent text nodes are appended together before the
* whitespace trimming occurs to avoid problems with multiple text nodes
* being created due to text content that spans parser buffers in a SAX
* parser.
*
* @param element
* DOCUMENT ME!
*
* @throws IOException
* DOCUMENT ME!
*/
protected void writeElementContent(Element element) throws IOException {
boolean trim = format.isTrimText();
boolean oldPreserve = preserve;
if (trim) { // verify we have to before more expensive test
preserve = isElementSpacePreserved(element);
trim = !preserve;
}
if (trim) {
// concatenate adjacent text nodes together
// so that whitespace trimming works properly
Text lastTextNode = null;
StringBuffer buff = null;
boolean textOnly = true;
for (int i = 0, size = element.nodeCount(); i < size; i++) {
Node node = element.node(i);
if (node instanceof Text) {
if (lastTextNode == null) {
lastTextNode = (Text) node;
} else {
if (buff == null) {
buff = new StringBuffer(lastTextNode.getText());
}
buff.append(((Text) node).getText());
}
} else {
if (!textOnly && format.isPadText()) {
// only add the PAD_TEXT if the text itself starts with
// whitespace
char firstChar = 'a';
if (buff != null) {
firstChar = buff.charAt(0);
} else if (lastTextNode != null) {
firstChar = lastTextNode.getText().charAt(0);
}
if (Character.isWhitespace(firstChar)) {
writer.write(PAD_TEXT);
}
}
if (lastTextNode != null) {
if (buff != null) {
writeString(buff.toString());
buff = null;
} else {
writeString(lastTextNode.getText());
}
if (format.isPadText()) {
// only add the PAD_TEXT if the text itself ends
// with whitespace
char lastTextChar = 'a';
if (buff != null) {
lastTextChar = buff.charAt(buff.length() - 1);
} else if (lastTextNode != null) {
String txt = lastTextNode.getText();
lastTextChar = txt.charAt(txt.length() - 1);
}
if (Character.isWhitespace(lastTextChar)) {
writer.write(PAD_TEXT);
}
}
lastTextNode = null;
}
textOnly = false;
writeNode(node);
}
}
if (lastTextNode != null) {
if (!textOnly && format.isPadText()) {
// only add the PAD_TEXT if the text itself starts with
// whitespace
char firstChar = 'a';
if (buff != null) {
firstChar = buff.charAt(0);
} else {
firstChar = lastTextNode.getText().charAt(0);
}
if (Character.isWhitespace(firstChar)) {
writer.write(PAD_TEXT);
}
}
if (buff != null) {
writeString(buff.toString());
buff = null;
} else {
writeString(lastTextNode.getText());
}
lastTextNode = null;
}
} else {
Node lastTextNode = null;
for (int i = 0, size = element.nodeCount(); i < size; i++) {
Node node = element.node(i);
if (node instanceof Text) {
writeNode(node);
lastTextNode = node;
} else {
if ((lastTextNode != null) && format.isPadText()) {
// only add the PAD_TEXT if the text itself ends with
// whitespace
String txt = lastTextNode.getText();
char lastTextChar = txt.charAt(txt.length() - 1);
if (Character.isWhitespace(lastTextChar)) {
writer.write(PAD_TEXT);
}
}
writeNode(node);
// if ((lastTextNode != null) && format.isPadText()) {
// writer.write(PAD_TEXT);
// }
lastTextNode = null;
}
}
}
preserve = oldPreserve;
}
protected void writeCDATA(String text) throws IOException {
writer.write("<![CDATA[");
if (text != null) {
writer.write(text);
}
writer.write("]]>");
lastOutputNodeType = Node.CDATA_SECTION_NODE;
}
protected void writeDocType(DocumentType docType) throws IOException {
if (docType != null) {
docType.write(writer);
writePrintln();
}
}
protected void writeNamespace(Namespace namespace) throws IOException {
if (namespace != null) {
writeNamespace(namespace.getPrefix(), namespace.getURI());
}
}
/**
* Writes the SAX namepsaces
*
* @throws IOException
* DOCUMENT ME!
*/
protected void writeNamespaces() throws IOException {
if (namespacesMap != null) {
for (Iterator iter = namespacesMap.entrySet().iterator(); iter
.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
String prefix = (String) entry.getKey();
String uri = (String) entry.getValue();
writeNamespace(prefix, uri);
}
namespacesMap = null;
}
}
/**
* Writes the SAX namepsaces
*
* @param prefix
* the prefix
* @param uri
* the namespace uri
*
* @throws IOException
*/
protected void writeNamespace(String prefix, String uri)
throws IOException {
if ((prefix != null) && (prefix.length() > 0)) {
writer.write(" xmlns:");
writer.write(prefix);
writer.write("=\"");
} else {
writer.write(" xmlns=\"");
}
writer.write(uri);
writer.write("\"");
}
protected void writeProcessingInstruction(ProcessingInstruction pi)
throws IOException {
// indent();
writer.write("<?");
writer.write(pi.getName());
writer.write(" ");
writer.write(pi.getText());
writer.write("?>");
writePrintln();
lastOutputNodeType = Node.PROCESSING_INSTRUCTION_NODE;
}
protected void writeString(String text) throws IOException {
if ((text != null) && (text.length() > 0)) {
if (escapeText) {
text = escapeElementEntities(text);
}
// if (format.isPadText()) {
// if (lastOutputNodeType == Node.ELEMENT_NODE) {
// writer.write(PAD_TEXT);
// }
// }
if (format.isTrimText()) {
boolean first = true;
StringTokenizer tokenizer = new StringTokenizer(text);
while (tokenizer.hasMoreTokens()) {
String token = tokenizer.nextToken();
if (first) {
first = false;
if (lastOutputNodeType == Node.TEXT_NODE) {
writer.write(" ");
}
} else {
writer.write(" ");
}
writer.write(token);
lastOutputNodeType = Node.TEXT_NODE;
lastChar = token.charAt(token.length() - 1);
}
} else {
lastOutputNodeType = Node.TEXT_NODE;
writer.write(text);
lastChar = text.charAt(text.length() - 1);
}
}
}
/**
* This method is used to write out Nodes that contain text and still allow
* for xml:space to be handled properly.
*
* @param node
* DOCUMENT ME!
*
* @throws IOException
* DOCUMENT ME!
*/
protected void writeNodeText(Node node) throws IOException {
String text = node.getText();
if ((text != null) && (text.length() > 0)) {
if (escapeText) {
text = escapeElementEntities(text);
}
lastOutputNodeType = Node.TEXT_NODE;
writer.write(text);
lastChar = text.charAt(text.length() - 1);
}
}
protected void writeNode(Node node) throws IOException {
int nodeType = node.getNodeType();
switch (nodeType) {
case Node.ELEMENT_NODE:
writeElement((Element) node);
break;
case Node.ATTRIBUTE_NODE:
writeAttribute((Attribute) node);
break;
case Node.TEXT_NODE:
writeNodeText(node);
// write((Text) node);
break;
case Node.CDATA_SECTION_NODE:
writeCDATA(node.getText());
break;
case Node.ENTITY_REFERENCE_NODE:
writeEntity((Entity) node);
break;
case Node.PROCESSING_INSTRUCTION_NODE:
writeProcessingInstruction((ProcessingInstruction) node);
break;
case Node.COMMENT_NODE:
writeComment(node.getText());
break;
case Node.DOCUMENT_NODE:
write((Document) node);
break;
case Node.DOCUMENT_TYPE_NODE:
writeDocType((DocumentType) node);
break;
case Node.NAMESPACE_NODE:
// Will be output with attributes
// write((Namespace) node);
break;
default:
throw new IOException("Invalid node type: " + node);
}
}
protected void installLexicalHandler() {
XMLReader parent = getParent();
if (parent == null) {
throw new NullPointerException("No parent for filter");
}
// try to register for lexical events
for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
try {
parent.setProperty(LEXICAL_HANDLER_NAMES[i], this);
break;
} catch (SAXNotRecognizedException ex) {
// ignore
} catch (SAXNotSupportedException ex) {
// ignore
}
}
}
protected void writeDocType(String name, String publicID, String systemID)
throws IOException {
boolean hasPublic = false;
writer.write("<!DOCTYPE ");
writer.write(name);
if ((publicID != null) && (!publicID.equals(""))) {
writer.write(" PUBLIC \"");
writer.write(publicID);
writer.write("\"");
hasPublic = true;
}
if ((systemID != null) && (!systemID.equals(""))) {
if (!hasPublic) {
writer.write(" SYSTEM");
}
writer.write(" \"");
writer.write(systemID);
writer.write("\"");
}
writer.write(">");
writePrintln();
}
protected void writeEntity(Entity entity) throws IOException {
if (!resolveEntityRefs()) {
writeEntityRef(entity.getName());
} else {
writer.write(entity.getText());
}
}
protected void writeEntityRef(String name) throws IOException {
writer.write("&");
writer.write(name);
writer.write(";");
lastOutputNodeType = Node.ENTITY_REFERENCE_NODE;
}
protected void writeComment(String text) throws IOException {
if (format.isNewlines()) {
println();
indent();
}
writer.write("<!--");
writer.write(text);
writer.write("-->");
lastOutputNodeType = Node.COMMENT_NODE;
}
/**
* Writes the attributes of the given element
*
* @param element
* DOCUMENT ME!
*
* @throws IOException
* DOCUMENT ME!
*/
protected void writeAttributes(Element element) throws IOException {
// I do not yet handle the case where the same prefix maps to
// two different URIs. For attributes on the same element
// this is illegal; but as yet we don't throw an exception
// if someone tries to do this
for (int i = 0, size = element.attributeCount(); i < size; i++) {
Attribute attribute = element.attribute(i);
Namespace ns = attribute.getNamespace();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -