feat(jdk8): move files to new folder to avoid resources compiled.

This commit is contained in:
2025-09-07 15:25:52 +08:00
parent 3f0047bf6f
commit 8c35cfb1c0
17415 changed files with 217 additions and 213 deletions

View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
/**
* Callback interface that allows the driver of the XJC API
* to rename JAXB-generated classes/interfaces/enums.
*
* @author Kohsuke Kawaguchi
*/
public interface ClassNameAllocator {
/**
* Hook that allows the client of the XJC API to rename some of the JAXB-generated classes.
*
* <p>
* When registered, this calllbcak is consulted for every package-level
* classes/interfaces/enums (hereafter, simply "classes")
* that the JAXB RI generates. Note that
* the JAXB RI does not use this allocator for nested/inner classes.
*
* <p>
* If the allocator chooses to rename some classes. It is
* the allocator's responsibility to find unique names.
* If the returned name collides with other classes, the JAXB RI will
* report errors.
*
* @param packageName
* The package name, such as "" or "foo.bar". Never be null.
* @param className
* The short name of the proposed class name. Such as
* "Foo" or "Bar". Never be null, never be empty.
* Always a valid Java identifier.
*
* @return
* The short name of the class name that should be used.
* The class will be generated into the same package with this name.
* The return value must be a valid Java identifier. May not be null.
*/
String assignClassName( String packageName, String className );
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXParseException;
/**
* Implemented by the driver of the compiler engine to handle
* errors found during the compiliation.
*
* <p>
* This class implements {@link ErrorHandler} so it can be
* passed to anywhere where {@link ErrorHandler} is expected.
*
* <p>
* However, to make the error handling easy (and make it work
* with visitor patterns nicely), this interface is not allowed
* to abort the processing. It merely receives errors.
*
* @author Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public interface ErrorListener extends com.sun.xml.internal.bind.api.ErrorListener {
void error(SAXParseException exception);
void fatalError(SAXParseException exception);
void warning(SAXParseException exception);
/**
* Used to report possibly verbose information that
* can be safely ignored.
*/
void info(SAXParseException exception);
}

View File

@@ -0,0 +1,82 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import java.io.IOException;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
/**
* {@link JAXBModel} that exposes additional information available
* only for the java->schema direction.
*
* @author Kohsuke Kawaguchi
*/
public interface J2SJAXBModel extends JAXBModel {
/**
* Returns the name of the XML Type bound to the
* specified Java type.
*
* @param javaType
* must not be null. This must be one of the {@link Reference}s specified
* in the {@link JavaCompiler#bind} method.
*
* @return
* null if it is not a part of the input to {@link JavaCompiler#bind}.
*
* @throws IllegalArgumentException
* if the parameter is null
*/
QName getXmlTypeName(Reference javaType);
/**
* Generates the schema documents from the model.
*
* @param outputResolver
* this object controls the output to which schemas
* will be sent.
*
* @throws IOException
* if {@link SchemaOutputResolver} throws an {@link IOException}.
*/
void generateSchema(SchemaOutputResolver outputResolver, ErrorListener errorListener) throws IOException;
/**
* Generates the episode file from the model.
*
* <p>
* The "episode file" is really just a JAXB customization file (but with vendor extensions,
* at this point), that can be used later with a schema compilation to support separate
* compilation.
*
* @param output
* This receives the generated episode file.
* @since 2.1
*/
void generateEpisodeFile(Result output);
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import java.util.List;
import javax.xml.bind.JAXBContext;
/**
* The in-memory representation of the JAXB binding.
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public interface JAXBModel {
/**
* Returns a list of fully-qualified class names, which should
* be used at the runtime to create a new {@link JAXBContext}.
*
* <p>
* Until the JAXB team fixes the bootstrapping issue, we have
* two bootstrapping methods. This one is to use a list of class names
* to call {@link JAXBContext#newInstance(Class[])} method. If
* this method returns non-null, the caller is expected to use
* that method. <b>This is meant to be a temporary workaround.</b>
*
* @return
* non-null read-only list.
*
* @deprecated
* this method is provided for now to allow gradual migration for JAX-RPC.
*/
List<String> getClassList();
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import java.util.Collection;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.annotation.processing.ProcessingEnvironment;
/**
* Java-to-Schema compiler.
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public interface JavaCompiler {
/**
* Compiles the given annotated Java source code.
*
* <p>
* This operation takes a set of "root types", then compute the list of
* all the types that need to be bound by forming a transitive reflexive
* closure of types that are referenced by the root types.
*
* <p>
* Errors will be sent to {@link javax.annotation.processing.ProcessingEnvironment#getMessager()}.
*
* @param rootTypes
* The list of types that needs to be bound to XML.
* "root references" from JAX-RPC to JAXB is always in the form of (type,annotations) pair.
*
* @param additionalElementDecls
* Add element declarations for the specified element names to
* the XML types mapped from the corresponding {@link Reference}s.
* Those {@link Reference}s must be included in the <tt>rootTypes</tt> parameter.
* In this map, a {@link Reference} can be null, in which case the element name is
* declared to have an empty complex type.
* (&lt;xs:element name='foo'>&lt;xs:complexType/>&lt;/xs:element>)
* This parameter can be null, in which case the method behaves as if the empty map is given.
*
* @param defaultNamespaceRemap
* If not-null, all the uses of the empty default namespace ("") will
* be replaced by this namespace URI.
*
* @param source
* The caller supplied view to the annotated source code that JAXB is going to process.
*
* @return
* Non-null if no error was reported. Otherwise null.
*/
J2SJAXBModel bind(
Collection<Reference> rootTypes,
Map<QName, Reference> additionalElementDecls,
String defaultNamespaceRemap,
ProcessingEnvironment source);
}

View File

@@ -0,0 +1,99 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import java.util.List;
import javax.xml.namespace.QName;
/**
* JAXB-induced mapping between a Java class
* and an XML element declaration. A part of the compiler artifacts.
*
* <p>
* To be precise, this is a mapping between two Java classes and an
* XML element declaration. There's one Java class/interface that
* represents the element, and there's another Java class/interface that
* represents the type of the element.
*
* The former is called "element representation" and the latter is called
* "type representation".
*
* <p>
* The {@link Mapping} interface provides operation that lets the caller
* convert an instance of the element representation to that of the
* type representation or vice versa.
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public interface Mapping {
/**
* Name of the XML element.
*
* @return
* never be null.
*/
QName getElement();
/**
* Returns the fully-qualified name of the java class for the type of this element.
*
* TODO: does this method returns the name of the wrapper bean when it's qualified
* for the wrapper style? Seems no (consider &lt;xs:element name='foo' type='xs:long' />),
* but then how does JAX-RPC captures that bean?
*
* @return
* never be null.
*/
TypeAndAnnotation getType();
/**
* If this element is a so-called "wrapper-style" element,
* obtains its member information.
*
* <p>
* The notion of the wrapper style should be defined by the JAXB spec,
* and ideally it should differ from that of the JAX-RPC only at
* the point where the JAX-RPC imposes additional restriction
* on the element name.
*
* <p>
* As of this writing the JAXB spec doesn't define "the wrapper style"
* and as such the exact definition of what XJC thinks
* "the wrapper style" isn't spec-ed.
*
* <p>
* Ths returned list includes {@link Property} defined not just
* in this class but in all its base classes.
*
* @return
* null if this isn't a wrapper-style element.
* Otherwise list of {@link Property}s. The order signifies
* the order they appeared inside a schema.
*/
List<? extends Property> getWrapperStyleDrilldown();
}

