File: JavaToXSD.java

package info (click to toggle)
libjboss-web-services-java 0.0%2Bsvn5660%2Bdak2-3
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 7,276 kB
  • sloc: java: 79,207; xml: 29; makefile: 19; sh: 16
file content (287 lines) | stat: -rw-r--r-- 10,244 bytes parent folder | download | duplicates (3)
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
285
286
287
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jboss.ws.tools;

// $Id: JavaToXSD.java 2539 2007-03-07 08:08:09Z jason.greene@jboss.com $

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.xml.namespace.QName;

import org.apache.xerces.impl.xs.SchemaGrammar;
import org.apache.xerces.impl.xs.XMLSchemaLoader;
import org.apache.xerces.impl.xs.XSModelImpl;
import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xs.XSModel;
import org.jboss.logging.Logger;
import org.jboss.ws.WSException;
import org.jboss.ws.core.utils.JBossWSEntityResolver;
import org.jboss.ws.core.utils.ResourceURL;
import org.jboss.ws.metadata.wsdl.WSDLUtils;
import org.jboss.ws.metadata.wsdl.xmlschema.JBossXSEntityResolver;
import org.jboss.ws.metadata.wsdl.xmlschema.JBossXSErrorHandler;
import org.jboss.ws.metadata.wsdl.xmlschema.JBossXSModel;
import org.jboss.ws.metadata.wsdl.xmlschema.WSSchemaUtils;
import org.jboss.ws.metadata.wsdl.xsd.SchemaUtils;
import org.jboss.ws.tools.helpers.JavaToXSDHelper;
import org.jboss.ws.tools.interfaces.JavaToXSDIntf;
import org.jboss.ws.tools.interfaces.SchemaCreatorIntf;
import org.jboss.xb.binding.sunday.unmarshalling.LSInputAdaptor;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBinding;
import org.jboss.xb.binding.sunday.unmarshalling.SchemaBindingResolver;
import org.w3c.dom.ls.LSInput;
import org.xml.sax.InputSource;

/**
 * <P>
 *  Handles the conversion of Java classes to XML Schema
 *  This class is the key class to use for all Java to Schema requirements.
 *  </P>
 *  <P>
 *  Approach 1: Starting from scratch.
 *  You can generate an empty schema model by providing a target namespace.
 *  To this empty schema model, Complex Types and Global Elements can be added.
 *   <br>{@link #createSchema(String typens)  createSchema}
 *  </P>
 *  <P>
 *  Approach 2: You want to generate a complex type as a string given a xmltype and a Java Class.
 *  <br>{@link #generateForSingleType(QName xmlType, Class javaType) generateForSingleType}
 *  <br>{@link #generateForEndpoint(Class endpointOrServiceInt)  generateForEndpoint}
 * </P>
 *  @author <mailto:Anil.Saldhana@jboss.org>Anil Saldhana
 *  @since   May 6, 2005
 */
public class JavaToXSD implements JavaToXSDIntf
{
   // provide logging
   private static final Logger log = Logger.getLogger(JavaToXSD.class);

   protected WSDLUtils utils = WSDLUtils.getInstance();
   protected SchemaUtils schemautils = SchemaUtils.getInstance();
   protected String jaxrpcAssert = "JAXRPC2.0 Assertion:";

   protected JavaToXSDHelper helper = null;

   /**
    * Constructor
    */
   public JavaToXSD()
   {
      helper = new JavaToXSDHelper();
      SchemaCreatorIntf creator = helper.getSchemaCreator();
      JBossXSModel xsmodel = creator.getXSModel();
      if (xsmodel == null)
         creator.setXSModel(new JBossXSModel());
   }

   /*
    * @see org.jboss.ws.tools.interfaces.JavaToXSDIntf#generateForSingleType()
    */
   public JBossXSModel generateForSingleType(QName xmlType, Class javaType) throws IOException
   {
      SchemaCreatorIntf creator = helper.getSchemaCreator();
      creator.generateType(xmlType, javaType);
      return creator.getXSModel();
   }

   public JBossXSModel generateForSingleType(QName xmlType, Class javaType, Map<String, QName> elementNames) throws IOException
   {
      SchemaCreatorIntf creator = helper.getSchemaCreator();
      creator.generateType(xmlType, javaType, elementNames);
      return creator.getXSModel();
   }

   /*
    * @see org.jboss.ws.tools.interfaces.JavaToXSDIntf#getSchemaCreator()
    */
   public SchemaCreatorIntf getSchemaCreator()
   {
      return helper.getSchemaCreator();
   }

