summaryrefslogtreecommitdiffstats
path: root/sandbox/tools/xmlfromxsd/src/main/java/org/apache/tuscany/tools/xmlfromxsd/generate/XMLfromXSDConfiguration.java
blob: bd3d8bc403e78770e728acab0e1b4ac5769bfaed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/**
 *
 * Copyright 2006 The Apache Software Foundation or its licensors as applicable
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.tools.xmlfromxsd.generate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;

import org.apache.ws.java2wsdl.Java2WSDLUtils;

/**
 * This class encapsulates the configuration settings for the "XML From XSD" generation.  
 * Presently it contains settings like the XSD to be use, the schematype for which and
 * xml instance is to be created, the destination for the output etc.  All of these 
 * settings are used by the 'generator' classes during the genation of the xml instance.
 *
 */
public class XMLfromXSDConfiguration implements CmdLineArgsHandler
{
	private static int fileCount = 0;
	public static final String DEFAULT_XML_OUTPUT_FILENAME = "XML_FROM_XSD_" + (++fileCount) + ".xml";
	
	public static final String XSD_FILE_URL_OPTION = "xsd";
	public static final String XML_OUTPUT_LOCATION_OPTION = "o";
	public static final String XML_OUTPUT_FILENAME_OPTION = "of";
	public static final String ROOT_ELEMENT_NAMESPACE_URI_OPTION = "rns";
	public static final String ROOT_ELEMENT_LOCALNAME_OPTION = "rn";
	public static final String SCHEMA_TYPE_NAME = "st";
	public static final String SCHEMA_TYPE_NAMESPACE_URI = "stn";
	public static final String GENERATE_SAMPLE_DATA = "sd";
	
	protected String xmlOutputLocation = null;
	protected File xmlOutputDirectory = null;
	protected String xsdFileName = null;
	protected String xmlFileName = null;
	protected InputStream xsdInputStream = null;
	protected OutputStream xmlOutputStream = null;
	protected String rootElementNamespaceURI = null;
	protected String rootElementLocalName = null;
	protected String schemaTypeName = null;
	protected String schemaTypeNamespaceURI = null;
	protected boolean generateSampleData = false;
	
	private void handleXSDInputFileOption(String xsdFileUrl) throws Exception
	{
		xsdFileName = xsdFileUrl;
		//xsdInputStream = new FileInputStream(new File(xsdFileName));
		URL resourceUrl = Thread.currentThread().getContextClassLoader().getResource(xsdFileName);
		if ( resourceUrl != null )
		{
			xsdInputStream = resourceUrl.openStream();
		}
		else
		{
			File inFile = new File(xsdFileName);
			xsdFileName = inFile.getName();
			xsdInputStream = new FileInputStream(inFile);
		}
	}
	
	private void handleXMLOutputLocationOption(String outputLocation) throws Exception
	{
		xmlOutputLocation = outputLocation;
		xmlOutputDirectory = new File(xmlOutputLocation);
        if (!xmlOutputDirectory.exists()) 
        {
        	xmlOutputDirectory.mkdirs();
        } 
        else if (!xmlOutputDirectory.isDirectory()) 
        {
            throw new IllegalArgumentException("The input location for the xml output " + outputLocation + "is not a folder");
        }
	}
	
	private void handleOutputFilenameOption(String outputFileName) throws Exception 
	{
		xmlFileName = outputFileName;

        //first create a file in the given location
        File outputFile = new File(xmlOutputDirectory, xmlFileName);
        
        try {
            if (!outputFile.exists()) {
                outputFile.createNewFile();
            }
            xmlOutputStream = new FileOutputStream(outputFile);
        } catch (IOException e) {
            throw new Exception(e);
        }
	}
	
