use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method normalize.
/**
*
*/
void normalize() {
XmlCursor curs = newCursor();
TokenType tt = curs.currentTokenType();
// Walk through the tokens removing empty text nodes and merging adjacent text nodes.
if (tt.isStartdoc()) {
tt = curs.toFirstContentToken();
}
if (tt.isContainer()) {
int nestLevel = 1;
String previousText = null;
while (nestLevel > 0) {
tt = curs.toNextToken();
if (tt == XmlCursor.TokenType.TEXT) {
String currentText = curs.getChars().trim();
if (currentText.trim().length() == 0) {
// Empty text node, remove.
removeToken(curs);
curs.toPrevToken();
} else if (previousText == null) {
// No previous text node, reset to trimmed version
previousText = currentText;
} else {
// It appears that this case never happens with XBeans.
// Previous text node exists, concatenate
String newText = previousText + currentText;
curs.toPrevToken();
removeToken(curs);
removeToken(curs);
curs.insertChars(newText);
}
} else {
previousText = null;
}
if (tt.isStart()) {
nestLevel++;
} else if (tt.isEnd()) {
nestLevel--;
} else if (tt.isEnddoc()) {
// Shouldn't get here, but just in case.
break;
}
}
}
curs.dispose();
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method equivalentXml.
//
// Other public Functions from XMLObject
//
/**
*
* @param target
* @return
*/
boolean equivalentXml(Object target) {
boolean result = false;
if (target instanceof XML) {
XML otherXml = (XML) target;
// Compare with toString() if either side is text node or attribute
// otherwise compare as XML
XmlCursor.TokenType thisTT = tokenType();
XmlCursor.TokenType otherTT = otherXml.tokenType();
if (thisTT == XmlCursor.TokenType.ATTR || otherTT == XmlCursor.TokenType.ATTR || thisTT == XmlCursor.TokenType.TEXT || otherTT == XmlCursor.TokenType.TEXT) {
result = toString().equals(otherXml.toString());
} else {
XmlCursor cursOne = newCursor();
XmlCursor cursTwo = otherXml.newCursor();
result = LogicalEquality.nodesEqual(cursOne, cursTwo);
cursOne.dispose();
cursTwo.dispose();
// Old way of comparing by string.
// boolean orgPrettyPrinting = prototype.prettyPrinting;
// prototype.prettyPrinting = true;
// result = toXMLString(0).equals(otherXml.toXMLString(0));
// prototype.prettyPrinting = orgPrettyPrinting;
}
} else if (target instanceof XMLList) {
XMLList otherList = (XMLList) target;
if (otherList.length() == 1) {
result = equivalentXml(otherList.getXmlFromAnnotation(0));
}
} else if (hasSimpleContent()) {
String otherStr = ScriptRuntime.toString(target);
result = toString().equals(otherStr);
}
return result;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method createEmptyXML.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
// Public factories for creating a XScript XML object given an XBean cursor.
//
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static XML createEmptyXML(XMLLibImpl lib) {
XScriptAnnotation anno;
XmlObject xo = XmlObject.Factory.newInstance();
XmlCursor curs = xo.newCursor();
try {
anno = new XScriptAnnotation(curs);
curs.setBookmark(anno);
} finally {
curs.dispose();
}
return new XML(lib, anno);
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method toString.
/**
*
* @return
*/
public String toString() {
String result;
XmlCursor curs = newCursor();
if (curs.isStartdoc()) {
curs.toFirstContentToken();
}
if (curs.isText()) {
result = curs.getChars();
} else if (curs.isStart() && hasSimpleContent()) {
result = curs.getTextValue();
} else {
result = toXMLString(0);
}
return result;
}
use of org.apache.xmlbeans.XmlCursor in project hackpad by dropbox.
the class XML method localName.
/**
*
* @return
*/
String localName() {
XmlCursor cursor = newCursor();
if (cursor.isStartdoc())
cursor.toFirstContentToken();
String name = null;
if (cursor.isStart() || cursor.isAttr() || cursor.isProcinst()) {
javax.xml.namespace.QName qname = cursor.getName();
name = qname.getLocalPart();
}
cursor.dispose();
return name;
}
Aggregations