001: /*
002: * Distributed as part of c3p0 v.0.9.1.2
003: *
004: * Copyright (C) 2005 Machinery For Change, Inc.
005: *
006: * Author: Steve Waldman <swaldman@mchange.com>
007: *
008: * This library is free software; you can redistribute it and/or modify
009: * it under the terms of the GNU Lesser General Public License version 2.1, as
010: * published by the Free Software Foundation.
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
015: * GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License
018: * along with this software; see the file LICENSE. If not, write to the
019: * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: */
022:
023: package com.mchange.v2.codegen.bean;
024:
025: import org.w3c.dom.*;
026: import java.lang.reflect.Modifier;
027: import com.mchange.v1.xml.DomParseUtils;
028:
029: public class ParsedPropertyBeanDocument {
030: final static String[] EMPTY_SA = new String[0];
031:
032: String packageName;
033: int class_modifiers;
034: String className;
035: String super className;
036: String[] interfaceNames = EMPTY_SA;
037: String[] generalImports = EMPTY_SA;
038: String[] specificImports = EMPTY_SA;
039: Property[] properties;
040:
041: public ParsedPropertyBeanDocument(Document doc) {
042: Element rootElem = doc.getDocumentElement();
043: this .packageName = DomParseUtils.allTextFromUniqueChild(
044: rootElem, "package");
045: Element modifiersElem = DomParseUtils.uniqueImmediateChild(
046: rootElem, "modifiers");
047: if (modifiersElem != null)
048: class_modifiers = parseModifiers(modifiersElem);
049: else
050: class_modifiers = Modifier.PUBLIC;
051:
052: Element importsElem = DomParseUtils.uniqueChild(rootElem,
053: "imports");
054: if (importsElem != null) {
055: this .generalImports = DomParseUtils
056: .allTextFromImmediateChildElements(importsElem,
057: "general");
058: this .specificImports = DomParseUtils
059: .allTextFromImmediateChildElements(importsElem,
060: "specific");
061: }
062: this .className = DomParseUtils.allTextFromUniqueChild(rootElem,
063: "output-class");
064: this .super className = DomParseUtils.allTextFromUniqueChild(
065: rootElem, "extends");
066:
067: Element implements Elem = DomParseUtils.uniqueChild(rootElem,
068: "implements");
069: if (implements Elem != null)
070: this .interfaceNames = DomParseUtils
071: .allTextFromImmediateChildElements(implements Elem,
072: "interface");
073: Element propertiesElem = DomParseUtils.uniqueChild(rootElem,
074: "properties");
075: this .properties = findProperties(propertiesElem);
076: }
077:
078: public ClassInfo getClassInfo() {
079: return new ClassInfo() {
080: public String getPackageName() {
081: return packageName;
082: }
083:
084: public int getModifiers() {
085: return class_modifiers;
086: }
087:
088: public String getClassName() {
089: return className;
090: }
091:
092: public String getSuperclassName() {
093: return super className;
094: }
095:
096: public String[] getInterfaceNames() {
097: return interfaceNames;
098: }
099:
100: public String[] getGeneralImports() {
101: return generalImports;
102: }
103:
104: public String[] getSpecificImports() {
105: return specificImports;
106: }
107: };
108: }
109:
110: public Property[] getProperties() {
111: return (Property[]) properties.clone();
112: }
113:
114: private Property[] findProperties(Element propertiesElem) {
115: NodeList nl = DomParseUtils.immediateChildElementsByTagName(
116: propertiesElem, "property");
117: int len = nl.getLength();
118: Property[] out = new Property[len];
119: for (int i = 0; i < len; ++i) {
120: Element propertyElem = (Element) nl.item(i);
121:
122: int variable_modifiers;
123: String name;
124: String simpleTypeName;
125: String defensiveCopyExpression;
126: String defaultValueExpression;
127: int getter_modifiers;
128: int setter_modifiers;
129: boolean is_read_only;
130: boolean is_bound;
131: boolean is_constrained;
132:
133: variable_modifiers = modifiersThroughParentElem(
134: propertyElem, "variable", Modifier.PRIVATE);
135: name = DomParseUtils.allTextFromUniqueChild(propertyElem,
136: "name", true);
137: simpleTypeName = DomParseUtils.allTextFromUniqueChild(
138: propertyElem, "type", true);
139: defensiveCopyExpression = DomParseUtils
140: .allTextFromUniqueChild(propertyElem,
141: "defensive-copy", true);
142: defaultValueExpression = DomParseUtils
143: .allTextFromUniqueChild(propertyElem,
144: "default-value", true);
145: getter_modifiers = modifiersThroughParentElem(propertyElem,
146: "getter", Modifier.PUBLIC);
147: setter_modifiers = modifiersThroughParentElem(propertyElem,
148: "setter", Modifier.PUBLIC);
149: Element readOnlyElem = DomParseUtils.uniqueChild(
150: propertyElem, "read-only");
151: is_read_only = (readOnlyElem != null);
152: Element isBoundElem = DomParseUtils.uniqueChild(
153: propertyElem, "bound");
154: is_bound = (isBoundElem != null);
155: Element isConstrainedElem = DomParseUtils.uniqueChild(
156: propertyElem, "constrained");
157: is_constrained = (isConstrainedElem != null);
158: out[i] = new SimpleProperty(variable_modifiers, name,
159: simpleTypeName, defensiveCopyExpression,
160: defaultValueExpression, getter_modifiers,
161: setter_modifiers, is_read_only, is_bound,
162: is_constrained);
163: }
164: return out;
165: }
166:
167: private static int modifiersThroughParentElem(
168: Element grandparentElem, String parentElemName,
169: int default_mods) {
170: Element parentElem = DomParseUtils.uniqueChild(grandparentElem,
171: parentElemName);
172: if (parentElem != null) {
173: Element modifiersElem = DomParseUtils.uniqueChild(
174: parentElem, "modifiers");
175: if (modifiersElem != null)
176: return parseModifiers(modifiersElem);
177: else
178: return default_mods;
179: } else
180: return default_mods;
181: }
182:
183: private static int parseModifiers(Element modifiersElem) {
184: int out = 0;
185: String[] all_modifiers = DomParseUtils
186: .allTextFromImmediateChildElements(modifiersElem,
187: "modifier", true);
188: for (int i = 0, len = all_modifiers.length; i < len; ++i) {
189: String modifier = all_modifiers[i];
190: if ("public".equals(modifier))
191: out |= Modifier.PUBLIC;
192: else if ("protected".equals(modifier))
193: out |= Modifier.PROTECTED;
194: else if ("private".equals(modifier))
195: out |= Modifier.PRIVATE;
196: else if ("final".equals(modifier))
197: out |= Modifier.FINAL;
198: else if ("abstract".equals(modifier))
199: out |= Modifier.ABSTRACT;
200: else if ("static".equals(modifier))
201: out |= Modifier.STATIC;
202: else if ("synchronized".equals(modifier))
203: out |= Modifier.SYNCHRONIZED;
204: else if ("volatile".equals(modifier))
205: out |= Modifier.VOLATILE;
206: else if ("transient".equals(modifier))
207: out |= Modifier.TRANSIENT;
208: else if ("strictfp".equals(modifier))
209: out |= Modifier.STRICT;
210: else if ("native".equals(modifier))
211: out |= Modifier.NATIVE;
212: else if ("interface".equals(modifier))
213: out |= Modifier.INTERFACE; // ????
214: else
215: throw new IllegalArgumentException("Bad modifier: "
216: + modifier);
217: }
218: return out;
219: }
220:
221: }
|