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);
}
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);
}
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);
}
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;
}
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;
}
Aggregations