Package org.apache.ws.jaxme.generator.sg.impl

Source Code of org.apache.ws.jaxme.generator.sg.impl.JAXBSchemaReader

/*
* The Apache Software License, Version 1.1
*
*
* Copyright (c) 2003,2004 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.sg.impl;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.ws.jaxme.generator.Generator;
import org.apache.ws.jaxme.generator.impl.SchemaReaderImpl;
import org.apache.ws.jaxme.generator.sg.SGFactory;
import org.apache.ws.jaxme.generator.sg.SGFactoryChain;
import org.apache.ws.jaxme.generator.sg.SchemaSG;
import org.apache.ws.jaxme.logging.Logger;
import org.apache.ws.jaxme.logging.LoggerAccess;
import org.apache.ws.jaxme.xs.XSParser;
import org.apache.ws.jaxme.xs.XSSchema;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;


/**
* @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
* @version $Id: JAXBSchemaReader.java 231705 2004-01-07 02:07:20Z jochen $
* @since JaxMe 2.0
*/
public class JAXBSchemaReader extends SchemaReaderImpl {
  private static final Logger log = LoggerAccess.getLogger(JAXBSchemaReader.class);
  private boolean isSupportingExtensions = false;
  private List sgFactoryChains = new ArrayList();

  public boolean isSupportingExtensions() {
    return isSupportingExtensions;
  }

  public void setSupportingExtensions(boolean pSupportingExtensions) {
    isSupportingExtensions = pSupportingExtensions;
  }

  public void addSGFactoryChain(Class pChainClass) {
    final String mName = "addSGFactoryChain";
    if (pChainClass == null) {
      throw new NullPointerException("The pChainClass argument must not be null.");
    }
    log.finest(mName, "->", pChainClass.getName());
    sgFactoryChains.add(pChainClass);
    log.finest(mName, "<-", Integer.toString(sgFactoryChains.size()));
  }

  protected SGFactoryChain newSGFactoryChain(Generator pGenerator) {
    return new JAXBSGFactory(pGenerator);
  }

  public SGFactory getSGFactory() throws SAXException {
    final String mName = "getSGFactory";
    log.finest(mName, "->");
    SGFactoryChain chain = newSGFactoryChain(getGenerator());
    log.finest(mName, "Created instance of " + chain.getClass().getName());
    for (Iterator iter = sgFactoryChains.iterator();  iter.hasNext()) {
      Class c = (Class) iter.next();
      log.finest(mName, "Adding instance of " + c.getName());
      Object o;
      try {
        Constructor con = c.getConstructor(new Class[]{SGFactoryChain.class});
        o = con.newInstance(new Object[]{chain});
      } catch (NoSuchMethodException e) {
        throw new SAXException("The SGFactoryChain class " + c.getName() +
                                " has no constructor taking the backing chain as an argument.");
      } catch (InvocationTargetException e) {
        Throwable t = e.getCause();
        String msg = "Failed to invoke the constructor of class " + c.getName() +
                     " with an argument of type " + chain.getClass().getName() +
                     ": " + t.getClass().getName() + ", " + t.getMessage();
        if (t instanceof Exception) {
          throw new SAXException(msg, (Exception) t);
        } else {
          throw new SAXException(msg, e);
        }
      } catch (Exception e) {
        throw new SAXException("Failed to invoke the constructor of class " + c.getName() +
                                " with an argument of type " + chain.getClass().getName() +
                                ": " + e.getClass().getName() + ", " + e.getMessage(), e);
      }
      chain = (SGFactoryChain) o;
    }
    SGFactory result = new SGFactoryImpl(chain);
    result.init();
    log.finest(mName, "<-", result);
    return result;
  }

  public SchemaSG parse(InputSource pSource) throws Exception {
    final String mName = "parse";
    log.finest(mName, "->", pSource.getSystemId());
    SGFactory factory = getSGFactory();
    XSParser parser = factory.newXSParser();
    log.finest(mName, "Parser = " + parser + ", validating = " + getGenerator().isValidating());
    parser.setValidating(getGenerator().isValidating());
    XSSchema schema = parser.parse(pSource);
    log.finest(mName, "Schema = " + schema);
    SchemaSG result = factory.getSchemaSG(schema);
    log.finest(mName, "<-", result);
    return result;
  }
}
TOP

Related Classes of org.apache.ws.jaxme.generator.sg.impl.JAXBSchemaReader

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.