001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements.  See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership.  The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the
007     * "License"); you may not use this file except in compliance
008     * with the License.  You may obtain a copy of the License at
009     *
010     *  http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing,
013     * software distributed under the License is distributed on an
014     * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015     * KIND, either express or implied.  See the License for the
016     * specific language governing permissions and limitations
017     * under the License.
018     */
019    
020    
021    package org.apache.geronimo.system.configuration;
022    
023    import java.io.IOException;
024    import java.io.Reader;
025    import java.io.StringWriter;
026    import java.io.Writer;
027    
028    import javax.xml.bind.JAXBContext;
029    import javax.xml.bind.JAXBElement;
030    import javax.xml.bind.JAXBException;
031    import javax.xml.bind.Marshaller;
032    import javax.xml.bind.Unmarshaller;
033    import javax.xml.parsers.ParserConfigurationException;
034    import javax.xml.stream.XMLInputFactory;
035    import javax.xml.stream.XMLStreamException;
036    import javax.xml.stream.XMLStreamReader;
037    
038    import org.apache.geronimo.system.plugin.model.AttributeType;
039    import org.apache.geronimo.system.plugin.model.AttributesType;
040    import org.apache.geronimo.system.plugin.model.GbeanType;
041    import org.apache.geronimo.system.plugin.model.ModuleType;
042    import org.apache.geronimo.system.plugin.model.ObjectFactory;
043    import org.xml.sax.SAXException;
044    
045    /**
046     * @version $Rev: 706640 $ $Date: 2008-10-21 14:44:05 +0000 (Tue, 21 Oct 2008) $
047     */
048    public class AttributesXmlUtil {
049        public static final XMLInputFactory XMLINPUT_FACTORY = XMLInputFactory.newInstance();
050        public static final JAXBContext ATTRIBUTES_CONTEXT;
051        public static final JAXBContext ATTRIBUTE_CONTEXT;
052        public static final JAXBContext MODULE_CONTEXT;
053        public static final JAXBContext GBEAN_CONTEXT;
054    
055        static {
056            try {
057                ATTRIBUTES_CONTEXT = JAXBContext.newInstance(AttributesType.class);
058                MODULE_CONTEXT = JAXBContext.newInstance(ModuleType.class);
059                GBEAN_CONTEXT = JAXBContext.newInstance(GbeanType.class);
060                ATTRIBUTE_CONTEXT = JAXBContext.newInstance(AttributeType.class);
061            } catch (JAXBException e) {
062                throw new RuntimeException("Could not create jaxb contexts for plugin types");
063            }
064        }
065    
066    
067        public static void writeAttribute(AttributeType metadata, Writer out) throws XMLStreamException, JAXBException {
068            Marshaller marshaller = ATTRIBUTE_CONTEXT.createMarshaller();
069            marshaller.setProperty("jaxb.formatted.output", true);
070            JAXBElement<AttributeType> element = new ObjectFactory().createAttribute(metadata);
071            marshaller.marshal(element, out);
072        }
073        public static String extractAttributeValue(AttributeType attr) throws JAXBException, XMLStreamException {
074            StringWriter sw = new StringWriter();
075            writeAttribute(attr, sw);
076            String s = sw.toString();
077            int start = s.indexOf('>');
078            start = s.indexOf('>', start + 1);
079            int end = s.lastIndexOf('<');
080            if (end < start) {
081                return null;
082            }
083            return s.substring(start + 1, end).trim();
084        }
085    
086        public static void writeAttributes(AttributesType metadata, Writer out) throws XMLStreamException, JAXBException {
087            Marshaller marshaller = ATTRIBUTES_CONTEXT.createMarshaller();
088            marshaller.setProperty("jaxb.formatted.output", true);
089            JAXBElement<AttributesType> element = new ObjectFactory().createAttributes(metadata);
090            marshaller.marshal(element, out);
091        }
092    
093    
094        public static AttributesType loadAttributes(Reader in) throws ParserConfigurationException, IOException, SAXException, JAXBException, XMLStreamException {
095            Unmarshaller unmarshaller = ATTRIBUTES_CONTEXT.createUnmarshaller();
096            XMLStreamReader xmlStream = XMLINPUT_FACTORY.createXMLStreamReader(in);
097            JAXBElement<AttributesType> element = unmarshaller.unmarshal(xmlStream, AttributesType.class);
098            AttributesType pluginList = element.getValue();
099            return pluginList;
100        }
101        public static ModuleType loadModule(Reader in) throws ParserConfigurationException, IOException, SAXException, JAXBException, XMLStreamException {
102            Unmarshaller unmarshaller = MODULE_CONTEXT.createUnmarshaller();
103            XMLStreamReader xmlStream = XMLINPUT_FACTORY.createXMLStreamReader(in);
104            JAXBElement<ModuleType> element = unmarshaller.unmarshal(xmlStream, ModuleType.class);
105            ModuleType pluginList = element.getValue();
106            return pluginList;
107        }
108        public static GbeanType loadGbean(Reader in) throws ParserConfigurationException, IOException, SAXException, JAXBException, XMLStreamException {
109            Unmarshaller unmarshaller = GBEAN_CONTEXT.createUnmarshaller();
110            XMLStreamReader xmlStream = XMLINPUT_FACTORY.createXMLStreamReader(in);
111            JAXBElement<GbeanType> element = unmarshaller.unmarshal(xmlStream, GbeanType.class);
112            GbeanType pluginList = element.getValue();
113            return pluginList;
114        }
115        public static AttributeType loadAttribute(Reader in) throws ParserConfigurationException, IOException, SAXException, JAXBException, XMLStreamException {
116            Unmarshaller unmarshaller = ATTRIBUTE_CONTEXT.createUnmarshaller();
117            XMLStreamReader xmlStream = XMLINPUT_FACTORY.createXMLStreamReader(in);
118            JAXBElement<AttributeType> element = unmarshaller.unmarshal(xmlStream, AttributeType.class);
119            AttributeType attributeType = element.getValue();
120            return attributeType;
121        }
122    
123    /*
124        public static class NamespaceFilter extends XMLFilterImpl {
125            private static String PLUGIN_NS = "http://geronimo.apache.org/xml/ns/plugins-1.3";
126            private static String GBEAN_NS = "http://geronimo.apache.org/xml/ns/attributes-1.2";
127            private static String ENVIRONMENT_NS = "http://geronimo.apache.org/xml/ns/deployment-1.2";
128    
129            private String namespace;
130    
131            public NamespaceFilter(XMLReader xmlReader) {
132                super(xmlReader);
133            }
134    
135            @Override
136            public void startElement(String uri, String localName, String qname, Attributes atts) throws SAXException {
137                if ("plugin-artifact".equals(localName)) {
138                    namespace = PLUGIN_NS;
139                } else if ("gbean".equals(localName)) {
140                    namespace = GBEAN_NS;
141                } else if ("environment".equals(localName)) {
142                    namespace = ENVIRONMENT_NS;
143                }
144                super.startElement(namespace, localName, qname, atts);
145            }
146    
147            @Override
148            public void endElement(String uri, String localName, String qName) throws SAXException {
149                super.endElement(namespace, localName, qName);
150                if ("plugin-artifact".equals(localName)) {
151                    namespace = null;
152                } else if ("gbean".equals(localName)) {
153                    namespace = PLUGIN_NS;
154                } else if ("environment".equals(localName)) {
155                    namespace = GBEAN_NS;
156                }
157            }
158        }
159    */
160    }