View File

@@ -0,0 +1,87 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import javax.xml.namespace.QName;
import com.sun.codemodel.internal.JType;
/**
* Represents a property of a wrapper-style element.
*
* <p>
* Carrys information about one property of a wrapper-style
* element. This interface is solely intended for the use by
* the JAX-RPC and otherwise the use is discouraged.
*
* <p>
* REVISIT: use CodeModel.
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
* @see Mapping
*/
public interface Property {
/**
* The name of the property.
*
* <p>
* This method returns a valid identifier suitable for
* the use as a variable name.
*
* @return
* always non-null. Camel-style name like "foo" or "barAndZot".
* Note that it may contain non-ASCII characters (CJK, etc.)
* The caller is responsible for proper escaping if it
* wants to print this as a variable name.
*/
String name();
/**
* The Java type of the property.
*
* @return
* always non-null.
* {@link JType} is a representation of a Java type in a codeModel.
* If you just need the fully-qualified class name, call {@link JType#fullName()}.
*/
JType type();
/**
* Name of the XML element that corresponds to the property.
*
* <p>
* Each child of a wrapper style element corresponds with an
* element, and this method returns that name.
*
* @return
* always non-null valid {@link QName}.
*/
QName elementName();
QName rawName();
}

View File

@@ -0,0 +1,106 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.lang.model.element.ExecutableElement;
import javax.lang.model.element.TypeElement;
import javax.lang.model.element.VariableElement;
import javax.lang.model.type.TypeMirror;
/**
* Reference to a JAXB type (from JAX-RPC.)
*
* <p>
* A reference is a Java type (represented as a {@link javax.lang.model.type.TypeMirror})
* and a set of annotations (represented as a {@link javax.lang.model.element.Element}).
* Together they describe a root reference to a JAXB type binding.
*
* <p>
* Those two values can be supplied independently, or you can use
* other convenience constructors to supply two values at once.
*
*
* @author Kohsuke Kawaguchi
*/
public final class Reference {
/**
* The JAXB type being referenced. Must not be null.
*/
public final TypeMirror type;
/**
* The declaration from which annotations for the {@link #type} is read.
* Must not be null.
*/
public final Element annotations;
/**
* Creates a reference from the return type of the method
* and annotations on the method.
*/
public Reference(ExecutableElement method) {
this(method.getReturnType(),method);
}
/**
* Creates a reference from the parameter type
* and annotations on the parameter.
*/
public Reference(VariableElement param) {
this(param.asType(), param);
}
/**
* Creates a reference from a class declaration and its annotations.
*/
public Reference(TypeElement type, ProcessingEnvironment env) {
this(env.getTypeUtils().getDeclaredType(type),type);
}
/**
* Creates a reference by providing two values independently.
*/
public Reference(TypeMirror type, Element annotations) {
if(type==null || annotations==null)
throw new IllegalArgumentException();
this.type = type;
this.annotations = annotations;
}
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Reference)) return false;
final Reference that = (Reference) o;
return annotations.equals(that.annotations) && type.equals(that.type);
}
public int hashCode() {
return 29 * type.hashCode() + annotations.hashCode();
}
}

View File

@@ -0,0 +1,112 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import javax.xml.bind.annotation.XmlSeeAlso;
import com.sun.codemodel.internal.CodeWriter;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.codemodel.internal.JClass;
import com.sun.tools.internal.xjc.Options;
import com.sun.tools.internal.xjc.Plugin;
/**
* {@link JAXBModel} that exposes additional information available
* only for the schema->java direction.
*
* @author Kohsuke Kawaguchi
*/
public interface S2JJAXBModel extends JAXBModel {
/**
* Gets a {@link Mapping} object for the given global element.
*
* @return
* null if the element name is not a defined global element in the schema.
*/
Mapping get( QName elementName );
/**
* Gets all the <tt>ObjectFactory</tt> classes generated by the compilation.
*
* <p>
* This should be used for generating {@link XmlSeeAlso} on the SEI.
*/
List<JClass> getAllObjectFactories();
/**
* Gets a read-only view of all the {@link Mapping}s.
*/
Collection<? extends Mapping> getMappings();
/**
* Returns the fully-qualified name of the Java type that is bound to the
* specified XML type.
*
* @param xmlTypeName
* must not be null.
* @return
* null if the XML type is not bound to any Java type.
*/
TypeAndAnnotation getJavaType(QName xmlTypeName);
/**
* Generates artifacts.
*
* <p>
* TODO: if JAXB supports various modes of code generations
* (such as public interface only or implementation only or
* etc), we should define bit flags to control those.
*
* <p>
* This operation is only supported for a model built from a schema.
*
* @param extensions
* The JAXB RI extensions to run. This can be null or empty
* array if the caller wishes not to run any extension.
* <br>
*
* Those specified extensions
* will participate in the code generation. Specifying an extension
* in this list has the same effect of turning that extension on
* via command line.
* <br>
*
* It is the caller's responsibility to configure each augmenter
* properly by using {@link Plugin#parseArgument(Options, String[], int)}.
*
* @return
* object filled with the generated code. Use
* {@link JCodeModel#build(CodeWriter)} to write them
* to a disk.
*/
JCodeModel generateCode( Plugin[] extensions, ErrorListener errorListener );
}

View File

