001: /*
002: * JBoss, Home of Professional Open Source.
003: * Copyright 2006, Red Hat Middleware LLC, and individual contributors
004: * as indicated by the @author tags. See the copyright.txt file in the
005: * distribution for a full listing of individual contributors.
006: *
007: * This is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as
009: * published by the Free Software Foundation; either version 2.1 of
010: * the License, or (at your option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this software; if not, write to the Free
019: * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
021: */
022: package org.jboss.services.deployment.metadata;
023:
024: import java.beans.PropertyEditor;
025: import java.beans.PropertyEditorManager;
026:
027: import org.jboss.xb.binding.UnmarshallingContext;
028: import org.jboss.xb.binding.GenericObjectModelFactory;
029: import org.xml.sax.Attributes;
030:
031: /**
032: * Class that implements the binding of the XML model
033: * to our POJO classes, using the GenericObjectModelFactory
034: * facility
035: *
036: * @author <a href="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
037: *
038: * @version $Revision: 57210 $
039: */
040: public class ConfigInfoBinding implements GenericObjectModelFactory {
041:
042: // GenericObjectModelFactory implementation ----------------------
043:
044: public Object completeRoot(Object root, UnmarshallingContext ctx,
045: String uri, String name) {
046: return root;
047: }
048:
049: public Object newRoot(Object root, UnmarshallingContext navigator,
050: String namespaceURI, String localName, Attributes attrs) {
051: final ConfigInfo ci;
052: if (root == null) {
053: root = ci = new ConfigInfo();
054: } else {
055: ci = (ConfigInfo) root;
056: }
057:
058: if (attrs.getLength() > 0) {
059: for (int i = 0; i < attrs.getLength(); ++i) {
060: if (attrs.getLocalName(i).equals("copydir")) {
061: ci.setCopydir(attrs.getValue(i));
062: } else if (attrs.getLocalName(i).equals("template")) {
063: ci.setTemplate(attrs.getValue(i));
064: } else if (attrs.getLocalName(i).equals("extension")) {
065: ci.setExtension(attrs.getValue(i));
066: }
067: }
068: }
069: return root;
070: }
071:
072: public Object newChild(Object parent,
073: UnmarshallingContext navigator, String namespaceURI,
074: String localName, Attributes attrs) {
075: Object child = null;
076:
077: if (parent instanceof ConfigInfo) {
078: if ("property".equals(localName)) {
079: PropertyInfo pi = new PropertyInfo();
080: child = pi;
081:
082: if (attrs.getLength() > 0) {
083: for (int i = 0; i < attrs.getLength(); ++i) {
084: if (attrs.getLocalName(i).equals("name")) {
085: pi.setName(attrs.getValue(i));
086: } else if (attrs.getLocalName(i).equals("type")) {
087: pi.setType(attrs.getValue(i));
088: } else if (attrs.getLocalName(i).equals(
089: "optional")) {
090: pi.setOptional(Boolean.valueOf(
091: attrs.getValue(i)).booleanValue());
092: }
093: }
094: }
095: if (pi.getName() == null)
096: throw new RuntimeException(
097: "Missing attribute 'name' for <property> element");
098:
099: // check type if specified, otherwise assume the default
100: String type = pi.getType();
101: if (type == null)
102: pi.setType("java.lang.String");
103: } else if ("template".equals(localName)) {
104: TemplateInfo ti = new TemplateInfo();
105: child = ti;
106:
107: if (attrs.getLength() > 0) {
108: for (int i = 0; i < attrs.getLength(); ++i) {
109: if (attrs.getLocalName(i).equals("input")) {
110: ti.setInput(attrs.getValue(i));
111: } else if (attrs.getLocalName(i).equals(
112: "output")) {
113: ti.setOutput(attrs.getValue(i));
114: }
115: }
116: }
117: // check both attributes are populated
118: if (ti.getInput() == null || ti.getOutput() == null)
119: throw new RuntimeException(
120: "Both 'input' and 'output' attribute must be set for a <template> element");
121: }
122: }
123: return child;
124: }
125:
126: public void addChild(Object parent, Object child,
127: UnmarshallingContext navigator, String namespaceURI,
128: String localName) {
129: if (parent instanceof ConfigInfo) {
130: final ConfigInfo ci = (ConfigInfo) parent;
131: if (child instanceof PropertyInfo) {
132: ci.addPropertyInfo((PropertyInfo) child);
133: } else if (child instanceof TemplateInfo) {
134: ci.addTemplateInfo((TemplateInfo) child);
135: }
136: }
137: }
138:
139: public void setValue(Object o, UnmarshallingContext navigator,
140: String namespaceURI, String localName, String value) {
141: if (o instanceof ConfigInfo) {
142: final ConfigInfo ci = (ConfigInfo) o;
143: if ("description".equals(localName)) {
144: ci.setDescription(value);
145: }
146: } else if (o instanceof PropertyInfo) {
147: PropertyInfo pi = (PropertyInfo) o;
148: if ("description".equals(localName)) {
149: pi.setDescription(value);
150: } else if ("default-value".equals(localName)) {
151: ClassLoader cl = Thread.currentThread()
152: .getContextClassLoader();
153: Class clazz = null;
154: try {
155: clazz = cl.loadClass(pi.getType());
156: } catch (ClassNotFoundException e) {
157: throw new RuntimeException(
158: "Class not found for property '"
159: + pi.getName() + "' of type '"
160: + pi.getType() + "'");
161: }
162: PropertyEditor peditor = PropertyEditorManager
163: .findEditor(clazz);
164:
165: if (peditor != null) {
166: peditor.setAsText(value);
167: pi.setDefaultValue(peditor.getValue());
168: } else
169: throw new RuntimeException(
170: "Property editor not found for property '"
171: + pi.getName() + "' of type '"
172: + pi.getType() + "'");
173: }
174: }
175: }
176:
177: public Object completedRoot(Object root,
178: UnmarshallingContext navigator, String namespaceURI,
179: String localName) {
180: return root;
181: }
182: }
|