/* * Copyright (C) 2010 The Android Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package libcore.xml; import java.io.BufferedInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringReader; import java.io.StringWriter; import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import javax.xml.transform.ErrorListener; import javax.xml.transform.Result; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMResult; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import junit.framework.Assert; import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestCase; import junit.framework.TestSuite; import org.w3c.dom.Attr; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.EntityReference; import org.w3c.dom.NamedNodeMap; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import org.w3c.dom.ProcessingInstruction; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.xmlpull.v1.XmlPullParserException; import org.xmlpull.v1.XmlPullParserFactory; import org.xmlpull.v1.XmlSerializer; /** * The OASIS * XSLT conformance test suite, adapted for use by JUnit. To run these tests * on a device: *
adb shell mkdir /data/oasis ;
* adb push ./XSLT-Conformance-TC /data/oasis.
* catalog.xml file as an argument.
* Unfortunately, some of the tests in the OASIS suite will fail when * executed outside of their original development environment: *
tag
String trimmed = node.getTextContent().trim();
if (trimmed.length() > 0) {
serializer.text(trimmed);
}
} else if (node.getNodeType() == Node.DOCUMENT_NODE) {
Document document = (Document) node;
serializer.startDocument("UTF-8", true);
emitNode(serializer, document.getDocumentElement());
serializer.endDocument();
} else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
ProcessingInstruction processingInstruction = (ProcessingInstruction) node;
String data = processingInstruction.getData();
String target = processingInstruction.getTarget();
serializer.processingInstruction(target + " " + data);
} else if (node.getNodeType() == Node.COMMENT_NODE) {
// ignore!
} else if (node.getNodeType() == Node.ENTITY_REFERENCE_NODE) {
EntityReference entityReference = (EntityReference) node;
serializer.entityRef(entityReference.getNodeName());
} else {
throw new UnsupportedOperationException(
"Cannot emit " + node + " of type " + node.getNodeType());
}
}
private void emitAttributes(XmlSerializer serializer, Node node)
throws IOException {
NamedNodeMap map = node.getAttributes();
if (map == null) {
return;
}
List attributes = new ArrayList();
for (int i = 0; i < map.getLength(); i++) {
attributes.add((Attr) map.item(i));
}
Collections.sort(attributes, orderByName);
for (Attr attr : attributes) {
if ("xmlns".equals(attr.getPrefix()) || "xmlns".equals(attr.getLocalName())) {
/*
* Omit namespace declarations because they aren't considered
* data. Ie. is semantically
* equal to since the
* prefix doesn't matter, only the URI it points to.
*
* When we omit the prefix, our XML serializer will still
* generate one for us, using a predictable pattern.
*/
} else {
serializer.attribute(attr.getNamespaceURI(), attr.getLocalName(), attr.getValue());
}
}
}
private void emitChildren(XmlSerializer serializer, Node node)
throws IOException {
NodeList childNodes = node.getChildNodes();
for (int i = 0; i < childNodes.getLength(); i++) {
emitNode(serializer, childNodes.item(i));
}
}
private static List elementsOf(NodeList nodeList) {
List result = new ArrayList();
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node instanceof Element) {
result.add((Element) node);
}
}
return result;
}
/**
* Reads the given file into a string. If the file contains a byte order
* mark, the corresponding character set will be used. Otherwise the system
* default charset will be used.
*/
private String fileToString(File file) throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(file), 1024);
// Read the byte order mark to determine the charset.
// TODO: use a built-in API for this...
Reader reader;
in.mark(3);
int byte1 = in.read();
int byte2 = in.read();
if (byte1 == 0xFF && byte2 == 0xFE) {
reader = new InputStreamReader(in, "UTF-16LE");
} else if (byte1 == 0xFF && byte2 == 0xFF) {
reader = new InputStreamReader(in, "UTF-16BE");
} else {
int byte3 = in.read();
if (byte1 == 0xEF && byte2 == 0xBB && byte3 == 0xBF) {
reader = new InputStreamReader(in, "UTF-8");
} else {
in.reset();
reader = new InputStreamReader(in);
}
}
StringWriter out = new StringWriter();
char[] buffer = new char[1024];
int count;
while ((count = reader.read(buffer)) != -1) {
out.write(buffer, 0, count);
}
in.close();
return out.toString();
}
static class ErrorRecorder implements ErrorListener {
Exception warning;
Exception error;
public void warning(TransformerException exception) {
if (this.warning == null) {
this.warning = exception;
}
}
public void error(TransformerException exception) {
if (this.error == null) {
this.error = exception;
}
}
public void fatalError(TransformerException exception) {
if (this.error == null) {
this.error = exception;
}
}
}
}