Search in sources :

Example 1 with Document

use of org.kxml2.kdom.Document in project robovm by robovm.

the class KxmlSerializerTest method testCdataWithTerminatorInside.

public void testCdataWithTerminatorInside() throws Exception {
    StringWriter writer = new StringWriter();
    XmlSerializer serializer = new KXmlSerializer();
    serializer.setOutput(writer);
    serializer.startDocument("UTF-8", null);
    serializer.startTag(NAMESPACE, "p");
    serializer.cdsect("a]]>b");
    serializer.endTag(NAMESPACE, "p");
    serializer.endDocument();
    // Adjacent CDATA sections aren't merged, so let's stick them together ourselves...
    Document doc = domOf(writer.toString());
    NodeList children = doc.getFirstChild().getChildNodes();
    String text = "";
    for (int i = 0; i < children.getLength(); ++i) {
        text += children.item(i).getNodeValue();
    }
    assertEquals("a]]>b", text);
}
Also used : StringWriter(java.io.StringWriter) NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document) KXmlSerializer(org.kxml2.io.KXmlSerializer) XmlSerializer(org.xmlpull.v1.XmlSerializer) KXmlSerializer(org.kxml2.io.KXmlSerializer)

Example 2 with Document

use of org.kxml2.kdom.Document in project j2objc by google.

the class DocumentBuilderImpl method parse.

@Override
public Document parse(InputSource source) throws SAXException, IOException {
    if (source == null) {
        throw new IllegalArgumentException("source == null");
    }
    String namespaceURI = null;
    String qualifiedName = null;
    DocumentType doctype = null;
    String inputEncoding = source.getEncoding();
    String systemId = source.getSystemId();
    DocumentImpl document = new DocumentImpl(dom, namespaceURI, qualifiedName, doctype, inputEncoding);
    document.setDocumentURI(systemId);
    KXmlParser parser = new KXmlParser();
    try {
        parser.keepNamespaceAttributes();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, namespaceAware);
        if (source.getByteStream() != null) {
            parser.setInput(source.getByteStream(), inputEncoding);
        } else if (source.getCharacterStream() != null) {
            parser.setInput(source.getCharacterStream());
        } else if (systemId != null) {
            URL url = new URL(systemId);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            // TODO: if null, extract the inputEncoding from the Content-Type header?
            parser.setInput(urlConnection.getInputStream(), inputEncoding);
        } else {
            throw new SAXParseException("InputSource needs a stream, reader or URI", null);
        }
        if (parser.nextToken() == XmlPullParser.END_DOCUMENT) {
            throw new SAXParseException("Unexpected end of document", null);
        }
        parse(parser, document, document, XmlPullParser.END_DOCUMENT);
        parser.require(XmlPullParser.END_DOCUMENT, null, null);
    } catch (XmlPullParserException ex) {
        if (ex.getDetail() instanceof IOException) {
            throw (IOException) ex.getDetail();
        }
        if (ex.getDetail() instanceof RuntimeException) {
            throw (RuntimeException) ex.getDetail();
        }
        LocatorImpl locator = new LocatorImpl();
        locator.setPublicId(source.getPublicId());
        locator.setSystemId(systemId);
        locator.setLineNumber(ex.getLineNumber());
        locator.setColumnNumber(ex.getColumnNumber());
        SAXParseException newEx = new SAXParseException(ex.getMessage(), locator);
        if (errorHandler != null) {
            errorHandler.error(newEx);
        }
        throw newEx;
    } finally {
        IoUtils.closeQuietly(parser);
    }
    return document;
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) SAXParseException(org.xml.sax.SAXParseException) DocumentType(org.w3c.dom.DocumentType) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) LocatorImpl(org.xml.sax.helpers.LocatorImpl) IOException(java.io.IOException) DocumentImpl(org.apache.harmony.xml.dom.DocumentImpl) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 3 with Document

use of org.kxml2.kdom.Document in project XobotOS by xamarin.

the class DocumentBuilderImpl method parse.

