/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2003 The Apache Software Foundation. All rights
* reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by the
* Apache Software Foundation (http://www.apache.org/)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The name "Apache Software Foundation" must
* not be used to endorse or promote products derived from this
* software without prior written permission. For written
* permission, please contact apache@apache.org.
*
* 5. Products derived from this software may not be called "Apache",
* nor may "Apache" appear in their name, without prior written
* permission of the Apache Software Foundation.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
package org.apache.ws.jaxme.generator.impl;
import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.apache.ws.jaxme.generator.Generator;
import org.apache.ws.jaxme.generator.SchemaReader;
import org.apache.ws.jaxme.generator.sg.SchemaSG;
import org.apache.ws.jaxme.js.JavaSource;
import org.apache.ws.jaxme.js.JavaSourceFactory;
import org.apache.ws.jaxme.logging.Logger;
import org.apache.ws.jaxme.logging.LoggerAccess;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
/** <p>The Generator is reading an input schema. The schema is
* converted into a DOM tree. Finally one or more source
* writers are executed.
*
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
*/
public class GeneratorImpl implements Generator {
private static final Logger log = LoggerAccess.getLogger(GeneratorImpl.class);
private SchemaReader schemaReader;
private java.io.File targetDirectory;
private Map properties = new HashMap();
private int nextKey;
private boolean isValidating, isForcingOverwrite, isSettingReadOnly;
private EntityResolver entityResolver;
/** <p>Sets the {@link EntityResolver} being used to import external
* schemata.</p>
*/
public void setEntityResolver(EntityResolver pEntityResolver) {
entityResolver = pEntityResolver;
}
/** <p>Returns the {@link EntityResolver} being used to import external
* schemata.</p>
*/
public EntityResolver getEntityResolver() {
return entityResolver;
}
/** <p>Returns whether the generator is forcing an overwrite of files.</p>
*/
public boolean isForcingOverwrite() {
return isForcingOverwrite;
}
/** <p>Sets whether the generator is forcing an overwrite of files.</p>
*/
public void setForcingOverwrite(boolean pIsForcingOverwrite) {
isForcingOverwrite = pIsForcingOverwrite;
}
/** <p>Returns whether the generator will create files in read-only mode.</p>
*/
public boolean isSettingReadOnly() {
return isSettingReadOnly;
}
/** <p>Sets whether the generator will create files in read-only mode.</p>
*/
public void setSettingReadOnly(boolean pIsSettingReadOnly) {
isSettingReadOnly = pIsSettingReadOnly;
}
public boolean isValidating() {
return isValidating;
}
public void setValidating(boolean pIsValidating) {
isValidating = pIsValidating;
}
/** Creates a new GeneratorImpl */
public GeneratorImpl() {
}
/** <p>Sets the SchemaReader.</p>
*/
public void setSchemaReader(SchemaReader pReader) {
schemaReader = pReader;
}
/** <p>Returns the SchemaReader.</p>
*/
public SchemaReader getSchemaReader() {
return schemaReader;
}
public void setTargetDirectory(File pDirectory) {
targetDirectory = pDirectory;
}
public File getTargetDirectory() {
return targetDirectory;
}
public SchemaSG generate(InputSource pSource) throws Exception {
SchemaReader sr = getSchemaReader();
sr.setGenerator(this);
SchemaSG s = sr.parse(pSource);
s.generate();
File targetDir = getTargetDirectory();
JavaSourceFactory jsf = s.getJavaSourceFactory();
for (Iterator iter = jsf.getJavaSources(); iter.hasNext(); ) {
((JavaSource) iter.next()).setForcingFullyQualifiedName(true);
}
s.getJavaSourceFactory().write(targetDir);
return s;
}
/** <p>Opens the given file, calls the specified SchemaReaders
* () method and SourceWriters write() method.</p>
*/
public SchemaSG generate(File pFile) throws Exception {
final String mName = "generate(File)";
log.finer(mName, "->", pFile);
String path = pFile.getAbsolutePath();
if (!pFile.exists()) {
throw new java.io.FileNotFoundException("File does not exist: " + path);
}
if (!pFile.isFile()) {
throw new java.io.FileNotFoundException("Not a file: " + path);
}
InputSource isource = new InputSource(new FileInputStream(pFile));
isource.setSystemId(pFile.toURL().toString());
SchemaSG s = generate(isource);
log.finer(mName, "<-", s);
return s;
}
/** <p>Opens the given URL, calls the specified SchemaReaders
* () method and SourceWriters write() method.</p>
*/
public SchemaSG generate(java.net.URL pURL) throws Exception {
final String mName = "generate(URL)";
log.entering(mName, pURL);
java.net.URLConnection conn = pURL.openConnection();
InputSource isource = new InputSource(conn.getInputStream());
isource.setSystemId(pURL.toString());
SchemaSG s = generate(isource);
log.exiting(mName, s);
return s;
}
public String getProperty(String pName) {
return (String) properties.get(pName);
}
public String getProperty(String pName, String pDefault) {
String result = (String) properties.get(pName);
return (result == null) ? pDefault : result;
}
public void setProperty(String pName, String pValue) {
properties.put(pName, pValue);
}
public String getKey() {
return Integer.toString(nextKey++);
}
}