	public void handleArgument(String optionFlag, String argValue) throws IllegalArgumentException
	{
		try
		{
			if ( XSD_FILE_URL_OPTION.equalsIgnoreCase(optionFlag) )
			{
				handleXSDInputFileOption(argValue);
			}
			else if ( XML_OUTPUT_LOCATION_OPTION.equalsIgnoreCase(optionFlag) )
			{
				handleXMLOutputLocationOption(argValue);
			}
			else if ( XML_OUTPUT_FILENAME_OPTION.equalsIgnoreCase(optionFlag) )
			{
				handleOutputFilenameOption(argValue);
			}
			else if ( ROOT_ELEMENT_NAMESPACE_URI_OPTION.equalsIgnoreCase(optionFlag) )
			{
				setRootElementNamespaceURI(argValue);
			}
			else if ( ROOT_ELEMENT_LOCALNAME_OPTION.equalsIgnoreCase(optionFlag) )
			{
				setRootElementLocalName(argValue);
			}
			else if ( SCHEMA_TYPE_NAME.equalsIgnoreCase(optionFlag) )
			{
				setSchemaTypeName(argValue);
			}
			else if ( SCHEMA_TYPE_NAMESPACE_URI.equalsIgnoreCase(optionFlag) )
			{
				setSchemaTypeNamespaceURI(argValue);
			}
			else if ( GENERATE_SAMPLE_DATA.equalsIgnoreCase(optionFlag) )
			{
				setGenerateSampleData(true);
			}
		}
		catch ( Exception e )
		{
			throw new IllegalArgumentException("Exception due to - " + e);
		}
	}

	public String getRootElementLocalName()
	{
		if ( rootElementLocalName == null )
		{
			rootElementLocalName = schemaTypeName.toLowerCase();
		}
		return rootElementLocalName;
	}

	public void setRootElementLocalName(String rootElementLocalName)
	{
		this.rootElementLocalName = rootElementLocalName;
	}

	public String getRootElementNamespaceURI()
	{
		if ( rootElementNamespaceURI == null )
		{
			rootElementNamespaceURI = schemaTypeNamespaceURI;
		}
		return rootElementNamespaceURI;
	}

	public void setRootElementNamespaceURI(String rootElementNamespaceURI)
	{
		this.rootElementNamespaceURI = rootElementNamespaceURI;
	}

	public String getXmlFileName()
	{
		if ( xmlFileName == null )
		{
			xmlFileName = getXsdFileName() + ".xml";
		}
		return xmlFileName;
	}

	public void setXmlFileName(String xmlFileName)
	{
		this.xmlFileName = xmlFileName;
	}

	public File getXmlOutputLocation()
	{
		return xmlOutputDirectory;
	}

	public void setXmlOutputLocation(File xmlOutputLocation)
	{
		this.xmlOutputDirectory = xmlOutputLocation;
	}

	public OutputStream getXmlOutputStream() throws Exception
	{
		if ( xmlOutputStream == null )
		{
			if ( xmlOutputDirectory == null )
			{
				handleXMLOutputLocationOption(System.getProperty("user.dir"));
			}
			handleOutputFilenameOption(getXmlFileName());
		}
		return xmlOutputStream;
	}

	public void setXmlOutputStream(OutputStream xmlOutputStream)
	{
		this.xmlOutputStream = xmlOutputStream;
	}

	public String getXsdFileName()
	{
		return xsdFileName;
	}

	public void setXsdFileName(String xsdFileName) throws Exception
	{
		this.xsdFileName = xsdFileName;
		handleXSDInputFileOption(xsdFileName);
	}

	public InputStream getXsdInputStream() throws Exception
	{
		if ( xsdInputStream == null )
		{
			throw new IllegalArgumentException("XSD Input Source not set....!");
		}
		return xsdInputStream;
	}

	public void setXsdInputStream(InputStream xsdInputStream)
	{
		this.xsdInputStream = xsdInputStream;
	}

	public String getSchemaTypeName()
	{
		return schemaTypeName;
	}

	public void setSchemaTypeName(String schemaTypeName)
	{
		this.schemaTypeName = schemaTypeName;
	}
	
	public String getSchemaTypeNamespaceURI()
	{
		return schemaTypeNamespaceURI;
	}

	public void setSchemaTypeNamespaceURI(String schemaTypeNamespaceURI)
	{
		this.schemaTypeNamespaceURI = schemaTypeNamespaceURI;
	}

	public void setXmlOutputLocation(String xmlOutputLocation) throws Exception
	{
		this.xmlOutputLocation = xmlOutputLocation;
		handleXMLOutputLocationOption(xmlOutputLocation);
	}

	public boolean isGenerateSampleData()
	{
		return generateSampleData;
	}

	public void setGenerateSampleData(boolean generateSampleData)
	{
		this.generateSampleData = generateSampleData;
	}
	
}