@Override
public Document parse(InputSource source) throws SAXException, IOException {
    if (source == null) {
        throw new IllegalArgumentException("source == null");
    }
    String namespaceURI = null;
    String qualifiedName = null;
    DocumentType doctype = null;
    String inputEncoding = source.getEncoding();
    String systemId = source.getSystemId();
    DocumentImpl document = new DocumentImpl(dom, namespaceURI, qualifiedName, doctype, inputEncoding);
    document.setDocumentURI(systemId);
    KXmlParser parser = new KXmlParser();
    try {
        parser.keepNamespaceAttributes();
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, namespaceAware);
        if (source.getByteStream() != null) {
            parser.setInput(source.getByteStream(), inputEncoding);
        } else if (source.getCharacterStream() != null) {
            parser.setInput(source.getCharacterStream());
        } else if (systemId != null) {
            URL url = new URL(systemId);
            URLConnection urlConnection = url.openConnection();
            urlConnection.connect();
            // TODO: if null, extract the inputEncoding from the Content-Type header?
            parser.setInput(urlConnection.getInputStream(), inputEncoding);
        } else {
            throw new SAXParseException("InputSource needs a stream, reader or URI", null);
        }
        if (parser.nextToken() == XmlPullParser.END_DOCUMENT) {
            throw new SAXParseException("Unexpected end of document", null);
        }
        parse(parser, document, document, XmlPullParser.END_DOCUMENT);
        parser.require(XmlPullParser.END_DOCUMENT, null, null);
    } catch (XmlPullParserException ex) {
        if (ex.getDetail() instanceof IOException) {
            throw (IOException) ex.getDetail();
        }
        if (ex.getDetail() instanceof RuntimeException) {
            throw (RuntimeException) ex.getDetail();
        }
        LocatorImpl locator = new LocatorImpl();
        locator.setPublicId(source.getPublicId());
        locator.setSystemId(systemId);
        locator.setLineNumber(ex.getLineNumber());
        locator.setColumnNumber(ex.getColumnNumber());
        SAXParseException newEx = new SAXParseException(ex.getMessage(), locator);
        if (errorHandler != null) {
            errorHandler.error(newEx);
        }
        throw newEx;
    } finally {
        IoUtils.closeQuietly(parser);
    }
    return document;
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) SAXParseException(org.xml.sax.SAXParseException) DocumentType(org.w3c.dom.DocumentType) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) LocatorImpl(org.xml.sax.helpers.LocatorImpl) IOException(java.io.IOException) DocumentImpl(org.apache.harmony.xml.dom.DocumentImpl) URL(java.net.URL) URLConnection(java.net.URLConnection)

Example 4 with Document

use of org.kxml2.kdom.Document in project felix by apache.

the class PullParser method parseRepository.