@@ -0,0 +1,254 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import com.sun.istack.internal.NotNull;
import com.sun.tools.internal.xjc.Options;
import org.w3c.dom.Element;
import org.xml.sax.ContentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
/**
* Schema-to-Java compiler.
*
* <p>
* The caller can parse multiple schema documents,
* JAXB external binding files (or potentially WSDL
* and JSR-109.next mapping files in the future).
*
* <p>
* All the errors found during this process will be sent
* to the registered {@link ErrorListener}.
*
* <p>
* Once all the documents are parsed, call the {@link #bind()}
* method to get the compiled {@link JAXBModel} object.
*
*
* <h2>Tips: namespace URI -> package customization</h2>
* <p>
* The caller can feed the following synthesized schema
* to achive the namespace URI -> Java package customization:
* <pre><xmp>
* <schema targetNamespace="xml.namespace.uri"
* xmlns="http://www.w3.org/2001/XMLSchema"
* xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
* jaxb:version="1.0">
* <annotation><appinfo>
* <jaxb:schemaBindings>
* <jaxb:package name="java.package.name"/>
* </jaxb:schemaBindings>
* </appinfo></annotation>
* </schema>
* </xmp></pre>
* Feed this synthesized schema document for each namespace URI
* you need to map.
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public interface SchemaCompiler {
/**
* Parses schemas or external bindings
* through SAX events by feeding events into
* SAX {@link ContentHandler}.
*
* @param systemId
* The system ID of the document to be read in.
*
* @see #parseSchema(String, XMLStreamReader)
*/
ContentHandler getParserHandler( String systemId );
/**
* Parses a schema or an external binding file
* from an external source.
*
* @param source
* Its system Id must be set to an absolute URI.
*/
void parseSchema( InputSource source );
/**
* Specifies the target spec version for this compilaion.
*
* @param version
* If null, XJC will generate the source code that
* takes advantage of the latest JAXB spec that it understands.
* @since 2.1 EA2
*/
void setTargetVersion( SpecVersion version );
/**
* Parses a schema or an external binding file
* from the specified DOM element.
*
* <p>
* The given DOM element is treated as if it's the root of a
* virtual document.
*
* <p>
* XJC will not be able to print location information for
* errors found in this document, since DOM doesn't have them.
* For this reason, use of this method is strongly discouraged.
*
* @param systemId
* We need an absolute system ID that uniquely designates the virtual
* document. This should be different from the system ID of
* the document which contains this element.
* <p>
* One way to do that is by adding a fragment identifier
* to the system ID of the document. For example, if the document
* is "foo.wsdl" and you are passing in its types section, you
* can use an unique identifier like "foo.wsdl#types"
*/
void parseSchema( String systemId, Element element );
/**
* Parses a schema or an external binding file
* from the given source.
*
* <p>
* A stream reader must be pointing at the element or
* at the start of the document.
* XML is parsed until the corresponding end tag, then the
* sub tree is processed as a schema document.
*
* <p>
* When this method returns successfully, the parser is at
* the next token of the end element.
*
* @param systemId
* The absolute system ID of the document that is being parsed.
* This information is necessary to avoid double-inclusion
* and etc.
*
* Note that {@link XMLStreamReader#getLocation()} only
* returns the system ID of the entity it is parsing, not
* necessarily the system ID of the document itself.
*
* @throws XMLStreamException
* If an error happens while parsing a document.
* Note that not only the parser but also the XJC itself
* may throw this error (as a result of the additional validation
* for example.)
*/
void parseSchema( String systemId, XMLStreamReader reader ) throws XMLStreamException;
void setErrorListener( ErrorListener errorListener );
void setEntityResolver( EntityResolver entityResolver );
/**
* Sets the default Java package name into which the generated code will be placed.
*
* <p>
* Customizations in the binding files/schemas will have precedence over this setting.
* Set to null to use the default package name computation algorithm as specified by
* the JAXB spec (which is the default behavior.)
*
* <p>
* Initially this parameter is set to null.
*
* @param packageName
* Java pckage name such as "org.foo.bar". Use "" to represent the root package,
* and null to defer to the default computation algorithm.
*
* @see #forcePackageName(String)
*/
void setDefaultPackageName( String packageName );
/**
* Forces all the JAXB-generated classes to go into the specific package.
*
* <p>
* This setting takes precedence over the {@link #setDefaultPackageName(String)}
* or any of the customization found in the JAXB binding files. This method
* is designed to implement the semantics of the command-line '-p' option.
*
* <p>
* This somewhat ugly semantics actually have a long history now and too late
* to change.
*
* @see #setDefaultPackageName(String)
*/
void forcePackageName( String packageName );
/**
* Sets the {@link ClassNameAllocator} to be used for the binding operation.
*
* <p>
* This mechanism would allow the caller to participate in the binding operation.
*
* @see ClassNameAllocator
*/
void setClassNameAllocator( ClassNameAllocator allocator );
/**
* Clears all the schema files parsed so far.
*
* @since 2.1.1
*/
void resetSchema();
/**
* Obtains the compiled schema object model.
*
* Once this method is called, no other method should be
* invoked on the {@link SchemaCompiler}.
*
* @return
* null if the compilation fails. The errors should have been
* delivered to the registered error handler in such a case.
*/
S2JJAXBModel bind();
/**
* Allows the calling code to tweak more schema compilation details.
*
* <p>
* The caller can use this method to obtain an {@link Options} instance,
* then tweak settings on it. The updated settings will be used when the
* {@link #bind()} method is invoked.
*
* <p>
* The returned {@link Options} object is useful for example to specify
* command-line arguments.
*
* @since 2.0.2
* @deprecated
* This method is not really "deprecated" (in the sense of being removed
* from future versions), but the JAXB team is not committed to evolve
* {@link Options} class in the compatible fashion. So please don't
* use this method unless you know what you're doing.
*/
@NotNull Options getOptions();
}

View File

@@ -0,0 +1,59 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
/**
* Represents the spec version constant.
*
* @author Kohsuke Kawaguchi
*/
public enum SpecVersion {
V2_0, V2_1, V2_2;
/**
* Returns true if this version is equal or later than the given one.
*/
public boolean isLaterThan(SpecVersion t) {
return this.ordinal()>=t.ordinal();
}
/**
* Parses "2.0", "2.1", and "2.2" into the {@link SpecVersion} object.
*
* @return null for parsing failure.
*/
public static SpecVersion parse(String token) {
if(token.equals("2.0"))
return V2_0;
if(token.equals("2.1"))
return V2_1;
if(token.equals("2.2"))
return V2_2;
return null;
}
public static final SpecVersion LATEST = V2_2;
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import com.sun.codemodel.internal.JAnnotatable;
import com.sun.codemodel.internal.JType;
/**
* Java type and associated JAXB annotations.
*
* @author Kohsuke Kawaguchi
*/
public interface TypeAndAnnotation {
/**
* Returns the Java type.
*
* <p>
* {@link JType} is a representation of a Java type in a codeModel.
* If you just need the fully-qualified class name, call {@link JType#fullName()}.
*
* @return
* never be null.
*/
JType getTypeClass();
/**
* Annotates the given program element by additional JAXB annotations that need to be there
* at the point of reference.
*/
void annotate( JAnnotatable programElement );
/**
* Two {@link TypeAndAnnotation} are equal if they
* has the same type and annotations.
*/
boolean equals(Object o);
}

View File

@@ -0,0 +1,66 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api;
import com.sun.tools.internal.xjc.api.impl.s2j.SchemaCompilerImpl;
import com.sun.xml.internal.bind.api.impl.NameConverter;
/**
* Entry point to the programatic API to access
* schema compiler (XJC) and schema generator (schemagen).
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public final class XJC {
/**
* Gets a fresh {@link SchemaCompiler}.
*
* @return
* always return non-null object.
*/
public static SchemaCompiler createSchemaCompiler() {
return new SchemaCompilerImpl();
}
/**
* Computes the namespace URI -> package name conversion
* as specified by the JAXB spec.
*
* @param namespaceUri
* Namespace URI. Can be empty, but must not be null.
* @return
* A Java package name (e.g., "foo.bar"). "" to represent the root package.
* This method returns null if the method fails to derive the package name
* (there are certain namespace URIs with which this algorithm does not
* work --- such as ":::" as the URI.)
*/
public static String getDefaultPackageName( String namespaceUri ) {
if(namespaceUri==null) throw new IllegalArgumentException();
return NameConverter.standard.toPackageName( namespaceUri );
}
}

View File

