01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.axis2.wsdl.codegen.writer;
20:
21: import org.apache.axis2.util.FileWriter;
22: import org.apache.axis2.util.XMLPrettyPrinter;
23: import org.apache.ws.commons.schema.XmlSchema;
24:
25: import javax.xml.transform.OutputKeys;
26: import java.io.File;
27: import java.io.FileOutputStream;
28: import java.util.Map;
29: import java.util.HashMap;
30:
31: /** A convenient class to write out the schemas into a file in the output location */
32: public class SchemaWriter {
33:
34: private File baseFolder = null;
35:
36: public SchemaWriter(File baseFolder) {
37: this .baseFolder = baseFolder;
38: }
39:
40: public void writeSchema(XmlSchema schema, String schemaFileName) {
41: try {
42: if (schema != null) {
43: //create a output file
44: File outputFile = FileWriter.createClassFile(
45: baseFolder, null, schemaFileName.substring(0,
46: schemaFileName.lastIndexOf(".")),
47: ".xsd");
48: FileOutputStream fos = new FileOutputStream(outputFile);
49:
50: //set the options for the schemas
51: schema.write(fos, getDefaultOptionMap());
52: fos.flush();
53: fos.close();
54: XMLPrettyPrinter.prettify(outputFile);
55: }
56: } catch (Exception e) {
57: throw new RuntimeException("Schema writing failed!", e);
58: }
59: }
60:
61: private Map getDefaultOptionMap() {
62: Map options = new HashMap();
63: options.put(OutputKeys.OMIT_XML_DECLARATION, "no");
64: options.put(OutputKeys.INDENT, "yes");
65:
66: return options;
67: }
68:
69: }
|