use of com.sun.xml.xsom.XSDeclaration in project jaxb-ri by eclipse-ee4j.
the class PluginImpl method run.
/**
* Capture all the generated classes from global schema components
* and generate them in an episode file.
*/
@Override
public boolean run(Outline model, Options opt, ErrorHandler errorHandler) throws SAXException {
OutputStream episodeFileOutputStream = null;
try {
// reorganize qualifying components by their namespaces to
// generate the list nicely
Map<XSSchema, PerSchemaOutlineAdaptors> perSchema = new LinkedHashMap<>();
boolean hasComponentInNoNamespace = false;
// Combine classes and enums into a single list
List<OutlineAdaptor> outlines = new ArrayList<>();
for (ClassOutline co : model.getClasses()) {
XSComponent sc = co.target.getSchemaComponent();
String fullName = co.implClass.fullName();
String packageName = co.implClass.getPackage().name();
OutlineAdaptor adaptor = new OutlineAdaptor(sc, OutlineAdaptor.OutlineType.CLASS, fullName, packageName);
outlines.add(adaptor);
}
for (EnumOutline eo : model.getEnums()) {
XSComponent sc = eo.target.getSchemaComponent();
String fullName = eo.clazz.fullName();
String packageName = eo.clazz.getPackage().name();
OutlineAdaptor adaptor = new OutlineAdaptor(sc, OutlineAdaptor.OutlineType.ENUM, fullName, packageName);
outlines.add(adaptor);
}
for (OutlineAdaptor oa : outlines) {
XSComponent sc = oa.schemaComponent;
if (sc == null)
continue;
if (!(sc instanceof XSDeclaration))
continue;
XSDeclaration decl = (XSDeclaration) sc;
if (decl.isLocal())
// local components cannot be referenced from outside, so no need to list.
continue;
PerSchemaOutlineAdaptors list = perSchema.get(decl.getOwnerSchema());
if (list == null) {
list = new PerSchemaOutlineAdaptors();
perSchema.put(decl.getOwnerSchema(), list);
}
list.add(oa);
if (decl.getTargetNamespace().equals(""))
hasComponentInNoNamespace = true;
}
episodeFileOutputStream = new FileOutputStream(episodeFile);
Bindings bindings = TXW.create(Bindings.class, new StreamSerializer(episodeFileOutputStream, "UTF-8"));
if (// otherwise jaxb binding NS should be the default namespace
hasComponentInNoNamespace)
bindings._namespace(Const.JAXB_NSURI, "jaxb");
else
bindings._namespace(Const.JAXB_NSURI, "");
bindings.version("3.0");
bindings._comment("\n\n" + opt.getPrologComment() + "\n ");
// generate listing per schema
for (Map.Entry<XSSchema, PerSchemaOutlineAdaptors> e : perSchema.entrySet()) {
PerSchemaOutlineAdaptors ps = e.getValue();
Bindings group = bindings.bindings();
String tns = e.getKey().getTargetNamespace();
if (!tns.equals(""))
group._namespace(tns, "tns");
group.scd("x-schema::" + (tns.equals("") ? "" : "tns"));
SchemaBindings schemaBindings = group.schemaBindings();
schemaBindings.map(false);
if (ps.packageNames.size() == 1) {
final String packageName = ps.packageNames.iterator().next();
if (packageName != null && packageName.length() > 0) {
schemaBindings._package().name(packageName);
}
}
for (OutlineAdaptor oa : ps.outlineAdaptors) {
Bindings child = group.bindings();
oa.buildBindings(child);
}
group.commit(true);
}
bindings.commit();
return true;
} catch (IOException e) {
errorHandler.error(new SAXParseException("Failed to write to " + episodeFile, null, e));
return false;
} finally {
if (episodeFileOutputStream != null) {
try {
episodeFileOutputStream.close();
} catch (IOException e) {
errorHandler.error(new SAXParseException("Failed to close file handle for " + episodeFile, null, e));
}
}
}
}
use of com.sun.xml.xsom.XSDeclaration in project jaxb-ri by eclipse-ee4j.
the class ClassSelector method _bindToClass.
/**
* The real meat of the "bindToType" code.
*
* @param cannotBeDelayed
* if the binding of the body of the class cannot be defered
* and needs to be done immediately. If the flag is false,
* the binding of the body will be done later, to avoid
* cyclic binding problem.
* @param referer
* The component that refers to {@code sc}. This can be null,
* if figuring out the referer is too hard, in which case
* the error message might be less user friendly.
*/
// TODO: consider getting rid of "cannotBeDelayed"
CTypeInfo _bindToClass(@NotNull XSComponent sc, XSComponent referer, boolean cannotBeDelayed) {
// check if this class is already built.
if (!bindMap.containsKey(sc)) {
// craete a bind task
// if this is a global declaration, make sure they will be generated
// under a package.
boolean isGlobal = false;
if (sc instanceof XSDeclaration) {
isGlobal = ((XSDeclaration) sc).isGlobal();
if (isGlobal)
pushClassScope(new CClassInfoParent.Package(getPackage(((XSDeclaration) sc).getTargetNamespace())));
}
// otherwise check if this component should become a class.
CElement bean = sc.apply(classBinder);
if (isGlobal)
popClassScope();
if (bean == null)
return null;
// can this namespace generate a class?
if (bean instanceof CClassInfo) {
XSSchema os = sc.getOwnerSchema();
BISchemaBinding sb = builder.getBindInfo(os).get(BISchemaBinding.class);
if (sb != null && !sb.map) {
// nope
getErrorReporter().error(sc.getLocator(), Messages.ERR_REFERENCE_TO_NONEXPORTED_CLASS, sc.apply(new ComponentNameFunction()));
getErrorReporter().error(sb.getLocation(), Messages.ERR_REFERENCE_TO_NONEXPORTED_CLASS_MAP_FALSE, os.getTargetNamespace());
if (referer != null)
getErrorReporter().error(referer.getLocator(), Messages.ERR_REFERENCE_TO_NONEXPORTED_CLASS_REFERER, referer.apply(new ComponentNameFunction()));
}
}
queueBuild(sc, bean);
}
Binding bind = bindMap.get(sc);
if (cannotBeDelayed)
bind.build();
return bind.bean;
}
Aggregations