Search in sources :

Example 36 with KXmlParser

use of org.kxml2.io.KXmlParser in project felix by apache.

the class PullParser method parseRepository.

public RepositoryImpl parseRepository(Reader r) throws Exception {
    XmlPullParser reader = new KXmlParser();
    reader.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    reader.setInput(r);
    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");
    }
    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 37 with KXmlParser

use of org.kxml2.io.KXmlParser in project felix by apache.

the class PullParser method parseCapability.

public CapabilityImpl parseCapability(Reader r) throws Exception {
    XmlPullParser reader = new KXmlParser();
    reader.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    reader.setInput(r);
    int event = reader.nextTag();
    if (event != XmlPullParser.START_TAG || !CAPABILITY.equals(reader.getName())) {
        throw new Exception("Expected element 'capability'");
    }
    return parseCapability(reader);
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) IOException(java.io.IOException)

Example 38 with KXmlParser

use of org.kxml2.io.KXmlParser in project platform_frameworks_base by android.

the class LayoutParserWrapperTest method getParserFromString.

private static LayoutParserWrapper getParserFromString(String layoutContent) throws XmlPullParserException {
    XmlPullParser parser = new KXmlParser();
    parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
    parser.setInput(new StringReader(layoutContent));
    return new LayoutParserWrapper(parser);
}
Also used : KXmlParser(org.kxml2.io.KXmlParser) XmlPullParser(org.xmlpull.v1.XmlPullParser) StringReader(java.io.StringReader)

Example 39 with KXmlParser

use of org.kxml2.io.KXmlParser in project briefcase by opendatakit.

the class XmlManipulationUtils method parseXml.

public static Document parseXml(File submission) throws ParsingException, FileSystemException {
    // parse the xml document...
    Document doc = null;
    try {
        InputStream is = null;
        InputStreamReader isr = null;
        try {
            is = new FileInputStream(submission);
            isr = new InputStreamReader(is, UTF_8);
            Document tempDoc = new Document();
            KXmlParser parser = new KXmlParser();
            parser.setInput(isr);
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
            tempDoc.parse(parser);
            isr.close();
            doc = tempDoc;
        } finally {
            if (isr != null) {
                try {
                    isr.close();
                } catch (Exception e) {
                // no-op
                }
            }
            if (is != null) {
                try {
                    is.close();
                } catch (Exception e) {
                // no-op
                }
            }
        }
    } catch (XmlPullParserException e) {
        try {
            return BadXMLFixer.fixBadXML(submission);
        } catch (CannotFixXMLException e1) {
            File debugFileLocation = new File(new StorageLocation().getBriefcaseFolder(), "debug");
            try {
                if (!debugFileLocation.exists()) {
                    FileUtils.forceMkdir(debugFileLocation);
                }
                long checksum = FileUtils.checksumCRC32(submission);
                File debugFile = new File(debugFileLocation, "submission-" + checksum + ".xml");
                FileUtils.copyFile(submission, debugFile);
            } catch (IOException e2) {
                throw new RuntimeException(e2);
            }
            throw new ParsingException("Failed during parsing of submission Xml: " + e.toString());
        }
    } catch (IOException e) {
        throw new FileSystemException("Failed while reading submission xml: " + e.toString());
    }
    return doc;
}
Also used : CannotFixXMLException(org.opendatakit.briefcase.model.CannotFixXMLException) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Document(org.kxml2.kdom.Document) FileInputStream(java.io.FileInputStream) FileSystemException(org.opendatakit.briefcase.model.FileSystemException) CannotFixXMLException(org.opendatakit.briefcase.model.CannotFixXMLException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) MetadataUpdateException(org.opendatakit.briefcase.model.MetadataUpdateException) FileNotFoundException(java.io.FileNotFoundException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) ParsingException(org.opendatakit.briefcase.model.ParsingException) KXmlParser(org.kxml2.io.KXmlParser) FileSystemException(org.opendatakit.briefcase.model.FileSystemException) ParsingException(org.opendatakit.briefcase.model.ParsingException) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) StorageLocation(org.opendatakit.briefcase.ui.StorageLocation) MediaFile(org.opendatakit.briefcase.util.ServerFetcher.MediaFile) File(java.io.File)

Example 40 with KXmlParser

use of org.kxml2.io.KXmlParser in project briefcase by opendatakit.

the class XmlManipulationUtils method updateSubmissionMetadata.

public static final String updateSubmissionMetadata(File submissionFile, Document doc) throws MetadataUpdateException {
    Element root = doc.getRootElement();
    Element metadata = root.getElement(NAMESPACE_ODK, "submissionMetadata");
    // and get the instanceID and submissionDate from the metadata.
    // we need to put that back into the instance file if not already present
    String instanceID = metadata.getAttributeValue("", INSTANCE_ID_ATTRIBUTE_NAME);
    String submissionDate = metadata.getAttributeValue("", SUBMISSION_DATE_ATTRIBUTE_NAME);
    // read the original document...
    Document originalDoc = null;
    try {
        FileInputStream fs = new FileInputStream(submissionFile);
        InputStreamReader fr = new InputStreamReader(fs, "UTF-8");
        originalDoc = new Document();
        KXmlParser parser = new KXmlParser();
        parser.setInput(fr);
        parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
        originalDoc.parse(parser);
        fr.close();
        fs.close();
    } catch (IOException e) {
        String msg = "Original submission file could not be opened";
        log.error(msg, e);
        throw new MetadataUpdateException(msg);
    } catch (XmlPullParserException e) {
        String msg = "Original submission file could not be parsed as XML file";
        log.error(msg, e);
        throw new MetadataUpdateException(msg);
    }
    // determine whether it has the attributes already added.
    // if they are already there, they better match the values returned by
    // Aggregate 1.0
    boolean hasInstanceID = false;
    boolean hasSubmissionDate = false;
    root = originalDoc.getRootElement();
    for (int i = 0; i < root.getAttributeCount(); ++i) {
        String name = root.getAttributeName(i);
        if (name.equals(INSTANCE_ID_ATTRIBUTE_NAME)) {
            if (!root.getAttributeValue(i).equals(instanceID)) {
                String msg = "Original submission file's instanceID does not match that on server!";
                log.error(msg);
                throw new MetadataUpdateException(msg);
            } else {
                hasInstanceID = true;
            }
        }
        if (name.equals(SUBMISSION_DATE_ATTRIBUTE_NAME)) {
            Date oldDate = WebUtils.parseDate(submissionDate);
            String returnDate = root.getAttributeValue(i);
            Date newDate = WebUtils.parseDate(returnDate);
            // cross-platform datetime resolution is 1 second.
            if (Math.abs(newDate.getTime() - oldDate.getTime()) > 1000L) {
                String msg = "Original submission file's submissionDate does not match that on server!";
                log.error(msg);
                throw new MetadataUpdateException(msg);
            } else {
                hasSubmissionDate = true;
            }
        }
    }
    if (hasInstanceID && hasSubmissionDate) {
        log.info("submission already has instanceID and submissionDate attributes: " + submissionFile.getAbsolutePath());
        return instanceID;
    }
    if (!hasInstanceID) {
        root.setAttribute("", INSTANCE_ID_ATTRIBUTE_NAME, instanceID);
    }
    if (!hasSubmissionDate) {
        root.setAttribute("", SUBMISSION_DATE_ATTRIBUTE_NAME, submissionDate);
    }
    // and write out the changes...
    // write the file out...
    File revisedFile = new File(submissionFile.getParentFile(), "." + submissionFile.getName());
    try {
        FileOutputStream fos = new FileOutputStream(revisedFile, false);
        KXmlSerializer serializer = new KXmlSerializer();
        serializer.setOutput(fos, "UTF-8");
        originalDoc.write(serializer);
        serializer.flush();
        fos.close();
        // and swap files...
        boolean restoreTemp = false;
        File temp = new File(submissionFile.getParentFile(), ".back." + submissionFile.getName());
        try {
            if (temp.exists()) {
                if (!temp.delete()) {
                    String msg = "Unable to remove temporary submission backup file";
                    log.error(msg);
                    throw new MetadataUpdateException(msg);
                }
            }
            if (!submissionFile.renameTo(temp)) {
                String msg = "Unable to rename submission to temporary submission backup file";
                log.error(msg);
                throw new MetadataUpdateException(msg);
            }
            // recovery is possible...
            restoreTemp = true;
            if (!revisedFile.renameTo(submissionFile)) {
                String msg = "Original submission file could not be updated";
                log.error(msg);
                throw new MetadataUpdateException(msg);
            }
            // we're successful...
            restoreTemp = false;
        } finally {
            if (restoreTemp) {
                if (!temp.renameTo(submissionFile)) {
                    String msg = "Unable to restore submission from temporary submission backup file";
                    log.error(msg);
                    throw new MetadataUpdateException(msg);
                }
            }
        }
    } catch (FileNotFoundException e) {
        String msg = "Temporary submission file could not be opened";
        log.error(msg, e);
        throw new MetadataUpdateException(msg);
    } catch (IOException e) {
        String msg = "Temporary submission file could not be written";
        log.error(msg, e);
        throw new MetadataUpdateException(msg);
    }
    return instanceID;
}
Also used : InputStreamReader(java.io.InputStreamReader) Element(org.kxml2.kdom.Element) FileNotFoundException(java.io.FileNotFoundException) MetadataUpdateException(org.opendatakit.briefcase.model.MetadataUpdateException) IOException(java.io.IOException) Document(org.kxml2.kdom.Document) FileInputStream(java.io.FileInputStream) Date(java.util.Date) KXmlSerializer(org.kxml2.io.KXmlSerializer) KXmlParser(org.kxml2.io.KXmlParser) FileOutputStream(java.io.FileOutputStream) XmlPullParserException(org.xmlpull.v1.XmlPullParserException) MediaFile(org.opendatakit.briefcase.util.ServerFetcher.MediaFile) File(java.io.File)

Aggregations

KXmlParser (org.kxml2.io.KXmlParser)48 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)25 XmlPullParser (org.xmlpull.v1.XmlPullParser)21 IOException (java.io.IOException)19 InputStreamReader (java.io.InputStreamReader)8 StringReader (java.io.StringReader)8 Document (org.kxml2.kdom.Document)6 InputStream (java.io.InputStream)5 URL (java.net.URL)5 FileInputStream (java.io.FileInputStream)4 MalformedURLException (java.net.MalformedURLException)4 File (java.io.File)3 URISyntaxException (java.net.URISyntaxException)3 URLConnection (java.net.URLConnection)3 DocumentImpl (org.apache.harmony.xml.dom.DocumentImpl)3 MetadataUpdateException (org.opendatakit.briefcase.model.MetadataUpdateException)3 XmlAttribute (com.intellij.psi.xml.XmlAttribute)2 XmlFile (com.intellij.psi.xml.XmlFile)2 XmlTag (com.intellij.psi.xml.XmlTag)2 FileNotFoundException (java.io.FileNotFoundException)2