   /**
    * Given a schema file in a file, return a Schema Model
    * @param xsdURL Location of the schema file
    * @return Xerces XSModel which represents the schema
    */
   public JBossXSModel parseSchema(URL xsdURL)
   {
      JBossXSErrorHandler xserr = new JBossXSErrorHandler();
      JBossWSEntityResolver resolver = new JBossWSEntityResolver();
      JBossXSEntityResolver xsresolve = new JBossXSEntityResolver(resolver, new HashMap<String, URL>());
      XMLSchemaLoader loader = (XMLSchemaLoader)schemautils.getXSLoader(xserr, xsresolve);

      XSModel xsmodel = loader.loadURI(xsdURL.toExternalForm());
      if (xsmodel == null)
         throw new WSException("Cannot load schema: " + xsdURL);

      WSSchemaUtils sutils = WSSchemaUtils.getInstance(null, null);
      JBossXSModel jbxs = new JBossXSModel();
      sutils.copyXSModel(xsmodel, jbxs);
      return jbxs;
   }

   /**
    * Given a set of schema files, parse them to yield an unified JBossXSModel
    * @param locs a map of schema namespace to schema location
    * @return unified JBossXSModel
    */
   public JBossXSModel parseSchema(Map<String, URL> locs)
   {
      if (locs == null || locs.size() == 0)
         throw new IllegalArgumentException("Illegal schema location map");

      JBossXSErrorHandler xserr = new JBossXSErrorHandler();
      JBossWSEntityResolver resolver = new JBossWSEntityResolver();
      JBossXSEntityResolver xsresolve = new JBossXSEntityResolver(resolver, locs);
      XMLSchemaLoader loader = (XMLSchemaLoader)schemautils.getXSLoader(xserr, xsresolve);

      int index = 0;
      SchemaGrammar[] gs = new SchemaGrammar[locs.size()];
      Iterator<String> it = locs.keySet().iterator();
      while (it.hasNext())
      {
         try
         {
            String nsURI = it.next();
            URL orgURL = locs.get(nsURI); 
            URL resURL = resolveNamespaceURI(resolver, nsURI);
            URL url = resURL != null ? resURL : orgURL;
            
            log.debug("Load schema: " + nsURI + "=" + url);
            XMLInputSource inputSource = new XMLInputSource(null, url.toExternalForm(), null);

            InputSource tmpSrc = resolver.resolveEntity(null, url.toExternalForm());
            InputStream in = tmpSrc.getByteStream() != null ? tmpSrc.getByteStream() : new ResourceURL(url).openStream();
            inputSource.setByteStream(in);
            
            SchemaGrammar grammar = (SchemaGrammar)loader.loadGrammar(inputSource);
            if (grammar == null)
               throw new IllegalStateException("Cannot load grammar: " + url);
            
            gs[index++] = grammar;
         }
         catch (RuntimeException rte)
         {
            throw rte;
         }
         catch (Exception ex)
         {
            throw new IllegalStateException("Cannot parse schema", ex);
         }
      }
      XSModel xsmodel = new XSModelImpl(gs);

      // Convert Xerces XSModel into r/w JBossXSModel 
      WSSchemaUtils sutils = WSSchemaUtils.getInstance(null, null);
      JBossXSModel jbxs = new JBossXSModel();
      sutils.copyXSModel(xsmodel, jbxs);

      return jbxs;
   }

   private URL resolveNamespaceURI(JBossWSEntityResolver resolver, String nsURI)
   {
      URL url = null;

      String resource = (String)resolver.getEntityMap().get(nsURI);
      if (resource != null)
      {
         ClassLoader loader = Thread.currentThread().getContextClassLoader();
         url = loader.getResource(resource);
         if (url == null)
         {
            if (resource.endsWith(".dtd"))
               resource = "dtd/" + resource;
            else if (resource.endsWith(".xsd"))
               resource = "schema/" + resource;
            url = loader.getResource(resource);
         }
      }

      return url;
   }

   /**
    * @see org.jboss.ws.tools.interfaces.JavaToXSDIntf#setPackageNamespaceMap(java.util.Map)
    */
   public void setPackageNamespaceMap(Map<String, String> map)
   {
      helper.setPackageNamespaceMap(map);
   }

   /**
    * Set the WSDL Style
    */
   public void setWSDLStyle(String style)
   {
      helper.setWsdlStyle(style);
   }

   //******************************************************************
   //             PRIVATE METHODS
   //******************************************************************

   /**
    * FIXME: JBXB-33
    */
   private SchemaBindingResolver getSchemaBindingResolver(final Map<String, URL> map)
   {
      return new SchemaBindingResolver()
      {
         public String getBaseURI()
         {
            throw new UnsupportedOperationException("getBaseURI is not implemented.");
         }

         public void setBaseURI(String baseURI)
         {
            throw new UnsupportedOperationException("setBaseURI is not implemented.");
         }

         public SchemaBinding resolve(String nsUri, String baseURI, String schemaLocation)
         {
            throw new UnsupportedOperationException("resolve is not implemented.");
         }

         public LSInput resolveAsLSInput(String nsUri, String baseUri, String schemaLocation)
         {
            URL url = map.get(nsUri);
            if (url != null)
               try
               {
                  return new LSInputAdaptor(url.openStream(), null);
               }
               catch (IOException e)
               {
                  log.error("URL is bad for schema parsing");
               }
            return null;
         }
      };
   }
}