@@ -0,0 +1,188 @@
/*
* Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import javax.xml.namespace.QName;
import com.sun.tools.internal.xjc.api.Mapping;
import com.sun.tools.internal.xjc.api.Property;
import com.sun.tools.internal.xjc.model.CClassInfo;
import com.sun.tools.internal.xjc.model.CElement;
import com.sun.tools.internal.xjc.model.CElementInfo;
import com.sun.tools.internal.xjc.model.CElementPropertyInfo;
import com.sun.tools.internal.xjc.model.CPropertyInfo;
import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
import com.sun.tools.internal.xjc.model.CTypeRef;
import com.sun.xml.internal.bind.v2.model.core.ClassInfo;
import com.sun.xml.internal.bind.v2.model.core.ReferencePropertyInfo;
import com.sun.xml.internal.xsom.XSComplexType;
import com.sun.xml.internal.xsom.XSComponent;
import com.sun.xml.internal.xsom.XSContentType;
import com.sun.xml.internal.xsom.XSModelGroup;
import com.sun.xml.internal.xsom.XSParticle;
import com.sun.xml.internal.xsom.XSTerm;
/**
* Partial common implementation between {@link ElementMappingImpl} and {@link BeanMappingImpl}
*
* @author Kohsuke Kawaguchi
*/
abstract class AbstractMappingImpl<InfoT extends CElement> implements Mapping {
protected final JAXBModelImpl parent;
protected final InfoT clazz;
/**
* Lazily computed.
*
* @see #getWrapperStyleDrilldown()
*/
private List<Property> drilldown = null;
private boolean drilldownComputed = false;
protected AbstractMappingImpl(JAXBModelImpl parent, InfoT clazz) {
this.parent = parent;
this.clazz = clazz;
}
public final QName getElement() {
return clazz.getElementName();
}
public final String getClazz() {
return clazz.getType().fullName();
}
public final List<? extends Property> getWrapperStyleDrilldown() {
if (!drilldownComputed) {
drilldownComputed = true;
drilldown = calcDrilldown();
}
return drilldown;
}
protected abstract List<Property> calcDrilldown();
/**
* Derived classes can use this method to implement {@link #calcDrilldown}.
*/
protected List<Property> buildDrilldown(CClassInfo typeBean) {
//JAXWS 2.1 spec 2.3.1.2:
//Wrapper style if the wrapper elements only contain child elements,
//they must not contain xsd:choice
if (containingChoice(typeBean)) {
return null;
}
List<Property> result;
CClassInfo bc = typeBean.getBaseClass();
if (bc != null) {
result = buildDrilldown(bc);
if (result == null) {
return null; // aborted
}
} else {
result = new ArrayList<Property>();
}
for (CPropertyInfo p : typeBean.getProperties()) {
if (p instanceof CElementPropertyInfo) {
CElementPropertyInfo ep = (CElementPropertyInfo) p;
// wrong. A+,B,C is eligible for drill-down.
// if(ep.isCollection())
// // content model like A+,B,C is not eligible
// return null;
List<? extends CTypeRef> ref = ep.getTypes();
if (ref.size() != 1) {// content model like (A|B),C is not eligible
return null;
}
result.add(createPropertyImpl(ep, ref.get(0).getTagName()));
} else if (p instanceof ReferencePropertyInfo) {
CReferencePropertyInfo rp = (CReferencePropertyInfo) p;
Collection<CElement> elements = rp.getElements();
if (elements.size() != 1) {
return null;
}
CElement ref = elements.iterator().next();
if (ref instanceof ClassInfo) {
result.add(createPropertyImpl(rp, ref.getElementName()));
} else {
CElementInfo eref = (CElementInfo) ref;
if (!eref.getSubstitutionMembers().isEmpty()) {
return null; // elements with a substitution group isn't qualified for the wrapper style
}
// JAX-WS doesn't want to see JAXBElement, so we have to hide it for them.
ElementAdapter fr;
if (rp.isCollection()) {
fr = new ElementCollectionAdapter(parent.outline.getField(rp), eref);
} else {
fr = new ElementSingleAdapter(parent.outline.getField(rp), eref);
}
result.add(new PropertyImpl(this,
fr, eref.getElementName()));
}
} else {// to be eligible for the wrapper style, only elements are allowed.
// according to the JAX-RPC spec 2.3.1.2, element refs are disallowed
return null;
}
}
return result;
}
private boolean containingChoice(CClassInfo typeBean) {
XSComponent component = typeBean.getSchemaComponent();
if (component instanceof XSComplexType) {
XSContentType contentType = ((XSComplexType) component).getContentType();
XSParticle particle = contentType.asParticle();
if (particle != null) {
XSTerm term = particle.getTerm();
XSModelGroup modelGroup = term.asModelGroup();
if (modelGroup != null) {
return (modelGroup.getCompositor() == XSModelGroup.Compositor.CHOICE);
}
}
}
return false;
}
private Property createPropertyImpl(CPropertyInfo p, QName tagName) {
return new PropertyImpl(this,
parent.outline.getField(p), tagName);
}
}

View File

@@ -0,0 +1,63 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import java.util.List;
import com.sun.tools.internal.xjc.api.Mapping;
import com.sun.tools.internal.xjc.api.Property;
import com.sun.tools.internal.xjc.api.TypeAndAnnotation;
import com.sun.tools.internal.xjc.model.CClassInfo;
/**
* Partial implementation of {@link Mapping}
* for bean classes.
*
* @author Kohsuke Kawaguchi
*/
final class BeanMappingImpl extends AbstractMappingImpl<CClassInfo> {
private final TypeAndAnnotationImpl taa = new TypeAndAnnotationImpl(parent.outline,clazz);
BeanMappingImpl(JAXBModelImpl parent, CClassInfo classInfo) {
super(parent,classInfo);
assert classInfo.isElement();
}
public TypeAndAnnotation getType() {
return taa;
}
public final String getTypeClass() {
return getClazz();
}
public List<Property> calcDrilldown() {
if(!clazz.isOrdered())
return null; // all is not eligible for the wrapper style
return buildDrilldown(clazz);
}
}

View File

@@ -0,0 +1,55 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
/**
* {@link ErrorHandler} that reports all errors as warnings.
*
* @author Kohsuke Kawaguchi
*/
final class DowngradingErrorHandler implements ErrorHandler {
private final ErrorHandler core;
public DowngradingErrorHandler(ErrorHandler core) {
this.core = core;
}
public void warning(SAXParseException exception) throws SAXException {
core.warning(exception);
}
public void error(SAXParseException exception) throws SAXException {
core.warning(exception);
}
public void fatalError(SAXParseException exception) throws SAXException {
core.warning(exception);
}
}

View File

