01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.geronimo.deployment.plugin;
17:
18: import java.beans.PropertyChangeListener;
19: import java.beans.PropertyChangeSupport;
20: import java.io.IOException;
21: import java.io.InputStream;
22: import java.io.OutputStream;
23:
24: import org.apache.xmlbeans.SchemaTypeLoader;
25: import org.apache.xmlbeans.XmlException;
26: import org.apache.xmlbeans.XmlObject;
27: import org.apache.xmlbeans.XmlOptions;
28:
29: /**
30: *
31: *
32: * @version $Rev: 476049 $ $Date: 2006-11-16 20:35:17 -0800 (Thu, 16 Nov 2006) $
33: */
34: public abstract class XmlBeanSupport { // should implement Serializable or Externalizable
35: protected final PropertyChangeSupport pcs = new PropertyChangeSupport(
36: this );
37: private XmlObject xmlObject;
38:
39: public XmlBeanSupport(XmlObject xmlObject) {
40: this .xmlObject = xmlObject;
41: }
42:
43: protected void setXmlObject(XmlObject xmlObject) {
44: this .xmlObject = xmlObject;
45: }
46:
47: protected XmlObject getXmlObject() {
48: return xmlObject;
49: }
50:
51: public void addPropertyChangeListener(PropertyChangeListener pcl) {
52: pcs.addPropertyChangeListener(pcl);
53: }
54:
55: public void removePropertyChangeListener(PropertyChangeListener pcl) {
56: pcs.removePropertyChangeListener(pcl);
57: }
58:
59: public void toXML(OutputStream outputStream) throws IOException {
60: XmlOptions options = new XmlOptions();
61: options.setSavePrettyPrint();
62: options.setSavePrettyPrintIndent(4);
63: options.setUseDefaultNamespace();
64: xmlObject.save(outputStream, options);
65: }
66:
67: public void fromXML(InputStream inputStream) throws XmlException,
68: IOException {
69: xmlObject = getSchemaTypeLoader()
70: .parse(inputStream, null, null);
71: }
72:
73: //override unless the particular object can never be read directly from xml, such as the
74: //connector ConnectionDefinitionInstance.
75: protected SchemaTypeLoader getSchemaTypeLoader() {
76: return null;
77: }
78:
79: // Must be public but should not be a JavaBean property -- sigh
80: public boolean configured() {
81: return getXmlObject() != null;
82: }
83:
84: protected static boolean isEmpty(String s) {
85: return s == null || s.trim().equals("");
86: }
87: }
|