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
;
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
;
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);
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
;
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();
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
);
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
);
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.");
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
);
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.");
101 SchemaFactory factory
= SchemaFactory
.newInstance(XMLConstants
.W3C_XML_SCHEMA_NS_URI
);
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
);