@@ -0,0 +1,131 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import com.sun.tools.internal.xjc.outline.FieldOutline;
import com.sun.tools.internal.xjc.outline.ClassOutline;
import com.sun.tools.internal.xjc.outline.FieldAccessor;
import com.sun.tools.internal.xjc.outline.Aspect;
import com.sun.tools.internal.xjc.outline.Outline;
import com.sun.tools.internal.xjc.model.CPropertyInfo;
import com.sun.tools.internal.xjc.model.CElementInfo;
import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
import com.sun.codemodel.internal.JType;
import com.sun.codemodel.internal.JExpression;
import com.sun.codemodel.internal.JBlock;
import com.sun.codemodel.internal.JVar;
import com.sun.codemodel.internal.JConditional;
import com.sun.codemodel.internal.JExpr;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.codemodel.internal.JInvocation;
/**
* {@link FieldOutline} that wraps another {@link FieldOutline}
* and allows JAX-WS to access values without using about
* {@link JAXBElement}.
*
* <p>
* That means if a value is requested, we unwrap JAXBElement
* and give it to them. If a value is set, we wrap that into
* JAXBElement, etc.
*
* <p>
* This can be used only with {@link CReferencePropertyInfo}
* (or else it won't be {@link JAXBElement),
* with one {@link CElementInfo} (or else we can't infer the tag name.)
*
* @author Kohsuke Kawaguchi
*/
abstract class ElementAdapter implements FieldOutline {
protected final FieldOutline core;
/**
* The only one {@link CElementInfo} that can be in the property.
*/
protected final CElementInfo ei;
public ElementAdapter(FieldOutline core, CElementInfo ei) {
this.core = core;
this.ei = ei;
}
public ClassOutline parent() {
return core.parent();
}
public CPropertyInfo getPropertyInfo() {
return core.getPropertyInfo();
}
protected final Outline outline() {
return core.parent().parent();
}
protected final JCodeModel codeModel() {
return outline().getCodeModel();
}
protected abstract class FieldAccessorImpl implements FieldAccessor {
final FieldAccessor acc;
public FieldAccessorImpl(JExpression target) {
acc = core.create(target);
}
public void unsetValues(JBlock body) {
acc.unsetValues(body);
}
public JExpression hasSetValue() {
return acc.hasSetValue();
}
public FieldOutline owner() {
return ElementAdapter.this;
}
public CPropertyInfo getPropertyInfo() {
return core.getPropertyInfo();
}
/**
* Wraps a type value into a {@link JAXBElement}.
*/
protected final JInvocation createJAXBElement(JExpression $var) {
JCodeModel cm = codeModel();
return JExpr._new(cm.ref(JAXBElement.class))
.arg(JExpr._new(cm.ref(QName.class))
.arg(ei.getElementName().getNamespaceURI())
.arg(ei.getElementName().getLocalPart()))
.arg(getRawType().boxify().erasure().dotclass())
.arg($var);
}
}
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import com.sun.codemodel.internal.JBlock;
import com.sun.codemodel.internal.JClass;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.codemodel.internal.JConditional;
import com.sun.codemodel.internal.JExpr;
import com.sun.codemodel.internal.JExpression;
import com.sun.codemodel.internal.JForEach;
import com.sun.codemodel.internal.JType;
import com.sun.codemodel.internal.JVar;
import com.sun.tools.internal.xjc.model.CElementInfo;
import static com.sun.tools.internal.xjc.outline.Aspect.EXPOSED;
import com.sun.tools.internal.xjc.outline.FieldAccessor;
import com.sun.tools.internal.xjc.outline.FieldOutline;
/**
* {@link ElementAdapter} that works with a collection
* of {@link JAXBElement}.
*
* @author Kohsuke Kawaguchi
*/
final class ElementCollectionAdapter extends ElementAdapter {
public ElementCollectionAdapter(FieldOutline core, CElementInfo ei) {
super(core, ei);
}
public JType getRawType() {
return codeModel().ref(List.class).narrow(itemType().boxify());
}
private JType itemType() {
return ei.getContentInMemoryType().toType(outline(), EXPOSED);
}
public FieldAccessor create(JExpression targetObject) {
return new FieldAccessorImpl(targetObject);
}
final class FieldAccessorImpl extends ElementAdapter.FieldAccessorImpl {
public FieldAccessorImpl(JExpression target) {
super(target);
}
public void toRawValue(JBlock block, JVar $var) {
JCodeModel cm = outline().getCodeModel();
JClass elementType = ei.toType(outline(),EXPOSED).boxify();
// [RESULT]
// $var = new ArrayList();
// for( JAXBElement e : [core.toRawValue] ) {
// if(e==null)
// $var.add(null);
// else
// $var.add(e.getValue());
// }
block.assign($var,JExpr._new(cm.ref(ArrayList.class).narrow(itemType().boxify())));
JVar $col = block.decl(core.getRawType(), "col" + hashCode());
acc.toRawValue(block,$col);
JForEach loop = block.forEach(elementType, "v" + hashCode()/*unique string handling*/, $col);
JConditional cond = loop.body()._if(loop.var().eq(JExpr._null()));
cond._then().invoke($var,"add").arg(JExpr._null());
cond._else().invoke($var,"add").arg(loop.var().invoke("getValue"));
}
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
JCodeModel cm = outline().getCodeModel();
JClass elementType = ei.toType(outline(),EXPOSED).boxify();
// [RESULT]
// $t = new ArrayList();
// for( Type e : $var ) {
// $var.add(new JAXBElement(e));
// }
// [core.fromRawValue]
JClass col = cm.ref(ArrayList.class).narrow(elementType);
JVar $t = block.decl(col,uniqueName+"_col",JExpr._new(col));
JForEach loop = block.forEach(itemType(), uniqueName+"_i", $t);
loop.body().invoke($var,"add").arg(createJAXBElement(loop.var()));
acc.fromRawValue(block, uniqueName, $t);
}
}
}

View File

@@ -0,0 +1,91 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import java.util.List;
import com.sun.tools.internal.xjc.api.Property;
import com.sun.tools.internal.xjc.api.TypeAndAnnotation;
import com.sun.tools.internal.xjc.model.CAdapter;
import com.sun.tools.internal.xjc.model.CClassInfo;
import com.sun.tools.internal.xjc.model.CElementInfo;
import com.sun.tools.internal.xjc.model.CElementPropertyInfo;
import com.sun.tools.internal.xjc.model.CTypeInfo;
import com.sun.tools.internal.xjc.model.TypeUse;
import com.sun.tools.internal.xjc.model.TypeUseFactory;
/**
* @author Kohsuke Kawaguchi
*/
final class ElementMappingImpl extends AbstractMappingImpl<CElementInfo> {
private final TypeAndAnnotation taa;
protected ElementMappingImpl(JAXBModelImpl parent, CElementInfo elementInfo) {
super(parent,elementInfo);
TypeUse t = clazz.getContentType();
if(clazz.getProperty().isCollection())
t = TypeUseFactory.makeCollection(t);
CAdapter a = clazz.getProperty().getAdapter();
if(a!=null)
t = TypeUseFactory.adapt(t,a);
taa = new TypeAndAnnotationImpl(parent.outline,t);
}
public TypeAndAnnotation getType() {
return taa;
}
public final List<Property> calcDrilldown() {
CElementPropertyInfo p = clazz.getProperty();
if(p.getAdapter()!=null)
return null; // if adapted, avoid drill down
if(p.isCollection())
// things like <xs:element name="foo" type="xs:NMTOKENS" /> is not eligible.
return null;
CTypeInfo typeClass = p.ref().get(0);
if(!(typeClass instanceof CClassInfo))
// things like <xs:element name="foo" type="xs:string" /> is not eligible.
return null;
CClassInfo ci = (CClassInfo)typeClass;
// if the type is abstract we can't use it.
if(ci.isAbstract())
return null;
// the 'all' compositor doesn't qualify
if(!ci.isOrdered())
return null;
return buildDrilldown(ci);
}
}

View File

@@ -0,0 +1,88 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import com.sun.codemodel.internal.JType;
import com.sun.codemodel.internal.JBlock;
import com.sun.codemodel.internal.JVar;
import com.sun.codemodel.internal.JConditional;
import com.sun.codemodel.internal.JExpr;
import com.sun.codemodel.internal.JExpression;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.codemodel.internal.JInvocation;
import com.sun.tools.internal.xjc.outline.Aspect;
import com.sun.tools.internal.xjc.outline.FieldOutline;
import com.sun.tools.internal.xjc.outline.FieldAccessor;
import com.sun.tools.internal.xjc.model.CElementInfo;
/**
* {@link ElementAdapter} that works with a single {@link JAXBElement}.
*
* @author Kohsuke Kawaguchi
*/
final class ElementSingleAdapter extends ElementAdapter {
public ElementSingleAdapter(FieldOutline core, CElementInfo ei) {
super(core, ei);
}
public JType getRawType() {
return ei.getContentInMemoryType().toType(outline(), Aspect.EXPOSED);
}
public FieldAccessor create(JExpression targetObject) {
return new FieldAccessorImpl(targetObject);
}
final class FieldAccessorImpl extends ElementAdapter.FieldAccessorImpl {
public FieldAccessorImpl(JExpression target) {
super(target);
}
public void toRawValue(JBlock block, JVar $var) {
// [RESULT]
// if([core.hasSetValue])
// $var = [core.toRawValue].getValue();
// else
// $var = null;
JConditional cond = block._if(acc.hasSetValue());
JVar $v = cond._then().decl(core.getRawType(), "v" + hashCode());// TODO: unique value control
acc.toRawValue(cond._then(),$v);
cond._then().assign($var,$v.invoke("getValue"));
cond._else().assign($var, JExpr._null());
}
public void fromRawValue(JBlock block, String uniqueName, JExpression $var) {
// [RESULT]
// [core.fromRawValue](new JAXBElement(tagName, TYPE, $var));
acc.fromRawValue(block,uniqueName, createJAXBElement($var));
}
}
}

