KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mchange > v2 > codegen > bean > ParsedPropertyBeanDocument


1 /*
2  * Distributed as part of c3p0 v.0.9.1
3  *
4  * Copyright (C) 2005 Machinery For Change, Inc.
5  *
6  * Author: Steve Waldman <swaldman@mchange.com>
7  *
8  * This library is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License version 2.1, as
10  * published by the Free Software Foundation.
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
15  * GNU Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this software; see the file LICENSE. If not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */

22
23
24 package com.mchange.v2.codegen.bean;
25
26 import org.w3c.dom.*;
27 import java.lang.reflect.Modifier JavaDoc;
28 import com.mchange.v1.xml.DomParseUtils;
29
30 public class ParsedPropertyBeanDocument
31 {
32     final static String JavaDoc[] EMPTY_SA = new String JavaDoc[0];
33
34     String JavaDoc packageName;
35     int class_modifiers;
36     String JavaDoc className;
37     String JavaDoc superclassName;
38     String JavaDoc[] interfaceNames = EMPTY_SA;
39     String JavaDoc[] generalImports = EMPTY_SA;
40     String JavaDoc[] specificImports = EMPTY_SA;
41     Property[] properties;
42     
43     public ParsedPropertyBeanDocument(Document doc)
44     {
45     Element rootElem = doc.getDocumentElement();
46         this.packageName = DomParseUtils.allTextFromUniqueChild( rootElem, "package" );
47     Element modifiersElem = DomParseUtils.uniqueImmediateChild( rootElem, "modifiers" );
48     if (modifiersElem != null)
49         class_modifiers = parseModifiers( modifiersElem );
50     else
51         class_modifiers = Modifier.PUBLIC;
52     
53     Element importsElem = DomParseUtils.uniqueChild( rootElem, "imports" );
54     if (importsElem != null)
55         {
56         this.generalImports = DomParseUtils.allTextFromImmediateChildElements( importsElem, "general" );
57         this.specificImports = DomParseUtils.allTextFromImmediateChildElements( importsElem, "specific" );
58         }
59     this.className = DomParseUtils.allTextFromUniqueChild( rootElem, "output-class" );
60     this.superclassName = DomParseUtils.allTextFromUniqueChild( rootElem, "extends" );
61
62     Element implementsElem = DomParseUtils.uniqueChild( rootElem, "implements" );
63     if (implementsElem != null)
64         this.interfaceNames = DomParseUtils.allTextFromImmediateChildElements( implementsElem, "interface" );
65     Element propertiesElem = DomParseUtils.uniqueChild( rootElem, "properties" );
66     this.properties = findProperties( propertiesElem );
67     }
68
69
70     public ClassInfo getClassInfo()
71     {
72     return new ClassInfo()
73         {
74         public String JavaDoc getPackageName()
75         { return packageName; }
76
77         public int getModifiers()
78         { return class_modifiers; }
79
80         public String JavaDoc getClassName()
81         { return className; }
82
83         public String JavaDoc getSuperclassName()
84         { return superclassName; }
85
86         public String JavaDoc[] getInterfaceNames()
87         { return interfaceNames; }
88
89         public String JavaDoc[] getGeneralImports()
90         { return generalImports; }
91
92         public String JavaDoc[] getSpecificImports()
93         { return specificImports; }
94         };
95     }
96
97     public Property[] getProperties()
98     { return (Property[]) properties.clone(); }
99
100     private Property[] findProperties( Element propertiesElem )
101     {
102     NodeList nl = DomParseUtils.immediateChildElementsByTagName( propertiesElem, "property" );
103     int len = nl.getLength();
104     Property[] out = new Property[ len ];
105     for( int i = 0; i < len; ++i)
106         {
107         Element propertyElem = (Element) nl.item( i );
108
109         int variable_modifiers;
110         String JavaDoc name;
111         String JavaDoc simpleTypeName;
112         String JavaDoc defensiveCopyExpression;
113         String JavaDoc defaultValueExpression;
114         int getter_modifiers;
115         int setter_modifiers;
116         boolean is_read_only;
117         boolean is_bound;
118         boolean is_constrained;
119         
120         variable_modifiers = modifiersThroughParentElem( propertyElem, "variable", Modifier.PRIVATE );
121         name = DomParseUtils.allTextFromUniqueChild( propertyElem, "name", true );
122         simpleTypeName = DomParseUtils.allTextFromUniqueChild( propertyElem, "type", true );
123         defensiveCopyExpression = DomParseUtils.allTextFromUniqueChild( propertyElem, "defensive-copy", true );
124         defaultValueExpression = DomParseUtils.allTextFromUniqueChild( propertyElem, "default-value", true );
125         getter_modifiers = modifiersThroughParentElem( propertyElem, "getter", Modifier.PUBLIC );
126         setter_modifiers = modifiersThroughParentElem( propertyElem, "setter", Modifier.PUBLIC );
127         Element readOnlyElem = DomParseUtils.uniqueChild( propertyElem, "read-only" );
128         is_read_only = (readOnlyElem != null);
129         Element isBoundElem = DomParseUtils.uniqueChild( propertyElem, "bound" );
130         is_bound = (isBoundElem != null);
131         Element isConstrainedElem = DomParseUtils.uniqueChild( propertyElem, "constrained" );
132         is_constrained = (isConstrainedElem != null);
133         out[i] = new SimpleProperty( variable_modifiers, name, simpleTypeName, defensiveCopyExpression,
134                          defaultValueExpression, getter_modifiers, setter_modifiers,
135                          is_read_only, is_bound, is_constrained );
136         }
137     return out;
138     }
139
140     private static int modifiersThroughParentElem( Element grandparentElem, String JavaDoc parentElemName, int default_mods )
141     {
142     Element parentElem = DomParseUtils.uniqueChild( grandparentElem, parentElemName );
143     if (parentElem != null )
144         {
145         Element modifiersElem = DomParseUtils.uniqueChild( parentElem, "modifiers" );
146         if (modifiersElem != null)
147             return parseModifiers( modifiersElem );
148         else
149             return default_mods;
150         }
151     else
152         return default_mods;
153     }
154
155     private static int parseModifiers( Element modifiersElem )
156     {
157     int out = 0;
158     String JavaDoc[] all_modifiers = DomParseUtils.allTextFromImmediateChildElements( modifiersElem, "modifier", true );
159     for ( int i = 0, len = all_modifiers.length; i < len; ++i)
160         {
161         String JavaDoc modifier = all_modifiers[i];
162         if ("public".equals( modifier )) out |= Modifier.PUBLIC;
163         else if ("protected".equals( modifier )) out |= Modifier.PROTECTED;
164         else if ("private".equals( modifier )) out |= Modifier.PRIVATE;
165         else if ("final".equals( modifier )) out |= Modifier.FINAL;
166         else if ("abstract".equals( modifier )) out |= Modifier.ABSTRACT;
167         else if ("static".equals( modifier )) out |= Modifier.STATIC;
168         else if ("synchronized".equals( modifier )) out |= Modifier.SYNCHRONIZED;
169         else if ("volatile".equals( modifier )) out |= Modifier.VOLATILE;
170         else if ("transient".equals( modifier )) out |= Modifier.TRANSIENT;
171         else if ("strictfp".equals( modifier )) out |= Modifier.STRICT;
172         else if ("native".equals( modifier )) out |= Modifier.NATIVE;
173         else if ("interface".equals( modifier )) out |= Modifier.INTERFACE; // ????
174
else throw new IllegalArgumentException JavaDoc("Bad modifier: " + modifier);
175         }
176     return out;
177     }
178
179
180 }
181
Popular Tags