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,33 @@
/*
* 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.reader.relaxng;
/**
* @author Kohsuke Kawaguchi
*/
enum BindStyle {
TYPE, ELEMENT
}

View File

@@ -0,0 +1,130 @@
/*
* 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.reader.relaxng;
import javax.xml.namespace.QName;
import com.sun.tools.internal.xjc.model.CAttributePropertyInfo;
import com.sun.tools.internal.xjc.model.CClassInfo;
import com.sun.tools.internal.xjc.model.CElementPropertyInfo;
import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
import com.sun.tools.internal.xjc.model.Multiplicity;
import com.sun.tools.internal.xjc.reader.RawTypeSet;
import com.sun.xml.internal.bind.v2.model.core.ID;
import com.sun.xml.internal.rngom.digested.DAttributePattern;
import com.sun.xml.internal.rngom.digested.DChoicePattern;
import com.sun.xml.internal.rngom.digested.DMixedPattern;
import com.sun.xml.internal.rngom.digested.DOneOrMorePattern;
import com.sun.xml.internal.rngom.digested.DOptionalPattern;
import com.sun.xml.internal.rngom.digested.DPattern;
import com.sun.xml.internal.rngom.digested.DPatternWalker;
import com.sun.xml.internal.rngom.digested.DZeroOrMorePattern;
import static com.sun.tools.internal.xjc.model.CElementPropertyInfo.CollectionMode.REPEATED_ELEMENT;
/**
* Recursively visits {@link DPattern} and
* decides which patterns to map to properties.
*
* @author Kohsuke Kawaguchi
*/
final class ContentModelBinder extends DPatternWalker {
private final RELAXNGCompiler compiler;
private final CClassInfo clazz;
private boolean insideOptional = false;
private int iota=1;
public ContentModelBinder(RELAXNGCompiler compiler,CClassInfo clazz) {
this.compiler = compiler;
this.clazz = clazz;
}
public Void onMixed(DMixedPattern p) {
throw new UnsupportedOperationException();
}
public Void onChoice(DChoicePattern p) {
boolean old = insideOptional;
insideOptional = true;
super.onChoice(p);
insideOptional = old;
return null;
}
public Void onOptional(DOptionalPattern p) {
boolean old = insideOptional;
insideOptional = true;
super.onOptional(p);
insideOptional = old;
return null;
}
public Void onZeroOrMore(DZeroOrMorePattern p) {
return onRepeated(p,true);
}
public Void onOneOrMore(DOneOrMorePattern p) {
return onRepeated(p,insideOptional);
}
private Void onRepeated(DPattern p,boolean optional) {
RawTypeSet rts = RawTypeSetBuilder.build(compiler, p, optional? Multiplicity.STAR : Multiplicity.PLUS);
if(rts.canBeTypeRefs==RawTypeSet.Mode.SHOULD_BE_TYPEREF) {
CElementPropertyInfo prop = new CElementPropertyInfo(
calcName(p),REPEATED_ELEMENT,ID.NONE,null,null,null,p.getLocation(),!optional);
rts.addTo(prop);
clazz.addProperty(prop);
} else {
CReferencePropertyInfo prop = new CReferencePropertyInfo(
calcName(p),true,!optional,false/*TODO*/,null,null,p.getLocation(), false, false, false);
rts.addTo(prop);
clazz.addProperty(prop);
}
return null;
}
public Void onAttribute(DAttributePattern p) {
// TODO: support multiple names
QName name = p.getName().listNames().iterator().next();
CAttributePropertyInfo ap = new CAttributePropertyInfo(
calcName(p), null,null/*TODO*/, p.getLocation(), name,
p.getChild().accept(compiler.typeUseBinder), null,
!insideOptional);
clazz.addProperty(ap);
return null;
}
private String calcName(DPattern p) {
// TODO
return "field"+(iota++);
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.reader.relaxng;
import java.util.HashMap;
import java.util.Map;
import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
import com.sun.tools.internal.xjc.model.TypeUse;
import com.sun.tools.internal.xjc.reader.xmlschema.SimpleTypeBuilder;
import com.sun.xml.internal.rngom.xml.util.WellKnownNamespaces;
/**
* Data-bindable datatype library.
*
* @author Kohsuke Kawaguchi
*/
final class DatatypeLib {
/**
* Datatype library's namespace URI.
*/
public final String nsUri;
private final Map<String,TypeUse> types = new HashMap<String,TypeUse>();
public DatatypeLib(String nsUri) {
this.nsUri = nsUri;
}
/**
* Maps the type name to the information.
*/
TypeUse get(String name) {
return types.get(name);
}
/**
* Datatype library for the built-in type.
*/
public static final DatatypeLib BUILTIN = new DatatypeLib("");
/**
* Datatype library for XML Schema datatypes.
*/
public static final DatatypeLib XMLSCHEMA = new DatatypeLib(WellKnownNamespaces.XML_SCHEMA_DATATYPES);
static {
BUILTIN.types.put("token",CBuiltinLeafInfo.TOKEN);
BUILTIN.types.put("string",CBuiltinLeafInfo.STRING);
XMLSCHEMA.types.putAll(SimpleTypeBuilder.builtinConversions);
}
}

View File

@@ -0,0 +1,61 @@
/*
* 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.reader.relaxng;
import java.util.HashSet;
import java.util.Set;
import com.sun.xml.internal.rngom.digested.DDefine;
import com.sun.xml.internal.rngom.digested.DGrammarPattern;
import com.sun.xml.internal.rngom.digested.DPatternWalker;
import com.sun.xml.internal.rngom.digested.DRefPattern;
/**
* Recursively find all {@link DDefine}s in the grammar.
*
* @author Kohsuke Kawaguchi
*/
final class DefineFinder extends DPatternWalker {
public final Set<DDefine> defs = new HashSet<DDefine>();
public Void onGrammar(DGrammarPattern p) {
for( DDefine def : p ) {
defs.add(def);
def.getPattern().accept(this);
}
return p.getStart().accept(this);
}
/**
* We visit all {@link DDefine}s from {@link DGrammarPattern},
* so no point in resolving refs.
*/
public Void onRef(DRefPattern p) {
return null;
}
}

View File

@@ -0,0 +1,35 @@
/*
* 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.reader.relaxng;
import com.sun.xml.internal.rngom.digested.DPatternWalker;
/**
* Decides the name for a particle.
* @author Kohsuke Kawaguchi
*/
class NameCalculator extends DPatternWalker {
}

View File

@@ -0,0 +1,270 @@
/*
* 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.reader.relaxng;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;
import com.sun.codemodel.internal.JCodeModel;
import com.sun.codemodel.internal.JPackage;
import com.sun.tools.internal.xjc.Options;
import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
import com.sun.tools.internal.xjc.model.CClassInfo;
import com.sun.tools.internal.xjc.model.CClassInfoParent;
import com.sun.tools.internal.xjc.model.CEnumConstant;
import com.sun.tools.internal.xjc.model.CEnumLeafInfo;
import com.sun.tools.internal.xjc.model.CNonElement;
import com.sun.tools.internal.xjc.model.CTypeInfo;
import com.sun.tools.internal.xjc.model.Model;
import com.sun.tools.internal.xjc.model.TypeUse;
import com.sun.xml.internal.bind.api.impl.NameConverter;
import com.sun.xml.internal.rngom.digested.DChoicePattern;
import com.sun.xml.internal.rngom.digested.DDefine;
import com.sun.xml.internal.rngom.digested.DElementPattern;
import com.sun.xml.internal.rngom.digested.DPattern;
import com.sun.xml.internal.rngom.digested.DPatternWalker;
import com.sun.xml.internal.rngom.digested.DRefPattern;
import com.sun.xml.internal.rngom.digested.DValuePattern;
import com.sun.xml.internal.rngom.nc.NameClass;
import com.sun.xml.internal.rngom.xml.util.WellKnownNamespaces;
/**
* @author Kohsuke Kawaguchi
*/
public final class RELAXNGCompiler {
/**
* Schema to compile.
*/
final DPattern grammar;
/**
* All named patterns in this schema.
*/
final Set<DDefine> defs;
final Options opts;
final Model model;
/**
* The package to which we generate the code into.
*/
final JPackage pkg;
final Map<String,DatatypeLib> datatypes = new HashMap<String, DatatypeLib>();
/**
* Patterns that are mapped to Java concepts.
*
* <p>
* The value is an array because we map elements with finite names
* to multiple classes.
*
* TODO: depending on the type of the key, the type of the values can be further
* restricted. Make this into its own class to represent those constraints better.
*/
final Map<DPattern,CTypeInfo[]> classes = new HashMap<DPattern,CTypeInfo[]>();
/**
* Classes that need to be bound.
*
* The value is the content model to be bound.
*/
final Map<CClassInfo,DPattern> bindQueue = new HashMap<CClassInfo,DPattern>();
final TypeUseBinder typeUseBinder = new TypeUseBinder(this);
public static Model build(DPattern grammar, JCodeModel codeModel, Options opts ) {
RELAXNGCompiler compiler = new RELAXNGCompiler(grammar, codeModel, opts);
compiler.compile();
return compiler.model;
}
public RELAXNGCompiler(DPattern grammar, JCodeModel codeModel, Options opts) {
this.grammar = grammar;
this.opts = opts;
this.model = new Model(opts,codeModel, NameConverter.smart,opts.classNameAllocator,null);
datatypes.put("",DatatypeLib.BUILTIN);
datatypes.put(WellKnownNamespaces.XML_SCHEMA_DATATYPES,DatatypeLib.XMLSCHEMA);
// find all defines
DefineFinder deff = new DefineFinder();
grammar.accept(deff);
this.defs = deff.defs;
if(opts.defaultPackage2!=null)
pkg = codeModel._package(opts.defaultPackage2);
else
if(opts.defaultPackage!=null)
pkg = codeModel._package(opts.defaultPackage);
else
pkg = codeModel.rootPackage();
}
private void compile() {
// decide which patterns to map to classes
promoteElementDefsToClasses();
promoteTypeSafeEnums();
// TODO: promote patterns with <jaxb:class> to classes
// TODO: promote 'type' patterns to classes
promoteTypePatternsToClasses();
for (Map.Entry<CClassInfo,DPattern> e : bindQueue.entrySet())
bindContentModel(e.getKey(),e.getValue());
}
private void bindContentModel(CClassInfo clazz, DPattern pattern) {
// first we decide which patterns in it map to properties
// then we process each of them by using RawTypeSetBuilder.
// much like DefaultParticleBinder in XSD
pattern.accept(new ContentModelBinder(this,clazz));
}
private void promoteTypeSafeEnums() {
// we'll be trying a lot of choices,
// and most of them will not be type-safe enum.
// using the same list improves the memory efficiency.
List<CEnumConstant> members = new ArrayList<CEnumConstant>();
OUTER:
for( DDefine def : defs ) {
DPattern p = def.getPattern();
if (p instanceof DChoicePattern) {
DChoicePattern cp = (DChoicePattern) p;
members.clear();
// check if the choice consists of all value patterns
// and that they are of the same datatype
DValuePattern vp = null;
for( DPattern child : cp ) {
if(child instanceof DValuePattern) {
DValuePattern c = (DValuePattern) child;
if(vp==null)
vp=c;
else {
if(!vp.getDatatypeLibrary().equals(c.getDatatypeLibrary())
|| !vp.getType().equals(c.getType()) )
continue OUTER; // different type name
}
members.add(new CEnumConstant(
model.getNameConverter().toConstantName(c.getValue()),
null, c.getValue(), null, null/*TODO*/, c.getLocation()
));
} else
continue OUTER; // not a value
}
if(members.isEmpty())
continue; // empty choice
CNonElement base = CBuiltinLeafInfo.STRING;
DatatypeLib lib = datatypes.get(vp.getNs());
if(lib!=null) {
TypeUse use = lib.get(vp.getType());
if(use instanceof CNonElement)
base = (CNonElement)use;
}
CEnumLeafInfo xducer = new CEnumLeafInfo(model, null,
new CClassInfoParent.Package(pkg), def.getName(), base,
new ArrayList<CEnumConstant>(members),
null, null/*TODO*/, cp.getLocation());
classes.put(cp,new CTypeInfo[]{xducer});
}
}
}
private void promoteElementDefsToClasses() {
// look for elements among named patterns
for( DDefine def : defs ) {
DPattern p = def.getPattern();
if (p instanceof DElementPattern) {
DElementPattern ep = (DElementPattern) p;
mapToClass(ep);
}
}
// also look for root elements
grammar.accept(new DPatternWalker() {
public Void onRef(DRefPattern p) {
return null; // stop recursion
}
public Void onElement(DElementPattern p) {
mapToClass(p);
return null;
}
});
}
private void mapToClass(DElementPattern p) {
NameClass nc = p.getName();
if(nc.isOpen())
return; // infinite name. can't map to a class.
Set<QName> names = nc.listNames();
CClassInfo[] types = new CClassInfo[names.size()];
int i=0;
for( QName n : names ) {
// TODO: read class names from customization
String name = model.getNameConverter().toClassName(n.getLocalPart());
bindQueue.put(
types[i++] = new CClassInfo(model,pkg,name,p.getLocation(),null,n,null,null/*TODO*/),
p.getChild() );
}
classes.put(p,types);
}
/**
* Looks for named patterns that are not bound to classes so far,
* but that can be bound to classes.
*/
private void promoteTypePatternsToClasses() {
// for( DDefine def : defs ) {
// ;
//
// def.getPattern().accept(new InheritanceChecker());
// }
}
}

View File

@@ -0,0 +1,76 @@
/*
* 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.reader.relaxng;
import com.sun.tools.internal.xjc.reader.Const;
import com.sun.tools.internal.xjc.reader.internalizer.AbstractReferenceFinderImpl;
import com.sun.tools.internal.xjc.reader.internalizer.DOMForest;
import com.sun.tools.internal.xjc.reader.internalizer.InternalizationLogic;
import org.w3c.dom.Element;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.XMLFilterImpl;
/**
* RELAX NG specific internalization logic.
*
* @author
* Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
*/
public class RELAXNGInternalizationLogic implements InternalizationLogic {
/**
* This filter looks for &lt;xs:import> and &lt;xs:include>
* and parses those documents referenced by them.
*/
private static final class ReferenceFinder extends AbstractReferenceFinderImpl {
ReferenceFinder( DOMForest parent ) {
super(parent);
}
protected String findExternalResource( String nsURI, String localName, Attributes atts) {
if( Const.RELAXNG_URI.equals(nsURI)
&& ("include".equals(localName) || "externalRef".equals(localName) ) )
return atts.getValue("href");
else
return null;
}
};
public XMLFilterImpl createExternalReferenceFinder(DOMForest parent) {
return new ReferenceFinder(parent);
}
public boolean checkIfValidTargetNode(DOMForest parent, Element bindings, Element target) {
return Const.RELAXNG_URI.equals(target.getNamespaceURI());
}
public Element refineTarget(Element target) {
// no refinement necessary
return target;
}
}

View File

@@ -0,0 +1,137 @@
/*
* 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.reader.relaxng;
import java.util.HashSet;
import java.util.Set;
import com.sun.tools.internal.xjc.model.CClassInfo;
import com.sun.tools.internal.xjc.model.CElementPropertyInfo;
import com.sun.tools.internal.xjc.model.CReferencePropertyInfo;
import com.sun.tools.internal.xjc.model.CTypeInfo;
import com.sun.tools.internal.xjc.model.CTypeRef;
import com.sun.tools.internal.xjc.model.Multiplicity;
import com.sun.tools.internal.xjc.reader.RawTypeSet;
import com.sun.xml.internal.bind.v2.model.core.ID;
import com.sun.xml.internal.rngom.digested.DAttributePattern;
import com.sun.xml.internal.rngom.digested.DElementPattern;
import com.sun.xml.internal.rngom.digested.DOneOrMorePattern;
import com.sun.xml.internal.rngom.digested.DPattern;
import com.sun.xml.internal.rngom.digested.DPatternWalker;
import com.sun.xml.internal.rngom.digested.DZeroOrMorePattern;
/**
* Builds {@link RawTypeSet} for RELAX NG.
*
* @author Kohsuke Kawaguchi
*/
public final class RawTypeSetBuilder extends DPatternWalker {
public static RawTypeSet build( RELAXNGCompiler compiler, DPattern contentModel, Multiplicity mul ) {
RawTypeSetBuilder builder = new RawTypeSetBuilder(compiler,mul);
contentModel.accept(builder);
return builder.create();
}
/**
* Multiplicity of the property.
*/
private Multiplicity mul;
/**
* Accumulates discovered {@link RawTypeSet.Ref}s.
*/
private final Set<RawTypeSet.Ref> refs = new HashSet<RawTypeSet.Ref>();
private final RELAXNGCompiler compiler;
public RawTypeSetBuilder(RELAXNGCompiler compiler,Multiplicity mul) {
this.mul = mul;
this.compiler = compiler;
}
private RawTypeSet create() {
return new RawTypeSet(refs,mul);
}
public Void onAttribute(DAttributePattern p) {
// attributes form their own properties
return null;
}
public Void onElement(DElementPattern p) {
CTypeInfo[] tis = compiler.classes.get(p);
if(tis!=null) {
for( CTypeInfo ti : tis )
refs.add(new CClassInfoRef((CClassInfo)ti));
} else {
// TODO
assert false;
}
return null;
}
public Void onZeroOrMore(DZeroOrMorePattern p) {
mul = mul.makeRepeated();
return super.onZeroOrMore(p);
}
public Void onOneOrMore(DOneOrMorePattern p) {
mul = mul.makeRepeated();
return super.onOneOrMore(p);
}
/**
* For {@link CClassInfo}s that map to elements.
*/
private static final class CClassInfoRef extends RawTypeSet.Ref {
private final CClassInfo ci;
CClassInfoRef(CClassInfo ci) {
this.ci = ci;
assert ci.isElement();
}
protected ID id() {
return ID.NONE;
}
protected boolean isListOfValues() {
return false;
}
protected RawTypeSet.Mode canBeType(RawTypeSet parent) {
return RawTypeSet.Mode.SHOULD_BE_TYPEREF;
}
protected void toElementRef(CReferencePropertyInfo prop) {
prop.getElements().add(ci);
}
protected CTypeRef toTypeRef(CElementPropertyInfo ep) {
return new CTypeRef(ci,ci.getElementName(),ci.getTypeName(),false,null);
}
}
}

View File

@@ -0,0 +1,140 @@
/*
* 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.reader.relaxng;
import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import com.sun.xml.internal.rngom.digested.DAttributePattern;
import com.sun.xml.internal.rngom.digested.DChoicePattern;
import com.sun.xml.internal.rngom.digested.DDefine;
import com.sun.xml.internal.rngom.digested.DListPattern;
import com.sun.xml.internal.rngom.digested.DMixedPattern;
import com.sun.xml.internal.rngom.digested.DOneOrMorePattern;
import com.sun.xml.internal.rngom.digested.DOptionalPattern;
import com.sun.xml.internal.rngom.digested.DPatternWalker;
import com.sun.xml.internal.rngom.digested.DRefPattern;
import com.sun.xml.internal.rngom.digested.DZeroOrMorePattern;
/**
* Fumigate the named patterns that can be bound to inheritance.
*
* @author Kohsuke Kawaguchi
*/
final class TypePatternBinder extends DPatternWalker {
private boolean canInherit;
private final Stack<Boolean> stack = new Stack<Boolean>();
/**
* Patterns that are determined not to be bindable to inheritance.
*/
private final Set<DDefine> cannotBeInherited = new HashSet<DDefine>();
void reset() {
canInherit = true;
stack.clear();
}
public Void onRef(DRefPattern p) {
if(!canInherit) {
cannotBeInherited.add(p.getTarget());
} else {
// if the whole pattern is like "A,B", we can only inherit from
// either A or B. For now, always derive from A.
// it might be worthwhile to have a smarter binding logic where
// we pick A and B based on their 'usefulness' --- by taking into
// account how many other paterns are derived from those.
canInherit = false;
}
return null;
}
/*
Set the flag to false if we hit a pattern that cannot include
a <ref> to be bound as an inheritance.
All the following code are the same
*/
public Void onChoice(DChoicePattern p) {
push(false);
super.onChoice(p);
pop();
return null;
}
public Void onAttribute(DAttributePattern p) {
push(false);
super.onAttribute(p);
pop();
return null;
}
public Void onList(DListPattern p) {
push(false);
super.onList(p);
pop();
return null;
}
public Void onMixed(DMixedPattern p) {
push(false);
super.onMixed(p);
pop();
return null;
}
public Void onOneOrMore(DOneOrMorePattern p) {
push(false);
super.onOneOrMore(p);
pop();
return null;
}
public Void onZeroOrMore(DZeroOrMorePattern p) {
push(false);
super.onZeroOrMore(p);
pop();
return null;
}
public Void onOptional(DOptionalPattern p) {
push(false);
super.onOptional(p);
pop();
return null;
}
private void push(boolean v) {
stack.push(canInherit);
canInherit = v;
}
private void pop() {
canInherit = stack.pop();
}
}

View File

@@ -0,0 +1,172 @@
/*
* 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.reader.relaxng;
import com.sun.tools.internal.xjc.model.CBuiltinLeafInfo;
import com.sun.tools.internal.xjc.model.TypeUse;
import com.sun.tools.internal.xjc.model.TypeUseFactory;
import com.sun.xml.internal.rngom.digested.DAttributePattern;
import com.sun.xml.internal.rngom.digested.DChoicePattern;
import com.sun.xml.internal.rngom.digested.DContainerPattern;
import com.sun.xml.internal.rngom.digested.DDataPattern;
import com.sun.xml.internal.rngom.digested.DElementPattern;
import com.sun.xml.internal.rngom.digested.DEmptyPattern;
import com.sun.xml.internal.rngom.digested.DGrammarPattern;
import com.sun.xml.internal.rngom.digested.DGroupPattern;
import com.sun.xml.internal.rngom.digested.DInterleavePattern;
import com.sun.xml.internal.rngom.digested.DListPattern;
import com.sun.xml.internal.rngom.digested.DMixedPattern;
import com.sun.xml.internal.rngom.digested.DNotAllowedPattern;
import com.sun.xml.internal.rngom.digested.DOneOrMorePattern;
import com.sun.xml.internal.rngom.digested.DOptionalPattern;
import com.sun.xml.internal.rngom.digested.DPattern;
import com.sun.xml.internal.rngom.digested.DPatternVisitor;
import com.sun.xml.internal.rngom.digested.DRefPattern;
import com.sun.xml.internal.rngom.digested.DTextPattern;
import com.sun.xml.internal.rngom.digested.DValuePattern;
import com.sun.xml.internal.rngom.digested.DZeroOrMorePattern;
/**
* Walks the pattern tree and binds it to a {@link TypeUse}.
*
* The singleton instance is kept in {@link RELAXNGCompiler}.
*
* TODO: I should really normalize before process.
*
* @author Kohsuke Kawaguchi
*/
final class TypeUseBinder implements DPatternVisitor<TypeUse> {
private final RELAXNGCompiler compiler;
public TypeUseBinder(RELAXNGCompiler compiler) {
this.compiler = compiler;
}
public TypeUse onGrammar(DGrammarPattern p) {
return CBuiltinLeafInfo.STRING;
}
public TypeUse onChoice(DChoicePattern p) {
// can't support unions
return CBuiltinLeafInfo.STRING;
}
public TypeUse onData(DDataPattern p) {
return onDataType(p.getDatatypeLibrary(), p.getType());
}
public TypeUse onValue(DValuePattern p) {
return onDataType(p.getDatatypeLibrary(),p.getType());
}
private TypeUse onDataType(String datatypeLibrary, String type) {
DatatypeLib lib = compiler.datatypes.get(datatypeLibrary);
if(lib!=null) {
TypeUse use = lib.get(type);
if(use!=null)
return use;
}
// unknown
return CBuiltinLeafInfo.STRING;
}
public TypeUse onInterleave(DInterleavePattern p) {
return onContainer(p);
}
public TypeUse onGroup(DGroupPattern p) {
return onContainer(p);
}
private TypeUse onContainer(DContainerPattern p) {
TypeUse t=null;
for( DPattern child : p ) {
TypeUse s = child.accept(this);
if(t!=null && t!=s)
return CBuiltinLeafInfo.STRING; // heterogenous
t = s;
}
return t;
}
public TypeUse onNotAllowed(DNotAllowedPattern p) {
// TODO
return error();
}
public TypeUse onEmpty(DEmptyPattern p) {
return CBuiltinLeafInfo.STRING;
}
public TypeUse onList(DListPattern p) {
return p.getChild().accept(this);
}
public TypeUse onOneOrMore(DOneOrMorePattern p) {
return TypeUseFactory.makeCollection( p.getChild().accept(this) );
}
public TypeUse onZeroOrMore(DZeroOrMorePattern p) {
return TypeUseFactory.makeCollection( p.getChild().accept(this) );
}
public TypeUse onOptional(DOptionalPattern p) {
return CBuiltinLeafInfo.STRING;
}
public TypeUse onRef(DRefPattern p) {
// TODO: check for enums
return p.getTarget().getPattern().accept(this);
}
public TypeUse onText(DTextPattern p) {
return CBuiltinLeafInfo.STRING;
}
//
//
// Not allowed in this context
//
//
public TypeUse onAttribute(DAttributePattern p) {
return error();
}
public TypeUse onElement(DElementPattern p) {
return error();
}
public TypeUse onMixed(DMixedPattern p) {
return error();
}
private TypeUse error() {
throw new IllegalStateException();
}
}