Search in sources :

Example 56 with Element

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);
    }
}
Also used : Element(org.kxml2.kdom.Element) Stack(java.util.Stack) HashSet(java.util.HashSet)

Example 57 with Element

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());
    }
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 58 with Element

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;
}
Also used : Element(org.kxml2.kdom.Element) IDataPointer(org.javarosa.core.data.IDataPointer)

Example 59 with Element

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));
}
Also used : Element(org.kxml2.kdom.Element) Test(org.junit.Test)

Example 60 with Element

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));
}
Also used : Element(org.kxml2.kdom.Element) Test(org.junit.Test)

Aggregations

Element (org.kxml2.kdom.Element)50 TreeElement (org.javarosa.core.model.instance.TreeElement)25 AbstractTreeElement (org.javarosa.core.model.instance.AbstractTreeElement)23 ArrayList (java.util.ArrayList)21 IOException (java.io.IOException)17 IFormElement (org.javarosa.core.model.IFormElement)17 KXmlParser (org.kxml2.io.KXmlParser)12 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)12 File (java.io.File)8 Test (org.junit.Test)7 ParsingException (org.opendatakit.briefcase.model.ParsingException)7 XmlPullParser (org.xmlpull.v1.XmlPullParser)7 Document (org.kxml2.kdom.Document)6 UnsupportedEncodingException (java.io.UnsupportedEncodingException)4 FileInputStream (java.io.FileInputStream)3 FileNotFoundException (java.io.FileNotFoundException)3 OutputStreamWriter (java.io.OutputStreamWriter)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 HashMap (java.util.HashMap)3 KXmlSerializer (org.kxml2.io.KXmlSerializer)3