use of org.jdom.Attribute in project cxf by apache.
the class StaxBuilder method buildTree.
/**
* This takes a <code>XMLStreamReader</code> and builds up a JDOM tree.
* Recursion has been eliminated by using local stack of open elements; this
* improves performance somewhat (classic
* recursion-by-iteration-and-explicit stack transformation)
*
* @param node <code>Code</node> to examine.
* @param doc JDOM <code>Document</code> being built.
*/
private void buildTree(JDOMFactory f, XMLStreamReader r, Document doc) throws XMLStreamException {
// At top level
Element current = null;
int event = r.getEventType();
// if we're at the start then we need to do a next
if (event == -1) {
event = r.next();
}
while (true) {
boolean noadd = false;
Content child = null;
switch(event) {
case XMLStreamConstants.CDATA:
child = f.cdata(r.getText());
break;
case XMLStreamConstants.SPACE:
if (cfgIgnoreWS) {
noadd = true;
break;
}
case XMLStreamConstants.CHARACTERS:
/*
* Small complication: although (ignorable) white space is
* allowed in prolog/epilog, and StAX may report such event,
* JDOM barfs if trying to add it. Thus, let's just ignore all
* textual stuff outside the tree:
*/
if (current == null) {
noadd = true;
break;
}
child = f.text(r.getText());
break;
case XMLStreamConstants.COMMENT:
child = f.comment(r.getText());
break;
case XMLStreamConstants.END_DOCUMENT:
return;
case XMLStreamConstants.END_ELEMENT:
/**
* If current.getParentElement() previously returned null and we
* get this event again we shouldn't bail out with a
* NullPointerException
*/
if (current != null) {
current = current.getParentElement();
}
noadd = true;
if (isReadingMidStream && current == null)
return;
break;
case XMLStreamConstants.ENTITY_DECLARATION:
case XMLStreamConstants.NOTATION_DECLARATION:
/*
* Shouldn't really get these, but maybe some stream readers do
* provide the info. If so, better ignore it -- DTD event should
* have most/all we need.
*/
noadd = true;
break;
case XMLStreamConstants.ENTITY_REFERENCE:
child = f.entityRef(r.getLocalName());
break;
case XMLStreamConstants.PROCESSING_INSTRUCTION:
child = f.processingInstruction(r.getPITarget(), r.getPIData());
break;
case XMLStreamConstants.START_ELEMENT:
{
// Ok, need to add a new element and simulate recursion
Element newElem = null;
String nsURI = r.getNamespaceURI();
// needed for special
String elemPrefix = r.getPrefix();
// handling of elem's
// namespace
String ln = r.getLocalName();
if (nsURI == null || nsURI.length() == 0) {
if (elemPrefix == null || elemPrefix.length() == 0) {
newElem = f.element(ln);
} else {
/*
* Happens when a prefix is bound to the default (empty)
* namespace...
*/
newElem = f.element(ln, elemPrefix, "");
}
} else {
newElem = f.element(ln, elemPrefix, nsURI);
}
/*
* Let's add element right away (probably have to do it to bind
* attribute namespaces, too)
*/
if (current == null) {
// at root
doc.setRootElement(newElem);
if (additionalNamespaces != null) {
for (Iterator<String> iter = additionalNamespaces.keySet().iterator(); iter.hasNext(); ) {
String prefix = iter.next();
String uri = additionalNamespaces.get(prefix);
newElem.addNamespaceDeclaration(Namespace.getNamespace(prefix, uri));
}
}
} else {
f.addContent(current, newElem);
}
// Any declared namespaces?
int i;
int len;
for (i = 0, len = r.getNamespaceCount(); i < len; ++i) {
String prefix = r.getNamespacePrefix(i);
Namespace ns = Namespace.getNamespace(prefix, r.getNamespaceURI(i));
// JDOM has special handling for element's "own" ns:
if (prefix != null && prefix.equals(elemPrefix)) {
// already set by when it was constructed...
} else {
f.addNamespaceDeclaration(newElem, ns);
}
}
// And then the attributes:
for (i = 0, len = r.getAttributeCount(); i < len; ++i) {
String prefix = r.getAttributePrefix(i);
Namespace ns;
if (prefix == null || prefix.length() == 0) {
// Attribute not in any namespace
ns = Namespace.NO_NAMESPACE;
} else {
ns = newElem.getNamespace(prefix);
}
Attribute attr = f.attribute(r.getAttributeLocalName(i), r.getAttributeValue(i), resolveAttrType(r.getAttributeType(i)), ns);
f.setAttribute(newElem, attr);
}
// And then 'push' new element...
current = newElem;
// Already added the element, can continue
noadd = true;
break;
}
case XMLStreamConstants.START_DOCUMENT:
case XMLStreamConstants.DTD:
// case XMLStreamConstants.NAMESPACE:
default:
/*
* throw new XMLStreamException("Unrecognized iterator event
* type: " + r.getEventType() + "; should not receive such types
* (broken stream reader?)");
*/
break;
}
if (!noadd && child != null) {
if (current == null) {
f.addContent(doc, child);
} else {
f.addContent(current, child);
}
}
if (r.hasNext()) {
event = r.next();
} else {
break;
}
}
}
use of org.jdom.Attribute in project cxf by apache.
the class StaxSerializer method writeElement.
public void writeElement(Element e, XMLStreamWriter writer) throws XMLStreamException {
// need to check if the namespace is declared before we write the
// start element because that will put the namespace in the context.
String elPrefix = e.getNamespacePrefix();
String elUri = e.getNamespaceURI();
String boundPrefix = writer.getPrefix(elUri);
boolean writeElementNS = false;
if (boundPrefix == null || !elPrefix.equals(boundPrefix)) {
writeElementNS = true;
}
writer.writeStartElement(elPrefix, e.getName(), elUri);
List<?> namespaces = e.getAdditionalNamespaces();
for (Iterator<?> itr = namespaces.iterator(); itr.hasNext(); ) {
Namespace ns = (Namespace) itr.next();
String prefix = ns.getPrefix();
String uri = ns.getURI();
writer.setPrefix(prefix, uri);
writer.writeNamespace(prefix, uri);
if (elUri.equals(uri) && elPrefix.equals(prefix)) {
writeElementNS = false;
}
}
for (Iterator<?> itr = e.getAttributes().iterator(); itr.hasNext(); ) {
Attribute attr = (Attribute) itr.next();
String attPrefix = attr.getNamespacePrefix();
String attUri = attr.getNamespaceURI();
if (attUri == null || attUri.equals("")) {
writer.writeAttribute(attr.getName(), attr.getValue());
} else {
writer.writeAttribute(attPrefix, attUri, attr.getName(), attr.getValue());
if (!isDeclared(writer, attPrefix, attUri)) {
if (elUri.equals(attUri) && elPrefix.equals(attPrefix)) {
if (writeElementNS) {
writer.setPrefix(attPrefix, attUri);
writer.writeNamespace(attPrefix, attUri);
writeElementNS = false;
}
} else {
writer.setPrefix(attPrefix, attUri);
writer.writeNamespace(attPrefix, attUri);
}
}
}
}
if (writeElementNS) {
if (elPrefix == null || elPrefix.length() == 0) {
writer.writeDefaultNamespace(elUri);
} else {
writer.writeNamespace(elPrefix, elUri);
}
}
for (Iterator<?> itr = e.getContent().iterator(); itr.hasNext(); ) {
Content n = (Content) itr.next();
if (n instanceof CDATA) {
writer.writeCData(n.getValue());
} else if (n instanceof Text) {
writer.writeCharacters(((Text) n).getText());
} else if (n instanceof Element) {
writeElement((Element) n, writer);
} else if (n instanceof Comment) {
writer.writeComment(n.getValue());
} else if (n instanceof EntityRef) {
// EntityRef ref = (EntityRef) n;
// writer.writeEntityRef(ref.)
}
}
writer.writeEndElement();
}
Aggregations