View File

@@ -0,0 +1,118 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.namespace.QName;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.codemodel.internal.JClass;
import com.sun.tools.internal.xjc.Plugin;
import com.sun.tools.internal.xjc.api.ErrorListener;
import com.sun.tools.internal.xjc.api.JAXBModel;
import com.sun.tools.internal.xjc.api.Mapping;
import com.sun.tools.internal.xjc.api.S2JJAXBModel;
import com.sun.tools.internal.xjc.api.TypeAndAnnotation;
import com.sun.tools.internal.xjc.model.CClassInfo;
import com.sun.tools.internal.xjc.model.CElementInfo;
import com.sun.tools.internal.xjc.model.Model;
import com.sun.tools.internal.xjc.model.TypeUse;
import com.sun.tools.internal.xjc.outline.Outline;
import com.sun.tools.internal.xjc.outline.PackageOutline;
/**
* {@link JAXBModel} implementation.
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
final class JAXBModelImpl implements S2JJAXBModel {
/*package*/ final Outline outline;
/**
* All the known classes.
*/
private final Model model;
private final Map<QName,Mapping> byXmlName = new HashMap<QName,Mapping>();
JAXBModelImpl(Outline outline) {
this.model = outline.getModel();
this.outline = outline;
for (CClassInfo ci : model.beans().values()) {
if(!ci.isElement())
continue;
byXmlName.put(ci.getElementName(),new BeanMappingImpl(this,ci));
}
for (CElementInfo ei : model.getElementMappings(null).values()) {
byXmlName.put(ei.getElementName(),new ElementMappingImpl(this,ei));
}
}
public JCodeModel generateCode(Plugin[] extensions,ErrorListener errorListener) {
// we no longer do any code generation
return outline.getCodeModel();
}
public List<JClass> getAllObjectFactories() {
List<JClass> r = new ArrayList<JClass>();
for (PackageOutline pkg : outline.getAllPackageContexts()) {
r.add(pkg.objectFactory());
}
return r;
}
public final Mapping get(QName elementName) {
return byXmlName.get(elementName);
}
public final Collection<? extends Mapping> getMappings() {
return byXmlName.values();
}
public TypeAndAnnotation getJavaType(QName xmlTypeName) {
// TODO: primitive type handling?
TypeUse use = model.typeUses().get(xmlTypeName);
if(use==null) return null;
return new TypeAndAnnotationImpl(outline,use);
}
public final List<String> getClassList() {
List<String> classList = new ArrayList<String>();
// list up root classes
for( PackageOutline p : outline.getAllPackageContexts() )
classList.add( p.objectFactory().fullName() );
return classList;
}
}

View File

@@ -0,0 +1,75 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import javax.xml.namespace.QName;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.codemodel.internal.JType;
import com.sun.tools.internal.xjc.api.Mapping;
import com.sun.tools.internal.xjc.api.Property;
import com.sun.tools.internal.xjc.model.CElementInfo;
import com.sun.tools.internal.xjc.outline.FieldOutline;
/**
* @author Kohsuke Kawaguchi
*/
public /*for BSH*/ final class PropertyImpl implements Property {
protected final FieldOutline fr;
protected final QName elementName;
protected final Mapping parent;
protected final JCodeModel codeModel;
PropertyImpl( Mapping parent, FieldOutline fr, QName elementName ) {
this.parent = parent;
this.fr = fr;
this.elementName = elementName;
this.codeModel = fr.getRawType().owner();
}
public final String name() {
return fr.getPropertyInfo().getName(false);
}
/** Returns raw schema name for simpleType property. May return null for other types. */
public final QName rawName() {
if (fr instanceof ElementAdapter) {
CElementInfo eInfo = ((ElementAdapter)fr).ei;
if ((eInfo != null) && (eInfo.getProperty() != null)) {
return eInfo.getProperty().getTypes().get(0).getTypeName();
}
}
return null;
}
public final QName elementName() {
return elementName;
}
public final JType type() {
return fr.getRawType();
}
}

View File