public RepositoryImpl parseRepository(InputStream is, URI baseUri) throws Exception {
    XmlPullParser reader = new KXmlParser();
    // The spec-based Repository XML uses namespaces, so switch this on...
    reader.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    reader.setInput(is, null);
    int event = reader.nextTag();
    if (event != XmlPullParser.START_TAG || !REPOSITORY.equals(reader.getName())) {
        throw new Exception("Expected element 'repository' at the root of the document");
    }
    RepositoryImpl repo;
    if ("http://www.osgi.org/xmlns/repository/v1.0.0".equals(reader.getNamespace())) {
        // how its initiated.
        return SpecXMLPullParser.parse(reader, baseUri);
    } else {
        // We're parsing the old
        return parse(reader);
    }
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 5 with Document

use of org.kxml2.kdom.Document in project collect by opendatakit.

the class FileUtils method parseXML.

public static HashMap<String, String> parseXML(File xmlFile) {
    final HashMap<String, String> fields = new HashMap<String, String>();
    final InputStream is;
    try {
        is = new FileInputStream(xmlFile);
    } catch (FileNotFoundException e1) {
        Timber.d(e1);
        throw new IllegalStateException(e1);
    }
    InputStreamReader isr;
    try {
        isr = new InputStreamReader(is, "UTF-8");
    } catch (UnsupportedEncodingException uee) {
        Timber.w(uee, "Trying default encoding as UTF 8 encoding unavailable");
        isr = new InputStreamReader(is);
    }
    final Document doc;
    try {
        doc = XFormParser.getXMLDocument(isr);
    } catch (IOException e) {
        Timber.e(e, "Unable to parse XML document %s", xmlFile.getAbsolutePath());
        throw new IllegalStateException("Unable to parse XML document", e);
    } finally {
        try {
            isr.close();
        } catch (IOException e) {
            Timber.w("%s error closing from reader", xmlFile.getAbsolutePath());
        }
    }
    final String xforms = "http://www.w3.org/2002/xforms";
    final String html = doc.getRootElement().getNamespace();
    final Element head = doc.getRootElement().getElement(html, "head");
    final Element title = head.getElement(html, "title");
    if (title != null) {
        fields.put(TITLE, XFormParser.getXMLText(title, true));
    }
    final Element model = getChildElement(head, "model");
    Element cur = getChildElement(model, "instance");
    final int idx = cur.getChildCount();
    int i;
    for (i = 0; i < idx; ++i) {
        if (cur.isText(i)) {
            continue;
        }
        if (cur.getType(i) == Node.ELEMENT) {
            break;
        }
    }
    if (i < idx) {
        // this is the first data element
        cur = cur.getElement(i);
        final String id = cur.getAttributeValue(null, "id");
        final String version = cur.getAttributeValue(null, "version");
        final String uiVersion = cur.getAttributeValue(null, "uiVersion");
        if (uiVersion != null) {
            // pre-OpenRosa 1.0 variant of spec
            Timber.e("Obsolete use of uiVersion -- IGNORED -- only using version: %s", version);
        }
        fields.put(FORMID, (id == null) ? cur.getNamespace() : id);
        fields.put(VERSION, (version == null) ? null : version);
    } else {
        throw new IllegalStateException(xmlFile.getAbsolutePath() + " could not be parsed");
    }
    try {
        final Element submission = model.getElement(xforms, "submission");
        final String base64RsaPublicKey = submission.getAttributeValue(null, "base64RsaPublicKey");
        final String autoDelete = submission.getAttributeValue(null, "auto-delete");
        final String autoSend = submission.getAttributeValue(null, "auto-send");
        fields.put(SUBMISSIONURI, submission.getAttributeValue(null, "action"));
        fields.put(BASE64_RSA_PUBLIC_KEY, (base64RsaPublicKey == null || base64RsaPublicKey.trim().length() == 0) ? null : base64RsaPublicKey.trim());
        fields.put(AUTO_DELETE, autoDelete);
        fields.put(AUTO_SEND, autoSend);
    } catch (Exception e) {
        Timber.i("XML file %s does not have a submission element", xmlFile.getAbsolutePath());
    // and that's totally fine.
    }
    return fields;
}
Also used : InputStreamReader(java.io.InputStreamReader) HashMap(java.util.HashMap) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Element(org.kxml2.kdom.Element) FileNotFoundException(java.io.FileNotFoundException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IOException(java.io.IOException) Document(org.kxml2.kdom.Document) FileInputStream(java.io.FileInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

IOException (java.io.IOException)18 KXmlParser (org.kxml2.io.KXmlParser)12 Document (org.kxml2.kdom.Document)11 Element (org.kxml2.kdom.Element)11 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)11 ParsingException (org.opendatakit.briefcase.model.ParsingException)8 File (java.io.File)6 URL (java.net.URL)6 ArrayList (java.util.ArrayList)6 KXmlSerializer (org.kxml2.io.KXmlSerializer)6 FileNotFoundException (java.io.FileNotFoundException)5 InputStreamReader (java.io.InputStreamReader)5 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 InputStream (java.io.InputStream)4 MalformedURLException (java.net.MalformedURLException)4 FileSystemException (org.opendatakit.briefcase.model.FileSystemException)4 MetadataUpdateException (org.opendatakit.briefcase.model.MetadataUpdateException)4 MediaFile (org.opendatakit.briefcase.util.ServerFetcher.MediaFile)4 FileInputStream (java.io.FileInputStream)3 OutputStreamWriter (java.io.OutputStreamWriter)3