KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > services > deployment > metadata > ConfigInfoBinding


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2005, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.services.deployment.metadata;
23
24 import java.beans.PropertyEditor JavaDoc;
25 import java.beans.PropertyEditorManager JavaDoc;
26
27 import org.jboss.xb.binding.GenericObjectModelFactory;
28 import org.jboss.xb.binding.UnmarshallingContext;
29 import org.xml.sax.Attributes JavaDoc;
30
31 /**
32  * Class that implements the binding of the XML model
33  * to our POJO classes, using the GenericObjectModelFactory
34  * facility
35  *
36  * @author <a HREF="mailto:dimitris@jboss.org">Dimitris Andreadis</a>
37  *
38  * @version $Revision: 37459 $
39  */

40 public class ConfigInfoBinding
41    implements GenericObjectModelFactory
42 {
43          
44    // ObjectModelFactory implementation -----------------------------
45

46    public Object JavaDoc newRoot(Object JavaDoc root, UnmarshallingContext ctx, String JavaDoc namespaceURI,
47                          String JavaDoc localName, Attributes JavaDoc attrs)
48    {
49       final ConfigInfo ci;
50       if(root == null)
51       {
52          root = ci = new ConfigInfo();
53       }
54       else
55       {
56          ci = (ConfigInfo) root;
57       }
58
59       if(attrs.getLength() > 0)
60       {
61          for(int i = 0; i < attrs.getLength(); ++i)
62          {
63             if (attrs.getLocalName(i).equals("copydir"))
64             {
65                ci.setCopydir(attrs.getValue(i));
66             }
67             else if (attrs.getLocalName(i).equals("template"))
68             {
69                ci.setTemplate(attrs.getValue(i));
70             }
71             else if (attrs.getLocalName(i).equals("extension"))
72             {
73                ci.setExtension(attrs.getValue(i));
74             }
75          }
76       }
77       return root;
78    }
79    
80    public Object JavaDoc completeRoot(Object JavaDoc root, UnmarshallingContext ctx, String JavaDoc namespaceURI, String JavaDoc localName)
81    {
82       return root;
83    }
84    
85    // GenericObjectModelFactory implementation ----------------------
86

87    public Object JavaDoc newChild(Object JavaDoc parent, UnmarshallingContext ctx, String JavaDoc namespaceURI,
88                           String JavaDoc localName, Attributes JavaDoc attrs)
89    {
90       Object JavaDoc child = null;
91
92       if (parent instanceof ConfigInfo)
93       {
94          if("property".equals(localName))
95          {
96             PropertyInfo pi = new PropertyInfo();
97             child = pi;
98    
99             if(attrs.getLength() > 0)
100             {
101                for(int i = 0; i < attrs.getLength(); ++i)
102                {
103                   if(attrs.getLocalName(i).equals("name"))
104                   {
105                      pi.setName(attrs.getValue(i));
106                   }
107                   else if (attrs.getLocalName(i).equals("type"))
108                   {
109                      pi.setType(attrs.getValue(i));
110                   }
111                   else if (attrs.getLocalName(i).equals("optional"))
112                   {
113                      pi.setOptional(Boolean.valueOf(attrs.getValue(i)).booleanValue());
114                   }
115                }
116             }
117             if (pi.getName() == null)
118                throw new RuntimeException JavaDoc(
119                   "Missing attribute 'name' for <property> element");
120             
121             // check type if specified, otherwise assume the default
122
String JavaDoc type = pi.getType();
123             if (type == null)
124                pi.setType("java.lang.String");
125          }
126          else if ("template".equals(localName))
127          {
128             TemplateInfo ti = new TemplateInfo();
129             child = ti;
130             
131             if(attrs.getLength() > 0)
132             {
133                for(int i = 0; i < attrs.getLength(); ++i)
134                {
135                   if(attrs.getLocalName(i).equals("input"))
136                   {
137                      ti.setInput(attrs.getValue(i));
138                   }
139                   else if (attrs.getLocalName(i).equals("output"))
140                   {
141                      ti.setOutput(attrs.getValue(i));
142                   }
143                }
144             }
145             // check both attributes are populated
146
if (ti.getInput() == null || ti.getOutput() == null)
147                throw new RuntimeException JavaDoc(
148                   "Both 'input' and 'output' attribute must be set for a <template> element");
149          }
150       }
151       return child;
152    }
153
154    public void addChild(Object JavaDoc parent, Object JavaDoc child, UnmarshallingContext ctx,
155                         String JavaDoc namespaceURI, String JavaDoc localName)
156    {
157       if(parent instanceof ConfigInfo)
158       {
159          final ConfigInfo ci = (ConfigInfo)parent;
160          if(child instanceof PropertyInfo)
161          {
162             ci.addPropertyInfo((PropertyInfo)child);
163          }
164          else if(child instanceof TemplateInfo)
165          {
166             ci.addTemplateInfo((TemplateInfo)child);
167          }
168       }
169    }
170
171    public void setValue(Object JavaDoc o, UnmarshallingContext ctx, String JavaDoc namespaceURI,
172                         String JavaDoc localName, String JavaDoc value)
173    {
174       if(o instanceof ConfigInfo)
175       {
176          final ConfigInfo ci = (ConfigInfo)o;
177          if("description".equals(localName))
178          {
179             ci.setDescription(value);
180          }
181       }
182       else if(o instanceof PropertyInfo)
183       {
184          PropertyInfo pi = (PropertyInfo)o;
185          if("description".equals(localName))
186          {
187             pi.setDescription(value);
188          }
189          else if("default-value".equals(localName))
190          {
191             ClassLoader JavaDoc cl = Thread.currentThread().getContextClassLoader();
192             Class JavaDoc clazz = null;
193             try {
194                clazz = cl.loadClass(pi.getType());
195             }
196             catch (ClassNotFoundException JavaDoc e) {
197                throw new RuntimeException JavaDoc("Class not found for property '" + pi.getName() +
198                                     "' of type '" + pi.getType() + "'");
199             }
200             PropertyEditor JavaDoc peditor = PropertyEditorManager.findEditor(clazz);
201
202             if (peditor != null) {
203                peditor.setAsText(value);
204                pi.setDefaultValue(peditor.getValue());
205             }
206             else
207                throw new RuntimeException JavaDoc("Property editor not found for property '" + pi.getName() +
208                                     "' of type '" + pi.getType() + "'");
209          }
210       }
211    }
212 }
213
Popular Tags