@@ -0,0 +1,324 @@
/*
* Copyright (c) 1997, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import javax.xml.XMLConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import javax.xml.validation.SchemaFactory;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.istack.internal.NotNull;
import com.sun.istack.internal.SAXParseException2;
import com.sun.tools.internal.xjc.ErrorReceiver;
import com.sun.tools.internal.xjc.ModelLoader;
import com.sun.tools.internal.xjc.Options;
import com.sun.tools.internal.xjc.api.ClassNameAllocator;
import com.sun.tools.internal.xjc.api.ErrorListener;
import com.sun.tools.internal.xjc.api.SchemaCompiler;
import com.sun.tools.internal.xjc.api.SpecVersion;
import com.sun.tools.internal.xjc.model.Model;
import com.sun.tools.internal.xjc.outline.Outline;
import com.sun.tools.internal.xjc.reader.internalizer.DOMForest;
import com.sun.tools.internal.xjc.reader.internalizer.SCDBasedBindingSet;
import com.sun.tools.internal.xjc.reader.xmlschema.parser.LSInputSAXWrapper;
import com.sun.tools.internal.xjc.reader.xmlschema.parser.XMLSchemaInternalizationLogic;
import com.sun.xml.internal.bind.unmarshaller.DOMScanner;
import com.sun.xml.internal.bind.v2.util.XmlFactory;
import com.sun.xml.internal.xsom.XSSchemaSet;
import org.w3c.dom.Element;
import org.w3c.dom.ls.LSInput;
import org.w3c.dom.ls.LSResourceResolver;
import org.xml.sax.ContentHandler;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.LocatorImpl;
/**
* {@link SchemaCompiler} implementation.
*
* This class builds a {@link DOMForest} until the {@link #bind()} method,
* then this method does the rest of the hard work.
*
* @see ModelLoader
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public final class SchemaCompilerImpl extends ErrorReceiver implements SchemaCompiler {
/**
* User-specified error receiver.
* This field can be null, in which case errors need to be discarded.
*/
private ErrorListener errorListener;
protected final Options opts = new Options();
protected @NotNull DOMForest forest;
/**
* Set to true once an error is found.
*/
private boolean hadError;
public SchemaCompilerImpl() {
opts.compatibilityMode = Options.EXTENSION;
resetSchema();
if(System.getProperty("xjc-api.test")!=null) {
opts.debugMode = true;
opts.verbose = true;
}
}
@NotNull
public Options getOptions() {
return opts;
}
public ContentHandler getParserHandler( String systemId ) {
return forest.getParserHandler(systemId,true);
}
public void parseSchema( String systemId, Element element ) {
checkAbsoluteness(systemId);
try {
DOMScanner scanner = new DOMScanner();
// use a locator that sets the system ID correctly
// so that we can resolve relative URLs in most of the case.
// it still doesn't handle xml:base and XInclude and all those things
// correctly. There's just no way to make all those things work with DOM!
LocatorImpl loc = new LocatorImpl();
loc.setSystemId(systemId);
scanner.setLocator(loc);
scanner.setContentHandler(getParserHandler(systemId));
scanner.scan(element);
} catch (SAXException e) {
// since parsing DOM shouldn't cause a SAX exception
// and our handler will never throw it, it's not clear
// if this will ever happen.
fatalError(new SAXParseException2(
e.getMessage(), null, systemId,-1,-1, e));
}
}
public void parseSchema(InputSource source) {
checkAbsoluteness(source.getSystemId());
try {
forest.parse(source,true);
} catch (SAXException e) {
// parsers are required to report an error to ErrorHandler,
// so we should never see this error.
e.printStackTrace();
}
}
public void setTargetVersion(SpecVersion version) {
if(version==null)
version = SpecVersion.LATEST;
opts.target = version;
}
public void parseSchema(String systemId, XMLStreamReader reader) throws XMLStreamException {
checkAbsoluteness(systemId);
forest.parse(systemId,reader,true);
}
/**
* Checks if the system ID is absolute.
*/
@SuppressWarnings("ResultOfObjectAllocationIgnored")
private void checkAbsoluteness(String systemId) {
// we need to be able to handle system IDs like "urn:foo", which java.net.URL can't process,
// but OTOH we also need to be able to process system IDs like "file://a b c/def.xsd",
// which java.net.URI can't process. So for now, let's fail only if both of them fail.
// eventually we need a proper URI class that works for us.
try {
new URL(systemId);
} catch( MalformedURLException mue) {
try {
new URI(systemId);
} catch (URISyntaxException e ) {
throw new IllegalArgumentException("system ID '"+systemId+"' isn't absolute",e);
}
}
}
public void setEntityResolver(EntityResolver entityResolver) {
forest.setEntityResolver(entityResolver);
opts.entityResolver = entityResolver;
}
public void setDefaultPackageName(String packageName) {
opts.defaultPackage2 = packageName;
}
public void forcePackageName(String packageName) {
opts.defaultPackage = packageName;
}
public void setClassNameAllocator(ClassNameAllocator allocator) {
opts.classNameAllocator = allocator;
}
public void resetSchema() {
forest = new DOMForest(new XMLSchemaInternalizationLogic(), opts);
forest.setErrorHandler(this);
forest.setEntityResolver(opts.entityResolver);
}
public JAXBModelImpl bind() {
// this has been problematic. turn it off.
// if(!forest.checkSchemaCorrectness(this))
// return null;
// parse all the binding files given via XJC -b options.
// this also takes care of the binding files given in the -episode option.
for (InputSource is : opts.getBindFiles())
parseSchema(is);
// internalization
SCDBasedBindingSet scdBasedBindingSet = forest.transform(opts.isExtensionMode());
if (!NO_CORRECTNESS_CHECK) {
// correctness check
SchemaFactory sf = XmlFactory.createSchemaFactory(XMLConstants.W3C_XML_SCHEMA_NS_URI, opts.disableXmlSecurity);
// fix for https://jaxb.dev.java.net/issues/show_bug.cgi?id=795
// taken from SchemaConstraintChecker, TODO XXX FIXME UGLY
if (opts.entityResolver != null) {
sf.setResourceResolver(new LSResourceResolver() {
public LSInput resolveResource(String type, String namespaceURI, String publicId, String systemId, String baseURI) {
try {
// XSOM passes the namespace URI to the publicID parameter.
// we do the same here .
InputSource is = opts.entityResolver.resolveEntity(namespaceURI, systemId);
if (is == null) return null;
return new LSInputSAXWrapper(is);
} catch (SAXException e) {
// TODO: is this sufficient?
return null;
} catch (IOException e) {
// TODO: is this sufficient?
return null;
}
}
});
}
sf.setErrorHandler(new DowngradingErrorHandler(this));
forest.weakSchemaCorrectnessCheck(sf);
if (hadError)
return null; // error in the correctness check. abort now
}
JCodeModel codeModel = new JCodeModel();
ModelLoader gl = new ModelLoader(opts,codeModel,this);
try {
XSSchemaSet result = gl.createXSOM(forest, scdBasedBindingSet);
if(result==null)
return null;
// we need info about each field, so we go ahead and generate the
// skeleton at this point.
// REVISIT: we should separate FieldRenderer and FieldAccessor
// so that accessors can be used before we build the code.
Model model = gl.annotateXMLSchema(result);
if(model==null) return null;
if(hadError) return null; // if we have any error by now, abort
model.setPackageLevelAnnotations(opts.packageLevelAnnotations);
Outline context = model.generateCode(opts,this);
if(context==null) return null;
if(hadError) return null;
return new JAXBModelImpl(context);
} catch( SAXException e ) {
// since XSOM uses our parser that scans DOM,
// no parser error is possible.
// all the other errors will be directed to ErrorReceiver
// before it's thrown, so when the exception is thrown
// the error should have already been reported.
// thus ignore.
return null;
}
}
public void setErrorListener(ErrorListener errorListener) {
this.errorListener = errorListener;
}
public void info(SAXParseException exception) {
if(errorListener!=null)
errorListener.info(exception);
}
public void warning(SAXParseException exception) {
if(errorListener!=null)
errorListener.warning(exception);
}
public void error(SAXParseException exception) {
hadError = true;
if(errorListener!=null)
errorListener.error(exception);
}
public void fatalError(SAXParseException exception) {
hadError = true;
if(errorListener!=null)
errorListener.fatalError(exception);
}
/**
* We use JAXP 1.3 to do a schema correctness check, but we know
* it doesn't always work. So in case some people hit the problem,
* this switch is here so that they can turn it off as a workaround.
*/
private static boolean NO_CORRECTNESS_CHECK = false;
static {
try {
NO_CORRECTNESS_CHECK = Boolean.getBoolean(SchemaCompilerImpl.class.getName()+".noCorrectnessCheck");
} catch( Throwable t) {
// ignore
}
}
}

View File

