
com.predic8.membrane.annot.generator.Schemas Maven / Gradle / Ivy
/* Copyright 2013 predic8 GmbH, www.predic8.com
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
package com.predic8.membrane.annot.generator;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.processing.FilerException;
import javax.annotation.processing.ProcessingEnvironment;
import javax.lang.model.element.Element;
import javax.tools.FileObject;
import javax.tools.StandardLocation;
import com.predic8.membrane.annot.ProcessingException;
import com.predic8.membrane.annot.model.AbstractJavadocedInfo;
import com.predic8.membrane.annot.model.AttributeInfo;
import com.predic8.membrane.annot.model.ChildElementInfo;
import com.predic8.membrane.annot.model.ElementInfo;
import com.predic8.membrane.annot.model.MainInfo;
import com.predic8.membrane.annot.model.Model;
import com.predic8.membrane.annot.model.doc.Doc;
import com.predic8.membrane.annot.model.doc.Doc.Entry;
public class Schemas {
private ProcessingEnvironment processingEnv;
public Schemas(ProcessingEnvironment processingEnv) {
this.processingEnv = processingEnv;
}
public void writeXSD(Model m) throws IOException {
try {
for (MainInfo main : m.getMains()) {
List sources = new ArrayList();
sources.add(main.getElement());
sources.addAll(main.getInterceptorElements());
FileObject o = processingEnv.getFiler().createResource(StandardLocation.CLASS_OUTPUT,
main.getAnnotation().outputPackage(), main.getAnnotation().outputName(), sources.toArray(new Element[0]));
BufferedWriter bw = new BufferedWriter(o.openWriter());
try {
assembleXSD(bw, m, main);
} finally {
bw.close();
}
}
} catch (FilerException e) {
if (e.getMessage().contains("Source file already created"))
return;
throw e;
}
}
private void assembleXSD(Writer w, Model m, MainInfo main) throws IOException, ProcessingException {
String namespace = main.getAnnotation().targetNamespace();
w.append("\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
"\r\n" +
" \r\n" +
"\r\n" +
"\r\n" +
" \r\n" +
" \r\n" +
" \r\n" +
" \r\n" +
"\r\n" +
"\r\n" +
" \r\n" +
" \r\n" +
" \r\n" +
" \r\n\r\n");
assembleDeclarations(w, m, main);
w.append(" ");
}
private void assembleDeclarations(Writer w, Model m, MainInfo main) throws ProcessingException, IOException {
for (ElementInfo i : main.getElements().values())
assembleElementDeclaration(w, m, main, i);
}
private void assembleElementDeclaration(Writer w, Model m, MainInfo main, ElementInfo i) throws ProcessingException, IOException {
String footer;
if (i.getAnnotation().topLevel()) {
w.append("\r\n");
assembleDocumentation(w, i);
w.append("\r\n");
footer = " \r\n" +
" \r\n";
} else {
w.append("\r\n");
footer = " \r\n";
}
w.append("\r\n" +
"\r\n");
if (i.getAnnotation().mixed() && i.getCeis().size() > 0) {
throw new ProcessingException(
"@MCElement(..., mixed=true) and @MCTextContent is not compatible with @MCChildElement.",
i.getElement());
}
assembleElementInfo(w, m, main, i);
w.append(" \r\n" +
" \r\n");
w.append(footer);
}
private void assembleElementInfo(Writer w, Model m, MainInfo main, ElementInfo i) throws IOException {
w.append("\r\n");
for (ChildElementInfo cei : i.getCeis()) {
w.append("\r\n");
assembleDocumentation(w, i);
for (ElementInfo ei : main.getChildElementDeclarations().get(cei.getTypeDeclaration()).getElementInfo()) {
if (ei.getAnnotation().topLevel())
w.append("\r\n");
else
w.append("\r\n");
assembleDocumentation(w, ei);
w.append(" \r\n");
}
if (cei.getAnnotation().allowForeign())
w.append(" \r\n");
w.append(" \r\n");
}
w.append(" \r\n");
for (AttributeInfo ai : i.getAis())
if (!ai.getXMLName().equals("id"))
assembleAttributeDeclaration(w, ai);
if (i.getOai() != null) {
w.append("\r\n");
assembleDocumentation(w, i.getOai());
w.append(" \r\n");
}
}
private void assembleAttributeDeclaration(Writer w, AttributeInfo ai) throws IOException {
// TODO: default value
w.append("\r\n");
assembleDocumentation(w, ai);
w.append(" \r\n");
}
private void assembleDocumentation(Writer w, AbstractJavadocedInfo aji) throws IOException {
Doc doc = aji.getDoc(processingEnv);
if (doc == null)
return;
w.append("\r\n");
w.append("");
for (Entry e : doc.getEntries()) {
w.append(xmlEscape(""));
w.append(xmlEscape(capitalize(e.getKey()) + ":"));
w.append(xmlEscape("
"));
w.append(xmlEscape(e.getValueAsXMLSnippet(false)));
w.append(xmlEscape("
"));
}
w.append(" \r\n");
w.append(" \r\n");
}
private CharSequence xmlEscape(String string) {
return string.replace("<", "<").replace(">", ">");
}
private String capitalize(String key) {
if (key.length() == 0)
return key;
return Character.toUpperCase(key.charAt(0)) + key.substring(1);
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy