KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > zeus > transform > SimpleTransformer


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  */

19 package org.enhydra.zeus.transform;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25
26 // Zeus imports
27
import org.enhydra.zeus.Binding;
28 import org.enhydra.zeus.binding.Container;
29 import org.enhydra.zeus.Source;
30 import org.enhydra.zeus.ZeusDefaults;
31 import org.enhydra.zeus.ZeusException;
32 import org.enhydra.zeus.util.ClassUtils;
33 import org.enhydra.zeus.util.NamingUtils;
34
35 /**
36  * <p>
37  * <code>SimpleTransformer</code> is a basic implementation of a Zeus
38  * <code>{@link org.enhydra.zeus.Transformer}</code>. It iterates through
39  * the supplied <code>{@link Binding}</code> objects and assigns each the
40  * options for this transformer.
41  * </p>
42  *
43  * @author Brett McLaughlin
44  */

45 public class SimpleTransformer extends BaseTransformer {
46     
47     /**
48      * <p>
49      * This constructor takes in a <code>{@link Source}</code>
50      * to read mapping information from and allow generation of the
51      * transformation mappings from it.
52      * </p>
53      *
54      * @param source <code>Source</code> to read mapping input from.
55      */

56     public SimpleTransformer(Source source) {
57         super(source);
58     }
59     
60     /**
61      * <p>
62      * This method performs the work of transforming a set of Zeus
63      * <code>{@link org.enhydra.zeus.Binding}</code> objects. It then
64      * returns a <i>new</i> set of <code>Binding</code> objects
65      * (in <code>List</code> form) with transformed names, types, and values.
66      * </p><p>
67      * When this method is invoked, it attempts to see if parsing of the
68      * mapping file has already occurred. If it has not, parsing of that
69      * file will occur within this method.
70      * </p><p>
71      * It is important to note that the <i>number</i> of bindings returned
72      * from this method may not be the same as the number supplied. This
73      * is due to some XML elements/attributes being converted to value
74      * objects, while other mapping instructions may actually create new
75      * binding objects.
76      * </p>
77      *
78      * @param bindings <code>List</code> of bindings to process for
79      * transformation.
80      * @param recursing indication of if this is within a recursion loop.
81      * @return <code>List</code> - the transformed bindings.
82      * @throws <code>IOException</code> - when errors occur in parsing
83      * a mapping configuration file.
84      * @throws <code>ZeusException</code> - when errors occuring in
85      * transformation.
86      */

87     public List JavaDoc transform(List JavaDoc bindings, boolean recursing)
88         throws IOException JavaDoc, ZeusException {
89             
90         // Create list for result bindings
91
List JavaDoc resultBindings = new LinkedList JavaDoc();
92         List JavaDoc usedNames = new LinkedList JavaDoc();
93         List JavaDoc usedVariableNames = new LinkedList JavaDoc();
94         
95         // Assign Java properties from TransformerOptions to each binding
96
for (Iterator JavaDoc i = bindings.iterator(); i.hasNext(); ) {
97             Binding binding = (Binding)i.next();
98             
99             if (!recursing) {
100                 usedNames.clear();
101                 usedVariableNames.clear();
102             }
103             
104             // Handle generic options
105

106             // This is temporary //
107
int count = 2;
108             String JavaDoc javaName = NamingUtils.getJavaName(binding.getXMLName());
109             while (usedNames.contains(javaName)) {
110                 javaName = new StringBuffer JavaDoc(javaName)
111                     .append(count)
112                     .toString();
113                 count++;
114             }
115             usedNames.add(javaName);
116             binding.setJavaName(javaName);
117             
118             count = 2;
119             String JavaDoc javaVariableName =
120                 NamingUtils.getJavaVariableName(binding.getXMLName());
121             while (usedVariableNames.contains(javaVariableName)) {
122                 javaVariableName = new StringBuffer JavaDoc(javaVariableName)
123                     .append(count)
124                     .toString();
125                 count++;
126             }
127             usedVariableNames.add(javaVariableName);
128             binding.setJavaVariableName(javaVariableName);
129                 
130             binding.setJavaType(
131                 NamingUtils.getJavaType(binding.getXMLType()));
132             // End temporary block //
133

134             // Only set the package for non-PCDATA types
135
if (!ClassUtils.isJavaPrimitive(binding.getJavaType())) {
136                 binding.setJavaInterfacePackage(
137                     getTransformerOptions().getInterfacePackage());
138                 binding.setJavaImplementationPackage(
139                     getTransformerOptions().getImplementationPackage());
140             } else {
141                 binding.setJavaInterfacePackage("");
142                 binding.setJavaImplementationPackage("");
143             }
144             
145             binding.setJavaCollectionClass(
146                 getTransformerOptions().getDefaultCollectionType());
147             
148             // If this is a container, handle children
149
if (binding instanceof Container) {
150                 Container container = (Container)binding;
151                 // Recurse
152
container.setProperties(
153                     transform(container.getProperties(), true));
154             }
155             
156             // Add the transformed bindings to the result set
157
resultBindings.add(binding);
158         }
159         
160         return resultBindings;
161     }
162
163 }
164
Popular Tags