📄 xmlwriter.java
字号:
* @param text
* is the text to output
*
* @throws IOException
* DOCUMENT ME!
*/
public void write(String text) throws IOException {
writeString(text);
if (autoFlush) {
flush();
}
}
/**
* Writes the given {@link Text}.
*
* @param text
* <code>Text</code> to output.
*
* @throws IOException
* DOCUMENT ME!
*/
public void write(Text text) throws IOException {
writeString(text.getText());
if (autoFlush) {
flush();
}
}
/**
* Writes the given {@link Node}.
*
* @param node
* <code>Node</code> to output.
*
* @throws IOException
* DOCUMENT ME!
*/
public void write(Node node) throws IOException {
writeNode(node);
if (autoFlush) {
flush();
}
}
/**
* Writes the given object which should be a String, a Node or a List of
* Nodes.
*
* @param object
* is the object to output.
*
* @throws IOException
* DOCUMENT ME!
*/
public void write(Object object) throws IOException {
if (object instanceof Node) {
write((Node) object);
} else if (object instanceof String) {
write((String) object);
} else if (object instanceof List) {
List list = (List) object;
for (int i = 0, size = list.size(); i < size; i++) {
write(list.get(i));
}
} else if (object != null) {
throw new IOException("Invalid object: " + object);
}
}
/**
* <p>
* Writes the opening tag of an {@link Element}, including its {@link
* Attribute}s but without its content.
* </p>
*
* @param element
* <code>Element</code> to output.
*
* @throws IOException
* DOCUMENT ME!
*/
public void writeOpen(Element element) throws IOException {
writer.write("<");
writer.write(element.getQualifiedName());
writeAttributes(element);
writer.write(">");
}
/**
* <p>
* Writes the closing tag of an {@link Element}
* </p>
*
* @param element
* <code>Element</code> to output.
*
* @throws IOException
* DOCUMENT ME!
*/
public void writeClose(Element element) throws IOException {
writeClose(element.getQualifiedName());
}
// XMLFilterImpl methods
// -------------------------------------------------------------------------
public void parse(InputSource source) throws IOException, SAXException {
installLexicalHandler();
super.parse(source);
}
public void setProperty(String name, Object value)
throws SAXNotRecognizedException, SAXNotSupportedException {
for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
if (LEXICAL_HANDLER_NAMES[i].equals(name)) {
setLexicalHandler((LexicalHandler) value);
return;
}
}
super.setProperty(name, value);
}
public Object getProperty(String name) throws SAXNotRecognizedException,
SAXNotSupportedException {
for (int i = 0; i < LEXICAL_HANDLER_NAMES.length; i++) {
if (LEXICAL_HANDLER_NAMES[i].equals(name)) {
return getLexicalHandler();
}
}
return super.getProperty(name);
}
public void setLexicalHandler(LexicalHandler handler) {
if (handler == null) {
throw new NullPointerException("Null lexical handler");
} else {
this.lexicalHandler = handler;
}
}
public LexicalHandler getLexicalHandler() {
return lexicalHandler;
}
// ContentHandler interface
// -------------------------------------------------------------------------
public void setDocumentLocator(Locator locator) {
super.setDocumentLocator(locator);
}
public void startDocument() throws SAXException {
try {
writeDeclaration();
super.startDocument();
} catch (IOException e) {
handleException(e);
}
}
public void endDocument() throws SAXException {
super.endDocument();
if (autoFlush) {
try {
flush();
} catch (IOException e) {
}
}
}
public void startPrefixMapping(String prefix, String uri)
throws SAXException {
if (namespacesMap == null) {
namespacesMap = new HashMap();
}
namespacesMap.put(prefix, uri);
super.startPrefixMapping(prefix, uri);
}
public void endPrefixMapping(String prefix) throws SAXException {
super.endPrefixMapping(prefix);
}
public void startElement(String namespaceURI, String localName,
String qName, Attributes attributes) throws SAXException {
try {
charsAdded = false;
writePrintln();
indent();
writer.write("<");
writer.write(qName);
writeNamespaces();
writeAttributes(attributes);
writer.write(">");
++indentLevel;
lastOutputNodeType = Node.ELEMENT_NODE;
lastElementClosed = false;
super.startElement(namespaceURI, localName, qName, attributes);
} catch (IOException e) {
handleException(e);
}
}
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
try {
charsAdded = false;
--indentLevel;
if (lastElementClosed) {
writePrintln();
indent();
}
// XXXX: need to determine this using a stack and checking for
// content / children
boolean hadContent = true;
if (hadContent) {
writeClose(qName);
} else {
writeEmptyElementClose(qName);
}
lastOutputNodeType = Node.ELEMENT_NODE;
lastElementClosed = true;
super.endElement(namespaceURI, localName, qName);
} catch (IOException e) {
handleException(e);
}
}
public void characters(char[] ch, int start, int length)
throws SAXException {
if ((ch == null) || (ch.length == 0) || (length <= 0)) {
return;
}
try {
/*
* we can't use the writeString method here because it's possible we
* don't receive all characters at once and calling writeString
* would cause unwanted spaces to be added in between these chunks
* of character arrays.
*/
String string = String.valueOf(ch, start, length);
if (escapeText) {
string = escapeElementEntities(string);
}
if (format.isTrimText()) {
if ((lastOutputNodeType == Node.TEXT_NODE) && !charsAdded) {
writer.write(' ');
} else if (charsAdded && Character.isWhitespace(lastChar)) {
writer.write(' ');
} else if (lastOutputNodeType == Node.ELEMENT_NODE
&& format.isPadText() && lastElementClosed
&& Character.isWhitespace(ch[0])) {
writer.write(PAD_TEXT);
}
String delim = "";
StringTokenizer tokens = new StringTokenizer(string);
while (tokens.hasMoreTokens()) {
writer.write(delim);
writer.write(tokens.nextToken());
delim = " ";
}
} else {
writer.write(string);
}
charsAdded = true;
lastChar = ch[(start + length) - 1];
lastOutputNodeType = Node.TEXT_NODE;
super.characters(ch, start, length);
} catch (IOException e) {
handleException(e);
}
}
public void ignorableWhitespace(char[] ch, int start, int length)
throws SAXException {
super.ignorableWhitespace(ch, start, length);
}
public void processingInstruction(String target, String data)
throws SAXException {
try {
indent();
writer.write("<?");
writer.write(target);
writer.write(" ");
writer.write(data);
writer.write("?>");
writePrintln();
lastOutputNodeType = Node.PROCESSING_INSTRUCTION_NODE;
super.processingInstruction(target, data);
} catch (IOException e) {
handleException(e);
}
}
// DTDHandler interface
// -------------------------------------------------------------------------
public void notationDecl(String name, String publicID, String systemID)
throws SAXException {
super.notationDecl(name, publicID, systemID);
}
public void unparsedEntityDecl(String name, String publicID,
String systemID, String notationName) throws SAXException {
super.unparsedEntityDecl(name, publicID, systemID, notationName);
}
// LexicalHandler interface
// -------------------------------------------------------------------------
public void startDTD(String name, String publicID, String systemID)
throws SAXException {
inDTD = true;
try {
writeDocType(name, publicID, systemID);
} catch (IOException e) {
handleException(e);
}
if (lexicalHandler != null) {
lexicalHandler.startDTD(name, publicID, systemID);
}
}
public void endDTD() throws SAXException {
inDTD = false;
if (lexicalHandler != null) {
lexicalHandler.endDTD();
}
}
public void startCDATA() throws SAXException {
try {
writer.write("<![CDATA[");
} catch (IOException e) {
handleException(e);
}
if (lexicalHandler != null) {
lexicalHandler.startCDATA();
}
}
public void endCDATA() throws SAXException {
try {
writer.write("]]>");
} catch (IOException e) {
handleException(e);
}
if (lexicalHandler != null) {
lexicalHandler.endCDATA();
}
}
public void startEntity(String name) throws SAXException {
try {
writeEntityRef(name);
} catch (IOException e) {
handleException(e);
}
if (lexicalHandler != null) {
lexicalHandler.startEntity(name);
}
}
public void endEntity(String name) throws SAXException {
if (lexicalHandler != null) {
lexicalHandler.endEntity(name);
}
}
public void comment(char[] ch, int start, int length) throws SAXException {
if (showCommentsInDTDs || !inDTD) {
try {
charsAdded = false;
writeComment(new String(ch, start, length));
} catch (IOException e) {
handleException(e);
}
}
if (lexicalHandler != null) {
lexicalHandler.comment(ch, start, length);
}
}
// Implementation methods
// -------------------------------------------------------------------------
protected void writeElement(Element element) throws IOException {
int size = element.nodeCount();
String qualifiedName = element.getQualifiedName();
writePrintln();
indent();
writer.write("<");
writer.write(qualifiedName);
int previouslyDeclaredNamespaces = namespaceStack.size();
Namespace ns = element.getNamespace();
if (isNamespaceDeclaration(ns)) {
namespaceStack.push(ns);
writeNamespace(ns);
}
// Print out additional namespace declarations
boolean textOnly = true;
for (int i = 0; i < size; i++) {
Node node = element.node(i);
if (node instanceof Namespace) {
Namespace additional = (Namespace) node;
if (isNamespaceDeclaration(additional)) {
namespaceStack.push(additional);
writeNamespace(additional);
}
} else if (node instanceof Element) {
textOnly = false;
} else if (node instanceof Comment) {
textOnly = false;
}
}
writeAttributes(element);
lastOutputNodeType = Node.ELEMENT_NODE;
if (size <= 0) {
writeEmptyElementClose(qualifiedName);
} else {
writer.write(">");
if (textOnly) {
// we have at least one text node so lets assume
// that its non-empty
writeElementContent(element);
} else {
// we know it's not null or empty from above
++indentLevel;
writeElementContent(element);
--indentLevel;
writePrintln();
indent();
}
writer.write("</");
writer.write(qualifiedName);
writer.write(">");
}
// remove declared namespaceStack from stack
while (namespaceStack.size() > previouslyDeclaredNamespaces) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -