use of org.kxml2.io.KXmlParser in project briefcase by opendatakit.
the class AggregateUtils method httpRetrieveXmlDocument.
/**
* Common method for returning a parsed xml document given a url and the http
* context and client objects involved in the web connection. The document is
* the body of the response entity and should be xml.
*
* @return
*/
private static final DocumentFetchResult httpRetrieveXmlDocument(HttpUriRequest request, int[] validStatusList, ServerConnectionInfo serverInfo, boolean alwaysResetCredentials, DocumentDescription description, ResponseAction action) throws XmlDocumentFetchException {
HttpClient httpClient = WebUtils.createHttpClient();
// get shared HttpContext so that authentication and cookies are retained.
HttpClientContext localContext = WebUtils.getHttpContext();
URI uri = request.getURI();
log.info("Attempting URI {}", uri);
WebUtils.setCredentials(localContext, serverInfo, uri, alwaysResetCredentials);
if (description.isCancelled()) {
throw new XmlDocumentFetchException("Transfer of " + description.getDocumentDescriptionType() + " aborted.");
}
HttpResponse response = null;
try {
response = httpClient.execute(request, localContext);
int statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
String lcContentType = (entity == null) ? null : entity.getContentType().getValue().toLowerCase();
XmlDocumentFetchException ex = null;
boolean statusCodeValid = false;
for (int i : validStatusList) {
if (i == statusCode) {
statusCodeValid = true;
break;
}
}
if (!statusCodeValid) {
String webError = response.getStatusLine().getReasonPhrase() + " (" + statusCode + ")";
if (statusCode == 401) {
// We reset the Http context to force next request to authenticate itself
WebUtils.resetHttpContext();
ex = new XmlDocumentFetchException("Authentication failure");
} else if (statusCode == 400) {
ex = new XmlDocumentFetchException(description.getFetchDocFailed() + webError + " while accessing: " + uri.toString() + "\nPlease verify that the " + description.getDocumentDescriptionType() + " that is being uploaded is well-formed.");
} else {
ex = new XmlDocumentFetchException(description.getFetchDocFailed() + webError + " while accessing: " + uri.toString() + "\nPlease verify that the URL, your user credentials and your permissions are all correct.");
}
} else if (entity == null) {
log.warn("No entity body returned");
ex = new XmlDocumentFetchException(description.getFetchDocFailed() + " Server unexpectedly returned no content");
} else if (!(lcContentType.contains(HTTP_CONTENT_TYPE_TEXT_XML) || lcContentType.contains(HTTP_CONTENT_TYPE_APPLICATION_XML))) {
log.warn("Wrong ContentType: " + entity.getContentType().getValue() + "returned");
ex = new XmlDocumentFetchException(description.getFetchDocFailed() + "A non-XML document was returned. A network login screen may be interfering with the transmission to the server.");
}
if (ex != null) {
flushEntityBytes(entity);
// and throw the exception...
throw ex;
}
// parse the xml document...
Document doc = null;
try {
InputStream is = null;
InputStreamReader isr = null;
try {
is = entity.getContent();
isr = new InputStreamReader(is, "UTF-8");
doc = new Document();
KXmlParser parser = new KXmlParser();
parser.setInput(isr);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
doc.parse(parser);
isr.close();
} finally {
if (isr != null) {
try {
isr.close();
} catch (Exception e) {
// no-op
}
}
if (is != null) {
try {
is.close();
} catch (Exception e) {
// no-op
}
}
}
} catch (Exception e) {
log.warn("Parsing failed with " + e.getMessage(), e);
throw new XmlDocumentFetchException(description.getFetchDocFailed());
}
// examine header fields...
// is it an OpenRosa server?
boolean isOR = false;
Header[] fields = response.getHeaders(WebUtils.OPEN_ROSA_VERSION_HEADER);
if (fields != null && fields.length >= 1) {
isOR = true;
boolean versionMatch = false;
boolean first = true;
StringBuilder b = new StringBuilder();
for (Header h : fields) {
if (WebUtils.OPEN_ROSA_VERSION.equals(h.getValue())) {
versionMatch = true;
break;
}
if (!first) {
b.append("; ");
}
first = false;
b.append(h.getValue());
}
if (!versionMatch) {
log.warn(WebUtils.OPEN_ROSA_VERSION_HEADER + " unrecognized version(s): " + b);
}
}
// what about location?
Header[] locations = response.getHeaders("Location");
if (locations != null && locations.length == 1) {
try {
URL url = new URL(locations[0].getValue());
URI uNew = url.toURI();
log.info("Redirection to URI {}", uNew);
if (uri.getHost().equalsIgnoreCase(uNew.getHost())) {
// trust the server to tell us a new location
// ... and possibly to use https instead.
String fullUrl = url.toExternalForm();
int idx = fullUrl.lastIndexOf("/");
serverInfo.setUrl(fullUrl.substring(0, idx));
} else {
// Don't follow a redirection attempt to a different host.
// We can't tell if this is a spoof or not.
String msg = description.getFetchDocFailed() + "Unexpected redirection attempt";
log.warn(msg);
throw new XmlDocumentFetchException(msg);
}
} catch (MalformedURLException | URISyntaxException e) {
String msg = description.getFetchDocFailed() + "Unexpected exception: " + e.getMessage();
log.warn(msg, e);
throw new XmlDocumentFetchException(msg);
}
}
DocumentFetchResult result = new DocumentFetchResult(doc, isOR);
if (action != null) {
action.doAction(result);
}
return result;
} catch (UnknownHostException e) {
String msg = description.getFetchDocFailed() + "Unknown host";
log.warn(msg, e);
throw new XmlDocumentFetchException(msg);
} catch (IOException | MetadataUpdateException e) {
String msg = description.getFetchDocFailed() + "Unexpected exception: " + e;
log.warn(msg, e);
throw new XmlDocumentFetchException(msg);
}
}
use of org.kxml2.io.KXmlParser in project javarosa by opendatakit.
the class XFormParser method getXMLDocument.
/**
* Uses xkml to parse the provided XML content, and then consolidates text elements.
*
* @param reader the XML content provider
* @param stringCache an optional string cache, whose presence will cause the use of
* {@link InterningKXmlParser} rather than {@link KXmlParser}.
* @return the parsed document
* @throws IOException
* @deprecated The InterningKXmlParser is not used.
*/
@Deprecated
public static Document getXMLDocument(Reader reader, CacheTable<String> stringCache) throws IOException {
final CodeTimer ctParse = new CodeTimer("Reading XML and parsing with kXML2");
Document doc = new Document();
try {
KXmlParser parser;
if (stringCache != null) {
parser = new InterningKXmlParser(stringCache);
} else {
parser = new KXmlParser();
}
parser.setInput(reader);
parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, true);
doc.parse(parser);
} catch (XmlPullParserException e) {
String errorMsg = "XML Syntax Error at Line: " + e.getLineNumber() + ", Column: " + e.getColumnNumber() + "!";
Std.err.println(errorMsg);
Std.printStack(e);
throw new XFormParseException(errorMsg);
} catch (IOException e) {
// CTS - 12/09/2012 - Stop swallowing IO Exceptions
throw e;
} catch (Exception e) {
// #if debug.output==verbose || debug.output==exception
String errorMsg = "Unhandled Exception while Parsing XForm";
Std.err.println(errorMsg);
Std.printStack(e);
throw new XFormParseException(errorMsg);
// #endif
}
try {
reader.close();
} catch (IOException e) {
Std.out.println("Error closing reader");
Std.printStack(e);
}
ctParse.logDone();
final CodeTimer ctConsolidate = new CodeTimer("Consolidating text");
XmlTextConsolidator.consolidateText(stringCache, doc.getRootElement());
ctConsolidate.logDone();
return doc;
}
use of org.kxml2.io.KXmlParser 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.io.KXmlParser in project javarosa by opendatakit.
the class ExternalDataInstance method buildFromPath.
public static ExternalDataInstance buildFromPath(String path, String instanceId) throws IOException, UnfullfilledRequirementsException, XmlPullParserException, InvalidStructureException {
String absolutePath = getPathPrefix() + path;
KXmlParser xmlParser = ElementParser.instantiateParser(new FileInputStream(absolutePath));
TreeElementParser treeElementParser = new TreeElementParser(xmlParser, 0, instanceId);
TreeElement root = treeElementParser.parse();
return new ExternalDataInstance(path, instanceId, root);
}
use of org.kxml2.io.KXmlParser in project android-shape-imageview by siyamed.
the class SvgToPath method parse.
private static PathInfo parse(InputStream in, boolean ignoreDefs, float dpi) {
try {
XmlPullParser xr = new KXmlParser();
SvgToPath svgHandler = new SvgToPath(xr);
svgHandler.setDpi(dpi);
if (ignoreDefs) {
xr.setInput(new InputStreamReader(in));
svgHandler.processSvg();
} else {
CopyInputStream cin = new CopyInputStream(in);
XmlPullParser ids = new KXmlParser();
ids.setInput(new InputStreamReader(cin.getCopy()));
IdHandler idHandler = new IdHandler(ids);
idHandler.processIds();
svgHandler.idXml = idHandler.idXml;
xr.setInput(new InputStreamReader(cin.getCopy()));
svgHandler.processSvg();
}
return svgHandler.pathInfo;
} catch (Exception e) {
Log.w(TAG, "Parse error: " + e);
throw new RuntimeException(e);
}
}
Aggregations