use of org.kxml2.kdom.Element in project javarosa by opendatakit.
the class XmlTextConsolidator method consolidateText.
/**
* According to Clayton Sims, in Feb 2012:
* For escaped unicode strings we end up with a lot of cruft, so we really
* want to go through and convert the kxml parsed text (which have lots of
* characters each as their own string) into one single string
*/
static void consolidateText(CacheTable<String> stringCache, Element rootElement) {
Stack<Element> q = new Stack<>();
q.push(rootElement);
while (!q.isEmpty()) {
final Element e = q.pop();
final Set<Integer> removeIndexes = new HashSet<>();
String accumulator = "";
for (int i = 0; i < e.getChildCount(); ++i) {
final int type = e.getType(i);
if (type == TEXT) {
accumulator += e.getText(i);
removeIndexes.add(i);
} else {
if (type == ELEMENT) {
q.add(e.getElement(i));
}
if (nonBlank(accumulator)) {
e.addChild(i++, TEXT, maybeInternedString(stringCache, accumulator));
}
accumulator = "";
}
}
if (nonBlank(accumulator)) {
e.addChild(TEXT, maybeInternedString(stringCache, accumulator));
}
ElementChildDeleter.delete(e, removeIndexes);
}
}
use of org.kxml2.kdom.Element in project javarosa by opendatakit.
the class ElementParser method instantiateParser.
/**
* Prepares a parser that will be used by the element parser, configuring relevant
* parameters and setting it to the appropriate point in the document.
*
* @param stream A stream which is reading the XML content
* of the document.
* @throws IOException If the stream cannot be read for any reason
* other than invalid XML Structures.
*/
public static KXmlParser instantiateParser(InputStream stream) throws IOException {
KXmlParser parser = new KXmlParser();
try {
parser.setInput(stream, "UTF-8");
parser.setFeature(KXmlParser.FEATURE_PROCESS_NAMESPACES, true);
// Point to the first available tag.
parser.next();
return parser;
} catch (XmlPullParserException e) {
// TODO Auto-generated catch block
Logger.exception("Element Parser", e);
throw new IOException(e.getMessage());
} catch (IllegalArgumentException e) {
e.printStackTrace();
throw new IOException(e.getMessage());
}
}
use of org.kxml2.kdom.Element in project javarosa by opendatakit.
the class XFormAnswerDataSerializer method serializeAnswerData.
/**
* @param data The AnswerDataObject to be serialized
* @return A String which contains a reference to the
* data
*/
public Object serializeAnswerData(MultiPointerAnswerData data) {
// Note: In order to override this default behavior, a
// new serializer should be used, and then registered
// with this serializer
IDataPointer[] pointers = (IDataPointer[]) data.getValue();
if (pointers.length == 1) {
return pointers[0].getDisplayText();
}
Element parent = new Element();
for (int i = 0; i < pointers.length; ++i) {
Element datael = new Element();
datael.setName("data");
datael.addChild(Element.TEXT, pointers[i].getDisplayText());
parent.addChild(Element.ELEMENT, datael);
}
return parent;
}
use of org.kxml2.kdom.Element in project javarosa by opendatakit.
the class ChildProcessingTest method worksWithNoChildren.
@Test
public void worksWithNoChildren() {
Element el = new Element();
assertFalse(XFormParser.childOptimizationsOk(el));
}
use of org.kxml2.kdom.Element in project javarosa by opendatakit.
the class ChildProcessingTest method worksWithTwoNotMatchingChildren.
@Test
public void worksWithTwoNotMatchingChildren() {
Element el = new Element();
el.addChild(ELEMENT, el.createElement(null, "Child Name 1"));
el.addChild(ELEMENT, el.createElement(null, "Child Name 2"));
assertFalse(XFormParser.childOptimizationsOk(el));
}
Aggregations