/* 483: */ /* 484: */ protected AttributesImpl startPrefixMapping( Element element, NamespaceStack namespaceStack) /* 485: */ throws SAXException /* 486: */ { /* 487:754 */ AttributesImpl namespaceAttributes = null; /* 488: */ /* 489: */ /* 490:757 */ Namespace elementNamespace = element.getNamespace(); /* 491:759 */ if ((elementNamespace != null) && (!isIgnoreableNamespace(elementNamespace, namespaceStack))) /* 492: */ { /* 493:761 */ namespaceStack.push(elementNamespace); /* 494:762 */ this.contentHandler.startPrefixMapping( elementNamespace.getPrefix(), elementNamespace.getURI()); /* 495: */ /* 496:764 */ namespaceAttributes = addNamespaceAttribute(namespaceAttributes, elementNamespace); /* 497: */ } /* 498:768 */ List declaredNamespaces = element.declaredNamespaces(); /* 499: */ /* 500:770 */ int i = 0; /* 501:770 */ for (int size = declaredNamespaces.size(); i < size; i++) /* 502: */ { /* 503:771 */ Namespace namespace = (Namespace) declaredNamespaces.get(i); /* 504:773 */ if (!isIgnoreableNamespace(namespace, namespaceStack)) /* 505: */ { /* 506:774 */ namespaceStack.push(namespace); /* 507:775 */ this.contentHandler.startPrefixMapping( namespace.getPrefix(), namespace.getURI()); /* 508: */ /* 509:777 */ namespaceAttributes = addNamespaceAttribute(namespaceAttributes, namespace); /* 510: */ } /* 511: */ } /* 512:782 */ return namespaceAttributes; /* 513: */ }
/* 514: */ /* 515: */ protected void endPrefixMapping(NamespaceStack stack, int stackSize) /* 516: */ throws SAXException /* 517: */ { /* 518:799 */ while (stack.size() > stackSize) /* 519: */ { /* 520:800 */ Namespace namespace = stack.pop(); /* 521:802 */ if (namespace != null) { /* 522:803 */ this.contentHandler.endPrefixMapping(namespace.getPrefix()); /* 523: */ } /* 524: */ } /* 525: */ }
/* 471: */ /* 472: */ protected void write(Element element, NamespaceStack namespaceStack) /* 473: */ throws SAXException /* 474: */ { /* 475:729 */ int stackSize = namespaceStack.size(); /* 476:730 */ AttributesImpl namespaceAttributes = startPrefixMapping(element, namespaceStack); /* 477: */ /* 478:732 */ startElement(element, namespaceAttributes); /* 479:733 */ writeContent(element, namespaceStack); /* 480:734 */ endElement(element); /* 481:735 */ endPrefixMapping(namespaceStack, stackSize); /* 482: */ }
protected boolean isNamespaceDeclaration(Namespace ns) { if ((ns != null) && (ns != Namespace.XML_NAMESPACE)) { String uri = ns.getURI(); if (uri != null) { if (!namespaceStack.contains(ns)) { return true; } } } return false; }
/* 575: */ /* 576: */ protected boolean isIgnoreableNamespace( Namespace namespace, NamespaceStack namespaceStack) /* 577: */ { /* 578:889 */ if ((namespace.equals(Namespace.NO_NAMESPACE)) || (namespace.equals(Namespace.XML_NAMESPACE))) { /* 579:891 */ return true; /* 580: */ } /* 581:894 */ String uri = namespace.getURI(); /* 582:896 */ if ((uri == null) || (uri.length() <= 0)) { /* 583:897 */ return true; /* 584: */ } /* 585:900 */ return namespaceStack.contains(namespace); /* 586: */ }
/** * 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(); if ((ns != null) && (ns != Namespace.NO_NAMESPACE) && (ns != Namespace.XML_NAMESPACE)) { String prefix = ns.getPrefix(); String uri = namespaceStack.getURI(prefix); if (!ns.getURI().equals(uri)) { writeNamespace(ns); namespaceStack.push(ns); } } // If the attribute is a namespace declaration, check if we have // already written that declaration elsewhere (if that's the case, // it must be in the namespace stack String attName = attribute.getName(); if (attName.startsWith("xmlns:")) { String prefix = attName.substring(6); if (namespaceStack.getNamespaceForPrefix(prefix) == null) { String uri = attribute.getValue(); namespaceStack.push(prefix, uri); writeNamespace(prefix, uri); } } else if (attName.equals("xmlns")) { if (namespaceStack.getDefaultNamespace() == null) { String uri = attribute.getValue(); namespaceStack.push(null, uri); writeNamespace(null, uri); } } else { char quote = format.getAttributeQuoteCharacter(); writer.write(" "); writer.write(attribute.getQualifiedName()); writer.write("="); writer.write(quote); writeEscapeAttributeEntities(attribute.getValue()); writer.write(quote); } } }
// 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) { namespaceStack.pop(); } lastOutputNodeType = Node.ELEMENT_NODE; }
public XMLWriter(OutputFormat format) throws UnsupportedEncodingException { this.format = format; this.writer = createWriter(System.out, format.getEncoding()); this.autoFlush = true; namespaceStack.push(Namespace.NO_NAMESPACE); }
public XMLWriter(OutputStream out) throws UnsupportedEncodingException { this.format = DEFAULT_FORMAT; this.writer = createWriter(out, format.getEncoding()); this.autoFlush = true; namespaceStack.push(Namespace.NO_NAMESPACE); }
public XMLWriter() { this.format = DEFAULT_FORMAT; this.writer = new BufferedWriter(new OutputStreamWriter(System.out)); this.autoFlush = true; namespaceStack.push(Namespace.NO_NAMESPACE); }
public XMLWriter(Writer writer, OutputFormat format) { this.writer = writer; this.format = format; namespaceStack.push(Namespace.NO_NAMESPACE); }