@@ -0,0 +1,113 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.impl.s2j;
import javax.xml.bind.annotation.XmlAttachmentRef;
import javax.xml.bind.annotation.XmlList;
import com.sun.codemodel.internal.JAnnotatable;
import com.sun.codemodel.internal.JPrimitiveType;
import com.sun.codemodel.internal.JType;
import com.sun.tools.internal.xjc.api.TypeAndAnnotation;
import com.sun.tools.internal.xjc.generator.annotation.spec.XmlJavaTypeAdapterWriter;
import com.sun.tools.internal.xjc.model.CAdapter;
import com.sun.tools.internal.xjc.model.TypeUse;
import com.sun.tools.internal.xjc.model.nav.NType;
import static com.sun.tools.internal.xjc.outline.Aspect.EXPOSED;
import com.sun.tools.internal.xjc.outline.Outline;
import com.sun.xml.internal.bind.v2.runtime.SwaRefAdapterMarker;
/**
* {@link TypeAndAnnotation} implementation.
*
* @author Kohsuke Kawaguchi
*/
final class TypeAndAnnotationImpl implements TypeAndAnnotation {
private final TypeUse typeUse;
private final Outline outline;
public TypeAndAnnotationImpl(Outline outline, TypeUse typeUse) {
this.typeUse = typeUse;
this.outline = outline;
}
public JType getTypeClass() {
CAdapter a = typeUse.getAdapterUse();
NType nt;
if(a!=null)
nt = a.customType;
else
nt = typeUse.getInfo().getType();
JType jt = nt.toType(outline,EXPOSED);
JPrimitiveType prim = jt.boxify().getPrimitiveType();
if(!typeUse.isCollection() && prim!=null)
jt = prim;
if(typeUse.isCollection())
jt = jt.array();
return jt;
}
public void annotate(JAnnotatable programElement) {
if(typeUse.getAdapterUse()==null && !typeUse.isCollection())
return; // nothing
CAdapter adapterUse = typeUse.getAdapterUse();
if(adapterUse!=null) {
// ugly, ugly hack
if(adapterUse.getAdapterIfKnown() == SwaRefAdapterMarker.class) {
programElement.annotate(XmlAttachmentRef.class);
} else {
// [RESULT]
// @XmlJavaTypeAdapter( Foo.class )
programElement.annotate2(XmlJavaTypeAdapterWriter.class).value(
adapterUse.adapterType.toType(outline,EXPOSED));
}
}
if(typeUse.isCollection())
programElement.annotate(XmlList.class);
}
public String toString() {
StringBuilder builder = new StringBuilder();
// TODO: support annotations
builder.append(getTypeClass());
return builder.toString();
}
public boolean equals(Object o) {
if (!(o instanceof TypeAndAnnotationImpl)) return false;
TypeAndAnnotationImpl that = (TypeAndAnnotationImpl) o;
return this.typeUse==that.typeUse;
}
public int hashCode() {
return typeUse.hashCode();
}
}

View File

@@ -0,0 +1,152 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.util;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.File;
import java.net.URL;
import java.net.URLClassLoader;
import java.net.MalformedURLException;
import com.sun.istack.internal.Nullable;
/**
* {@link ClassLoader} that loads Annotation Processing and specified classes
* both into the same classloader, so that they can reference each other.
*
* @author Bhakti Mehta
* @since 2.0 beta
*/
public final class ApClassLoader extends URLClassLoader {
/**
* List of package prefixes we want to mask the
* parent classLoader from loading
*/
private final String[] packagePrefixes;
/**
*
* @param packagePrefixes
* The package prefixes that are forced to resolve within this class loader.
* @param parent
* The parent class loader to delegate to. Null to indicate bootstrap classloader.
*/
public ApClassLoader(@Nullable ClassLoader parent, String[] packagePrefixes) throws ToolsJarNotFoundException {
super(getToolsJar(parent),parent);
if(getURLs().length==0)
// if tools.jar was found in our classloader, no need to create
// a parallel classes
this.packagePrefixes = new String[0];
else
this.packagePrefixes = packagePrefixes;
}
public Class loadClass(String className) throws ClassNotFoundException {
for( String prefix : packagePrefixes ) {
if (className.startsWith(prefix) ) {
// we need to load those classes in this class loader
// without delegation.
return findClass(className);
}
}
return super.loadClass(className);
}
protected Class findClass(String name) throws ClassNotFoundException {
StringBuilder sb = new StringBuilder(name.length() + 6);
sb.append(name.replace('.','/')).append(".class");
InputStream is = getResourceAsStream(sb.toString());
if (is==null)
throw new ClassNotFoundException("Class not found" + sb);
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len;
while((len=is.read(buf))>=0)
baos.write(buf,0,len);
buf = baos.toByteArray();
// define package if not defined yet
int i = name.lastIndexOf('.');
if (i != -1) {
String pkgname = name.substring(0, i);
Package pkg = getPackage(pkgname);
if(pkg==null)
definePackage(pkgname, null, null, null, null, null, null, null);
}
return defineClass(name,buf,0,buf.length);
} catch (IOException e) {
throw new ClassNotFoundException(name,e);
} finally {
try {
is.close();
} catch (IOException ioe) {
//ignore
}
}
}
/**
* Returns a class loader that can load classes from JDK tools.jar.
* @param parent
*/
private static URL[] getToolsJar(@Nullable ClassLoader parent) throws ToolsJarNotFoundException {
try {
Class.forName("com.sun.tools.javac.Main", false, parent);
return new URL[0];
// we can already load them in the parent class loader.
// so no need to look for tools.jar.
// this happens when we are run inside IDE/Ant, or
// in Mac OS.
} catch (ClassNotFoundException e) {
// otherwise try to find tools.jar
}
File jreHome = new File(System.getProperty("java.home"));
File toolsJar = new File( jreHome.getParent(), "lib/tools.jar" );
if (!toolsJar.exists()) {
throw new ToolsJarNotFoundException(toolsJar);
}
try {
return new URL[]{toolsJar.toURL()};
} catch (MalformedURLException e) {
// impossible
throw new AssertionError(e);
}
}
}

View File

@@ -0,0 +1,81 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.util;
import javax.tools.StandardLocation;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;
import com.sun.codemodel.internal.CodeWriter;
import com.sun.codemodel.internal.JPackage;
import javax.annotation.processing.Filer;
import static javax.tools.StandardLocation.CLASS_PATH;
import static javax.tools.StandardLocation.SOURCE_PATH;
/**
* {@link CodeWriter} that generates source code to {@link Filer}.
*
* @author Kohsuke Kawaguchi
*/
public final class FilerCodeWriter extends CodeWriter {
private final Filer filer;
public FilerCodeWriter(Filer filer) {
this.filer = filer;
}
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException {
StandardLocation loc;
if(fileName.endsWith(".java")) {
// Annotation Processing doesn't do the proper Unicode escaping on Java source files,
// so we can't rely on Filer.createSourceFile.
loc = SOURCE_PATH;
} else {
// put non-Java files directly to the output folder
loc = CLASS_PATH;
}
return filer.createResource(loc, pkg.name(), fileName).openOutputStream();
}
public Writer openSource(JPackage pkg, String fileName) throws IOException {
String name;
if(pkg.isUnnamed())
name = fileName;
else
name = pkg.name()+'.'+fileName;
name = name.substring(0,name.length()-5); // strip ".java"
return filer.createSourceFile(name).openWriter();
}
public void close() {
; // noop
}
}

View File

@@ -0,0 +1,48 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.util;
import java.text.MessageFormat;
import java.util.ResourceBundle;
/**
* Message resources
*/
enum Messages {
TOOLS_JAR_NOT_FOUND, // 1 arg
;
private static final ResourceBundle rb = ResourceBundle.getBundle(Messages.class.getName());
public String toString() {
return format();
}
public String format( Object... args ) {
return MessageFormat.format( rb.getString(name()), args );
}
}

View File

@@ -0,0 +1,51 @@
/*
* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code 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 General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.sun.tools.internal.xjc.api.util;
import java.io.File;
/**
* Signals an error when tools.jar was not found.
*
* Simply print out the message obtained by {@link #getMessage()}.
*
* @author Kohsuke Kawaguchi
*/
public final class ToolsJarNotFoundException extends Exception {
/**
* Location where we expected to find tools.jar
*/
public final File toolsJar;
public ToolsJarNotFoundException(File toolsJar) {
super(calcMessage(toolsJar));
this.toolsJar = toolsJar;
}
private static String calcMessage(File toolsJar) {
return Messages.TOOLS_JAR_NOT_FOUND.format(toolsJar.getPath());
}
}