|
| 1 | +package com.ocpsoft.pretty.faces.config; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.InputStream; |
| 5 | + |
| 6 | +import javax.xml.bind.JAXBContext; |
| 7 | +import javax.xml.bind.JAXBElement; |
| 8 | +import javax.xml.bind.JAXBException; |
| 9 | +import javax.xml.bind.Unmarshaller; |
| 10 | +import javax.xml.parsers.ParserConfigurationException; |
| 11 | +import javax.xml.parsers.SAXParserFactory; |
| 12 | +import javax.xml.transform.sax.SAXSource; |
| 13 | + |
| 14 | +import org.xml.sax.Attributes; |
| 15 | +import org.xml.sax.InputSource; |
| 16 | +import org.xml.sax.SAXException; |
| 17 | +import org.xml.sax.XMLFilter; |
| 18 | +import org.xml.sax.XMLReader; |
| 19 | +import org.xml.sax.helpers.XMLFilterImpl; |
| 20 | + |
| 21 | +import com.ocpsoft.pretty.faces.config.types.PrettyConfigElement; |
| 22 | + |
| 23 | +/** |
| 24 | + * Implementation of {@link PrettyConfigParser} that uses the JAXB API to parse |
| 25 | + * the PrettyFaces configuration. |
| 26 | + * |
| 27 | + * @author Christian Kaltepoth <christian@kaltepoth.de> |
| 28 | + */ |
| 29 | +public class JAXBPrettyConfigParser implements PrettyConfigParser |
| 30 | +{ |
| 31 | + |
| 32 | + private JAXBContext jaxbContext; |
| 33 | + |
| 34 | + private SAXParserFactory saxParserFactory; |
| 35 | + |
| 36 | + /** |
| 37 | + * Constructor |
| 38 | + */ |
| 39 | + public JAXBPrettyConfigParser() |
| 40 | + { |
| 41 | + try |
| 42 | + { |
| 43 | + // build JAXBContext using the package we generate the classes to |
| 44 | + jaxbContext = JAXBContext.newInstance("com.ocpsoft.pretty.faces.config.types"); |
| 45 | + |
| 46 | + // create a namespace aware SAXParserFactory |
| 47 | + saxParserFactory = SAXParserFactory.newInstance(); |
| 48 | + saxParserFactory.setNamespaceAware(true); |
| 49 | + |
| 50 | + } |
| 51 | + catch (JAXBException e) |
| 52 | + { |
| 53 | + throw new IllegalStateException(e); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /* |
| 58 | + * @see com.ocpsoft.pretty.faces.config.PrettyConfigParser#parse(com.ocpsoft.pretty.faces.config.PrettyConfigBuilder, java.io.InputStream) |
| 59 | + */ |
| 60 | + public void parse(PrettyConfigBuilder builder, InputStream resource) throws IOException, SAXException |
| 61 | + { |
| 62 | + |
| 63 | + try |
| 64 | + { |
| 65 | + |
| 66 | + // create the JAXB unmarshaller |
| 67 | + Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); |
| 68 | + |
| 69 | + // use a special XMLFilter to do the namespace replacement |
| 70 | + XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader(); |
| 71 | + XMLFilter namespaceFilter = new NamespaceFilter(xmlReader); |
| 72 | + xmlReader.setContentHandler(unmarshaller.getUnmarshallerHandler()); |
| 73 | + SAXSource source = new SAXSource(namespaceFilter, new InputSource(resource)); |
| 74 | + |
| 75 | + // parse the document and get the PrettyConfigElement |
| 76 | + JAXBElement<?> e = (JAXBElement<?>) unmarshaller.unmarshal(source); |
| 77 | + PrettyConfigElement prettyConfigElement = (PrettyConfigElement) e.getValue(); |
| 78 | + |
| 79 | + // build the PrettyConfig and append it to the builder |
| 80 | + PrettyConfig config = new PrettyConfig(prettyConfigElement); |
| 81 | + builder.addFromConfig(config); |
| 82 | + |
| 83 | + } |
| 84 | + catch (JAXBException e) |
| 85 | + { |
| 86 | + throw new IOException(e); |
| 87 | + } |
| 88 | + catch (ParserConfigurationException e) |
| 89 | + { |
| 90 | + throw new IOException(e); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * This class is used to replace older PrettyFaces namespace URIs with the |
| 96 | + * most current one. This is required so that the JAXB classes generated by |
| 97 | + * the most current schema work correctly. |
| 98 | + * |
| 99 | + * @author Christian Kaltepoth <christian@kaltepoth.de> |
| 100 | + */ |
| 101 | + private class NamespaceFilter extends XMLFilterImpl |
| 102 | + { |
| 103 | + |
| 104 | + /** |
| 105 | + * The prefix of namespaces that have to be replaced |
| 106 | + */ |
| 107 | + private final static String EXPECTED_URI_PREFIX = "http://ocpsoft.com/prettyfaces/"; |
| 108 | + |
| 109 | + /** |
| 110 | + * The most current namespace |
| 111 | + */ |
| 112 | + private final static String CURRENT_NAMESPACE_URI = "http://ocpsoft.com/prettyfaces/3.2.1"; |
| 113 | + |
| 114 | + public NamespaceFilter(XMLReader reader) |
| 115 | + { |
| 116 | + super(reader); |
| 117 | + } |
| 118 | + |
| 119 | + public void startPrefixMapping(String prefix, String uri) throws SAXException |
| 120 | + { |
| 121 | + super.startPrefixMapping(prefix, replaceNamespace(uri)); |
| 122 | + } |
| 123 | + |
| 124 | + public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException |
| 125 | + { |
| 126 | + |
| 127 | + super.startElement(replaceNamespace(uri), localName, qName, attributes); |
| 128 | + } |
| 129 | + |
| 130 | + public void endElement(String uri, String localName, String qName) throws SAXException |
| 131 | + { |
| 132 | + super.endElement(replaceNamespace(uri), localName, qName); |
| 133 | + } |
| 134 | + |
| 135 | + private String replaceNamespace(String uri) |
| 136 | + { |
| 137 | + if (uri != null) |
| 138 | + { |
| 139 | + return uri.startsWith(EXPECTED_URI_PREFIX) ? CURRENT_NAMESPACE_URI : uri; |
| 140 | + } |
| 141 | + return null; |
| 142 | + } |
| 143 | + |
| 144 | + } |
| 145 | + |
| 146 | +} |
0 commit comments