001    /**
002     *  Licensed to the Apache Software Foundation (ASF) under one or more
003     *  contributor license agreements.  See the NOTICE file distributed with
004     *  this work for additional information regarding copyright ownership.
005     *  The ASF licenses this file to You under the Apache License, Version 2.0
006     *  (the "License"); you may not use this file except in compliance with
007     *  the License.  You may obtain a copy of the License at
008     *
009     *     http://www.apache.org/licenses/LICENSE-2.0
010     *
011     *  Unless required by applicable law or agreed to in writing, software
012     *  distributed under the License is distributed on an "AS IS" BASIS,
013     *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     *  See the License for the specific language governing permissions and
015     *  limitations under the License.
016     */
017    
018    package org.apache.geronimo.deployment.plugin;
019    
020    import java.beans.PropertyChangeListener;
021    import java.beans.PropertyChangeSupport;
022    import java.io.IOException;
023    import java.io.InputStream;
024    import java.io.OutputStream;
025    
026    import org.apache.xmlbeans.SchemaTypeLoader;
027    import org.apache.xmlbeans.XmlException;
028    import org.apache.xmlbeans.XmlObject;
029    import org.apache.xmlbeans.XmlOptions;
030    
031    /**
032     *
033     *
034     * @version $Rev: 476049 $ $Date: 2006-11-16 23:35:17 -0500 (Thu, 16 Nov 2006) $
035     */
036    public abstract class XmlBeanSupport { // should implement Serializable or Externalizable
037        protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
038        private XmlObject xmlObject;
039    
040        public XmlBeanSupport(XmlObject xmlObject) {
041            this.xmlObject = xmlObject;
042        }
043    
044        protected void setXmlObject(XmlObject xmlObject) {
045            this.xmlObject = xmlObject;
046        }
047    
048        protected XmlObject getXmlObject() {
049            return xmlObject;
050        }
051    
052        public void addPropertyChangeListener(PropertyChangeListener pcl) {
053            pcs.addPropertyChangeListener(pcl);
054        }
055    
056        public void removePropertyChangeListener(PropertyChangeListener pcl) {
057            pcs.removePropertyChangeListener(pcl);
058        }
059    
060        public void toXML(OutputStream outputStream) throws IOException {
061            XmlOptions options = new XmlOptions();
062            options.setSavePrettyPrint();
063            options.setSavePrettyPrintIndent(4);
064            options.setUseDefaultNamespace();
065            xmlObject.save(outputStream, options);
066        }
067    
068        public void fromXML(InputStream inputStream) throws XmlException, IOException {
069            xmlObject = getSchemaTypeLoader().parse(inputStream, null, null);
070        }
071    
072        //override unless the particular object can never be read directly from xml, such as the
073        //connector ConnectionDefinitionInstance.
074        protected SchemaTypeLoader getSchemaTypeLoader() {
075            return null;
076        }
077    
078        // Must be public but should not be a JavaBean property -- sigh
079        public boolean configured() {
080            return getXmlObject() != null;
081        }
082    
083        protected static boolean isEmpty(String s) {
084            return s == null || s.trim().equals("");
085        }
086    }