App Engine Java SDK version 1.9.25
[gae.git] / java / src / main / com / google / apphosting / utils / config / XmlUtils.java
blob1c7ef7e12e54ad7fade8b4eba0d2eb696010bcb4
1 package com.google.apphosting.utils.config;
3 import com.google.common.base.Charsets;
4 import com.google.common.io.Files;
6 import org.mortbay.xml.XmlParser;
7 import org.mortbay.xml.XmlParser.Node;
8 import org.xml.sax.SAXException;
10 import java.io.ByteArrayInputStream;
11 import java.io.File;
12 import java.io.IOException;
13 import java.io.InputStream;
14 import java.util.logging.Level;
15 import java.util.logging.Logger;
17 import javax.xml.XMLConstants;
18 import javax.xml.transform.stream.StreamSource;
19 import javax.xml.validation.SchemaFactory;
21 /**
22 * Utility functions for processing XML.
24 public class XmlUtils {
25 private static final Logger logger = Logger.getLogger(XmlUtils.class.getName());
27 static String getText(XmlParser.Node node) throws AppEngineConfigException{
28 Object child = node.get(0);
29 String value;
30 if (child == null) {
31 value = "";
32 } else {
33 if (!(child instanceof String)) {
34 String msg = "Invalid XML: String content expected in node '" + node.getTag() + "'.";
35 logger.log(Level.SEVERE, msg);
36 throw new AppEngineConfigException(msg);
38 value = (String) child;
41 return value.trim();
44 /**
45 * Parses the input stream and returns the {@link Node} for the root element.
47 * @throws AppEngineConfigException If the input stream cannot be parsed.
49 static Node parse(InputStream is) {
50 XmlParser xmlParser = new XmlParser();
51 try {
52 return xmlParser.parse(is);
53 } catch (IOException e) {
54 String msg = "Received IOException parsing the input stream.";
55 logger.log(Level.SEVERE, msg, e);
56 throw new AppEngineConfigException(msg, e);
57 } catch (SAXException e) {
58 String msg = "Received SAXException parsing the input stream.";
59 logger.log(Level.SEVERE, msg, e);
60 throw new AppEngineConfigException(msg, e);
64 /**
65 * Validates a given XML document against a given schema.
67 * @param xmlFilename filename with XML document.
68 * @param schema XSD schema to validate with.
70 * @throws AppEngineConfigException for malformed XML, or IO errors
72 public static void validateXml(String xmlFilename, File schema) {
73 File xml = new File(xmlFilename);
74 if (!xml.exists()) {
75 throw new AppEngineConfigException("Xml file: " + xml.getPath() + " does not exist.");
77 if (!schema.exists()) {
78 throw new AppEngineConfigException("Schema file: " + schema.getPath() + " does not exist.");
80 try {
81 validateXmlContent(Files.toString(xml, Charsets.UTF_8), schema);
82 } catch (IOException ex) {
83 throw new AppEngineConfigException(
84 "IO error validating " + xmlFilename + " against " + schema.getPath(), ex);
88 /**
89 * Validates a given XML document against a given schema.
91 * @param content a String containing the entire XML to validate.
92 * @param schema XSD schema to validate with.
94 * @throws AppEngineConfigException for malformed XML, or IO errors
96 public static void validateXmlContent(String content, File schema) {
97 if (!schema.exists()) {
98 throw new AppEngineConfigException("Schema file: " + schema.getPath() + " does not exist.");
100 try {
101 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
102 try {
103 factory
104 .newSchema(schema)
105 .newValidator()
106 .validate(new StreamSource(new ByteArrayInputStream(content.getBytes(Charsets.UTF_8))));
107 } catch (SAXException ex) {
108 throw new AppEngineConfigException(
109 "XML error validating " + content + " against " + schema.getPath(), ex);
111 } catch (IOException ex) {
112 throw new AppEngineConfigException(
113 "IO error validating " + content + " against " + schema.getPath(), ex);