summaryrefslogtreecommitdiffstats
path: root/sandbox/tools/xmlfromxsd/src/main/java/org/apache/tuscany/tools/xmlfromxsd/generate/SDObasedXMLGenerator.java
blob: f415babca25e4be64f60ce591d8c3c512d4b6ad4 (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
/**
 *
 * 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.util.HashMap;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.tuscany.sdo.util.SDOUtil;

import commonj.sdo.DataObject;
import commonj.sdo.Property;
import commonj.sdo.Sequence;
import commonj.sdo.Type;
import commonj.sdo.helper.CopyHelper;
import commonj.sdo.helper.DataFactory;
import commonj.sdo.helper.TypeHelper;
import commonj.sdo.helper.XMLHelper;
import commonj.sdo.helper.XSDHelper;

/**
 * This class generates an XML instance given and XSD.  All inputs required by this generator are
 * set into the XMLFromXSDConfiguration object that is passed in the constructor.  This generator
 * first loads the XSD Types into the TypeHelper.  Then for the XSD type for which an XML instnace
 * is to be created, this generator creates SDOs which are then serialized as XML 
 *
 */
public class SDObasedXMLGenerator implements XMLGenerator
{
	private XMLfromXSDConfiguration config = null;

	private TypeHelper typeHelper = null; //SDOUtil.createTypeHelper();

	private XSDHelper xsdHelper = null; //SDOUtil.createXSDHelper(typeHelper);

	private XMLHelper xmlHelper = null;//SDOUtil.createXMLHelper(typeHelper);

	private Hashtable complexTypesMap = new Hashtable();

	DataFactory dataFactory = SDOUtil.createDataFactory(typeHelper);

	public SDObasedXMLGenerator(XMLfromXSDConfiguration config)
	{
		this.config = config;
		// config.setXsdInputStream(getClass().getResource(conf).openStream());
	}
	
	

	public void generateXML(XMLfromXSDConfiguration config) throws Exception
	{
		this.config = config;
		generateXML();
	}



	public void generateXML() throws Exception
	{
		typeHelper = SDOUtil.createTypeHelper();
        xsdHelper = SDOUtil.createXSDHelper(typeHelper);
		
		if ( config != null )
		{
			List types = xsdHelper.define(config.getXsdInputStream(), null);
			Type type = typeHelper.getType(config.getSchemaTypeNamespaceURI(),
					config.getSchemaTypeName());
	
			if ( type != null )
			{
				Object instance = createInstance(type, null);
				if ( instance != null )
				{
					xmlHelper =  SDOUtil.createXMLHelper(typeHelper);
					xmlHelper.save((DataObject) instance, 
							config.getRootElementNamespaceURI(),
							config.getRootElementLocalName(), config.getXmlOutputStream());
				}
			}
			else
			{
				throw new IllegalArgumentException("Incorrect input of SchemaType Namespace / SchemaType to be instantiated as XML!");
			}
		}
		else
		{
			throw new Exception("Generation Configuraiton not set - no instnace of XMLFromXSDConfiguration found!");
		}
	}

	protected Object createSimpleDataObject(Type type, Object defaultValue)
	{
		// FIXME: We need to handle well-know simple types
		//return SDOUtil.createFromString(type, "0");
		try
    	{
			return SDOUtil.createFromString(type,"");
    	}
    	catch ( NumberFormatException nfe )
    	{
    		return SDOUtil.createFromString(type,"0");
    	}
		
	}

	protected Object createComplexDataObject(Type type, Object defaultValue)
	{
		// Type references can be recursive, we need to avoid infinite loop
		DataObject dataObject = (DataObject) complexTypesMap.get(type);
		if (dataObject != null)
		{
			dataObject = CopyHelper.INSTANCE.copy((DataObject) dataObject);
		} else
		{
			dataObject = dataFactory.create(type);
			complexTypesMap.put(type, dataObject);

			List properties = type.getProperties();
			for ( Iterator i = properties.iterator(); i.hasNext(); )
			{
				Property property = (Property) i.next();
				if (!property.isReadOnly())
				{
					if (property.isMany())
					{
						Object values = dataObject.get(property);
						if (values instanceof Sequence)
						{
							// NOP
						}
						if (values instanceof List)
						{
							((List) values).add(createInstance(property
									.getType(), null));
						}
					} else
					{
						dataObject.set(property, createInstance(property
								.getType(), property.getDefault()));
					}
				}
			}
		}
		return dataObject;
	}

	public Object createInstance(Type type, Object defaultValue)
	{
		Object dataObject = null;
		if (type != null)
		{
			if (type.isDataType())
			{
				if (defaultValue != null)
				{
					dataObject = defaultValue;
				} else
				{
					dataObject = createSimpleDataObject(type, defaultValue);
				}

			} else
			{
				dataObject = createComplexDataObject(type, defaultValue);
			}
		}
		return dataObject;
	}

	public XMLfromXSDConfiguration getConfig()
	{
		return config;
	}

	public void setConfig(XMLfromXSDConfiguration config)
	{
		this.config = config;
	}
}