View Javadoc

1   /**
2    *
3    * Copyright 2003-2004 The Apache Software Foundation
4    *
5    *  Licensed under the Apache License, Version 2.0 (the "License");
6    *  you may not use this file except in compliance with the License.
7    *  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   *  Unless required by applicable law or agreed to in writing, software
12   *  distributed under the License is distributed on an "AS IS" BASIS,
13   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   *  See the License for the specific language governing permissions and
15   *  limitations under the License.
16   */
17  
18  package org.apache.geronimo.deployment.plugin;
19  
20  import java.beans.PropertyChangeListener;
21  import java.beans.PropertyChangeSupport;
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.io.OutputStream;
25  
26  import org.apache.xmlbeans.SchemaTypeLoader;
27  import org.apache.xmlbeans.XmlException;
28  import org.apache.xmlbeans.XmlObject;
29  import org.apache.xmlbeans.XmlOptions;
30  
31  /**
32   *
33   *
34   * @version $Rev: 406010 $ $Date: 2006-05-12 21:06:15 -0700 (Fri, 12 May 2006) $
35   */
36  public abstract class XmlBeanSupport { // should implement Serializable or Externalizable
37      protected final PropertyChangeSupport pcs = new PropertyChangeSupport(this);
38      private XmlObject xmlObject;
39  
40      public XmlBeanSupport(XmlObject xmlObject) {
41          this.xmlObject = xmlObject;
42      }
43  
44      protected void setXmlObject(XmlObject xmlObject) {
45          this.xmlObject = xmlObject;
46      }
47  
48      protected XmlObject getXmlObject() {
49          return xmlObject;
50      }
51  
52      public void addPropertyChangeListener(PropertyChangeListener pcl) {
53          pcs.addPropertyChangeListener(pcl);
54      }
55  
56      public void removePropertyChangeListener(PropertyChangeListener pcl) {
57          pcs.removePropertyChangeListener(pcl);
58      }
59  
60      public void toXML(OutputStream outputStream) throws IOException {
61          XmlOptions options = new XmlOptions();
62          options.setSavePrettyPrint();
63          options.setSavePrettyPrintIndent(4);
64          options.setUseDefaultNamespace();
65          xmlObject.save(outputStream, options);
66      }
67  
68      public void fromXML(InputStream inputStream) throws XmlException, IOException {
69          xmlObject = getSchemaTypeLoader().parse(inputStream, null, null);
70      }
71  
72      //override unless the particular object can never be read directly from xml, such as the
73      //connector ConnectionDefinitionInstance.
74      protected SchemaTypeLoader getSchemaTypeLoader() {
75          return null;
76      }
77  
78      // Must be public but should not be a JavaBean property -- sigh
79      public boolean configured() {
80          return getXmlObject() != null;
81      }
82  
83      protected static boolean isEmpty(String s) {
84          return s == null || s.trim().equals("");
85      }
86  }