KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > ws > jaxme > generator > impl > GeneratorImpl


1 /*
2  * Copyright 2003, 2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15
16  */

17 package org.apache.ws.jaxme.generator.impl;
18
19 import java.io.File JavaDoc;
20 import java.io.FileInputStream JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.ws.jaxme.generator.Generator;
26 import org.apache.ws.jaxme.generator.SchemaReader;
27 import org.apache.ws.jaxme.generator.sg.SchemaSG;
28 import org.apache.ws.jaxme.js.JavaSource;
29 import org.apache.ws.jaxme.js.JavaSourceFactory;
30 import org.apache.ws.jaxme.logging.Logger;
31 import org.apache.ws.jaxme.logging.LoggerAccess;
32 import org.xml.sax.EntityResolver JavaDoc;
33 import org.xml.sax.InputSource JavaDoc;
34
35
36 /** <p>The Generator is reading an input schema. The schema is
37  * converted into a DOM tree. Finally one or more source
38  * writers are executed.
39  *
40  * @author <a HREF="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
41  */

42 public class GeneratorImpl implements Generator {
43   private static final Logger log = LoggerAccess.getLogger(GeneratorImpl.class);
44   private SchemaReader schemaReader;
45   private java.io.File JavaDoc targetDirectory;
46   private Map JavaDoc properties = new HashMap JavaDoc();
47   private int nextKey;
48   private boolean isValidating, isForcingOverwrite, isSettingReadOnly;
49   private EntityResolver JavaDoc entityResolver;
50
51   /** <p>Sets the {@link EntityResolver} being used to import external
52    * schemata.</p>
53    */

54   public void setEntityResolver(EntityResolver JavaDoc pEntityResolver) {
55     entityResolver = pEntityResolver;
56   }
57
58   /** <p>Returns the {@link EntityResolver} being used to import external
59    * schemata.</p>
60    */

61   public EntityResolver JavaDoc getEntityResolver() {
62     return entityResolver;
63   }
64   
65   /** <p>Returns whether the generator is forcing an overwrite of files.</p>
66    */

67   public boolean isForcingOverwrite() {
68     return isForcingOverwrite;
69   }
70
71   /** <p>Sets whether the generator is forcing an overwrite of files.</p>
72    */

73   public void setForcingOverwrite(boolean pIsForcingOverwrite) {
74     isForcingOverwrite = pIsForcingOverwrite;
75   }
76
77   /** <p>Returns whether the generator will create files in read-only mode.</p>
78    */

79   public boolean isSettingReadOnly() {
80     return isSettingReadOnly;
81   }
82
83   /** <p>Sets whether the generator will create files in read-only mode.</p>
84    */

85   public void setSettingReadOnly(boolean pIsSettingReadOnly) {
86     isSettingReadOnly = pIsSettingReadOnly;
87   }
88
89   public boolean isValidating() {
90     return isValidating;
91   }
92
93   public void setValidating(boolean pIsValidating) {
94     isValidating = pIsValidating;
95   }
96
97   /** Creates a new GeneratorImpl */
98   public GeneratorImpl() {
99   }
100
101   /** <p>Sets the SchemaReader.</p>
102    */

103   public void setSchemaReader(SchemaReader pReader) {
104     schemaReader = pReader;
105   }
106
107   /** <p>Returns the SchemaReader.</p>
108    */

109   public SchemaReader getSchemaReader() {
110     return schemaReader;
111   }
112
113   public void setTargetDirectory(File JavaDoc pDirectory) {
114     targetDirectory = pDirectory;
115   }
116
117   public File JavaDoc getTargetDirectory() {
118     return targetDirectory;
119   }
120
121   public SchemaSG generate(InputSource JavaDoc pSource) throws Exception JavaDoc {
122     SchemaReader sr = getSchemaReader();
123     sr.setGenerator(this);
124
125     SchemaSG s = sr.parse(pSource);
126     s.generate();
127     File JavaDoc targetDir = getTargetDirectory();
128     JavaSourceFactory jsf = s.getJavaSourceFactory();
129     for (Iterator JavaDoc iter = jsf.getJavaSources(); iter.hasNext(); ) {
130         ((JavaSource) iter.next()).setForcingFullyQualifiedName(true);
131     }
132     s.getJavaSourceFactory().write(targetDir);
133
134     return s;
135   }
136
137
138   /** <p>Opens the given file, calls the specified SchemaReaders
139    * () method and SourceWriters write() method.</p>
140    */

141   public SchemaSG generate(File JavaDoc pFile) throws Exception JavaDoc {
142     final String JavaDoc mName = "generate(File)";
143     log.finer(mName, "->", pFile);
144     String JavaDoc path = pFile.getAbsolutePath();
145     if (!pFile.exists()) {
146        throw new java.io.FileNotFoundException JavaDoc("File does not exist: " + path);
147     }
148     if (!pFile.isFile()) {
149        throw new java.io.FileNotFoundException JavaDoc("Not a file: " + path);
150     }
151
152     InputSource JavaDoc isource = new InputSource JavaDoc(new FileInputStream JavaDoc(pFile));
153     isource.setSystemId(pFile.toURL().toString());
154     SchemaSG s = generate(isource);
155     log.finer(mName, "<-", s);
156     return s;
157   }
158
159   /** <p>Opens the given URL, calls the specified SchemaReaders
160    * () method and SourceWriters write() method.</p>
161    */

162   public SchemaSG generate(java.net.URL JavaDoc pURL) throws Exception JavaDoc {
163     final String JavaDoc mName = "generate(URL)";
164     log.entering(mName, pURL);
165     java.net.URLConnection JavaDoc conn = pURL.openConnection();
166
167     InputSource JavaDoc isource = new InputSource JavaDoc(conn.getInputStream());
168     isource.setSystemId(pURL.toString());
169     SchemaSG s = generate(isource);
170     log.exiting(mName, s);
171     return s;
172   }
173
174   public String JavaDoc getProperty(String JavaDoc pName) {
175     return (String JavaDoc) properties.get(pName);
176   }
177
178   public String JavaDoc getProperty(String JavaDoc pName, String JavaDoc pDefault) {
179     String JavaDoc result = (String JavaDoc) properties.get(pName);
180     return (result == null) ? pDefault : result;
181   }
182
183   public void setProperty(String JavaDoc pName, String JavaDoc pValue) {
184     properties.put(pName, pValue);
185   }
186
187   public String JavaDoc getKey() {
188     return Integer.toString(nextKey++);
189   }
190 }
191
Popular Tags