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