use of org.glassfish.jaxb.core.v2.schemagen.episode.SchemaBindings 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));
}
}
}
}
Aggregations