View Javadoc

1   package net.sf.cantina.util;
2   
3   import net.sf.cantina.exceptions.InvalidXmlException;
4   import net.sf.cantina.Document;
5   import org.apache.log4j.Logger;
6   import org.dom4j.DocumentFactory;
7   import org.dom4j.DocumentHelper;
8   import org.dom4j.DocumentType;
9   import org.dom4j.io.SAXReader;
10  import org.xml.sax.InputSource;
11  import org.xml.sax.SAXParseException;
12  
13  import javax.xml.parsers.SAXParser;
14  import javax.xml.parsers.SAXParserFactory;
15  import java.io.StringReader;
16  import java.util.ArrayList;
17  import java.util.List;
18  
19  /***
20   * @author Stephane JAIS
21   */
22  public abstract class XmlHelper
23  {
24    private static final Logger logger = Logger.getLogger(XmlHelper.class);
25  
26    public static void checkXml(String xmlString, boolean validate)
27      throws Exception
28    {
29      List errors = new ArrayList();
30      try
31      {
32        SAXParserFactory factory;
33        SAXParser parser;
34        factory = SAXParserFactory.newInstance();
35        factory.setValidating(validate);
36        parser = factory.newSAXParser();
37        parser.parse(new InputSource(new StringReader(xmlString)),
38          new SAXDefaultHandler(errors));
39      } catch (SAXParseException e)
40      {
41        InvalidXmlException e1 = new InvalidXmlException("Content has XML errors.",
42          (SAXParseException[]) errors.toArray(new SAXParseException[]{}));
43        for (int i = 1; i <= e1.getErrors().length; i++)
44          logger.debug("Error #" + i, e1);
45        throw e1;
46      }
47    }
48  
49    public static String surroundTextWithRoot(String text,
50                                              String rootElementName,
51                                              String publicId,
52                                              String system)
53      throws Exception
54    {
55      SAXReader reader = new SAXReader();
56      StringBuffer sb = new StringBuffer();
57      reader.setEntityResolver(new SAXDefaultHandler(new ArrayList(), sb));
58      DocumentFactory df = reader.getDocumentFactory();
59      DocumentType docType = df.createDocType(rootElementName, publicId, system);
60      org.dom4j.Document doc;
61      doc = DocumentHelper.parseText(
62        "<" + rootElementName + ">" +
63        text +
64        "</" + rootElementName + ">");
65      doc.setDocType(docType);
66      return doc.asXML();
67    }
68  
69  
70    public static String stripXml(String content)
71      throws Exception
72    {
73      SAXReader reader = new SAXReader();
74      reader.setEntityResolver(new SAXDefaultHandler(new ArrayList()));
75      reader.setStripWhitespaceText(true);
76      org.dom4j.Document doc = reader.read(new StringReader(content));
77      return doc.getStringValue();
78    }
79  
80    public static String escapeXml(String content)
81    {
82      StringBuffer result = new StringBuffer();
83      for (int i = 0; i < content.length(); i++)
84      {
85        char ch = content.charAt(i);
86        if (ch == '\'')
87          result.append("&apos;");
88        else if (ch == '"')
89          result.append("&quot;");
90        else if (ch == '<')
91          result.append("&lt;");
92        else if (ch == '>')
93          result.append("&gt;");
94        else if (ch == '&')
95          result.append("&amp;");
96        else
97          result.append(ch);
98      }
99      return result.toString();
100   }
101 }