listener.message(Messages.format(Messages.PARSING_SCHEMA));
}
final boolean[] hadWarning = new boolean[1];
ErrorReceiver receiver = new ErrorReceiverFilter(listener) {
public void info(SAXParseException exception) {
if(opt.verbose)
super.info(exception);
}
public void warning(SAXParseException exception) {
hadWarning[0] = true;
if(!opt.quiet)
super.warning(exception);
}
@Override
public void pollAbort() throws AbortException {
if(listener.isCanceled())
throw new AbortException();
}
};
if( opt.mode==Mode.FOREST ) {
// dump DOM forest and quit
ModelLoader loader = new ModelLoader( opt, new JCodeModel(), receiver );
try {
DOMForest forest = loader.buildDOMForest(new XMLSchemaInternalizationLogic());
forest.dump(System.out);
return 0;
} catch (SAXException e) {
// the error should have already been reported
} catch (IOException e) {
receiver.error(e);
}
return -1;
}
if( opt.mode==Mode.GBIND ) {
try {
XSSchemaSet xss = new ModelLoader(opt, new JCodeModel(), receiver).loadXMLSchema();
Iterator<XSComplexType> it = xss.iterateComplexTypes();
while (it.hasNext()) {
XSComplexType ct = it.next();
XSParticle p = ct.getContentType().asParticle();
if(p==null) continue;
Expression tree = ExpressionBuilder.createTree(p);
System.out.println("Graph for "+ct.getName());
System.out.println(tree.toString());
Graph g = new Graph(tree);
System.out.println(g.toString());
System.out.println();
}
return 0;
} catch (SAXException e) {
// the error should have already been reported
}
return -1;
}
Model model = ModelLoader.load( opt, new JCodeModel(), receiver );
if (model == null) {
listener.message(Messages.format(Messages.PARSE_FAILED));
return -1;
}
if( !opt.quiet ) {
listener.message(Messages.format(Messages.COMPILING_SCHEMA));
}
switch (opt.mode) {
case SIGNATURE :
try {
SignatureWriter.write(
BeanGenerator.generate(model,receiver),
new OutputStreamWriter(System.out));
return 0;
} catch (IOException e) {
receiver.error(e);
return -1;
}
case CODE :
case DRYRUN :
case ZIP :
{
// generate actual code
receiver.debug("generating code");
{// don't want to hold outline in memory for too long.
Outline outline = model.generateCode(opt,receiver);
if(outline==null) {
listener.message(
Messages.format(Messages.FAILED_TO_GENERATE_CODE));
return -1;
}
listener.compiled(outline);
}
if( opt.mode == Mode.DRYRUN )
break; // enough
// then print them out
try {
CodeWriter cw;
if( opt.mode==Mode.ZIP ) {
OutputStream os;
if(opt.targetDir.getPath().equals("."))
os = System.out;
else
os = new FileOutputStream(opt.targetDir);
cw = opt.createCodeWriter(new ZipCodeWriter(os));
} else
cw = opt.createCodeWriter();
if( !opt.quiet ) {
cw = new ProgressCodeWriter(cw,listener, model.codeModel.countArtifacts());
}
model.codeModel.build(cw);
} catch (IOException e) {
receiver.error(e);
return -1;
}
break;
}
default :
assert false;
}
if(opt.debugMode) {
try {
new FileOutputStream(new File(opt.targetDir,hadWarning[0]?"hadWarning":"noWarning")).close();
} catch (IOException e) {
receiver.error(e);
return -1;
}
}
return 0;