All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.predic8.membrane.annot.generator.Schemas Maven / Gradle / Ivy

There is a newer version: 5.8.0
Show newest version
/* 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 com.predic8.membrane.annot.*;
import com.predic8.membrane.annot.model.*;
import com.predic8.membrane.annot.model.doc.*;
import com.predic8.membrane.annot.model.doc.Doc.*;

import javax.annotation.processing.*;
import javax.lang.model.element.*;
import javax.tools.*;
import java.io.*;
import java.util.*;

public class Schemas {

	private final 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]));
                try (BufferedWriter bw = new BufferedWriter(o.openWriter())) {
                    assembleXSD(bw, m, main);
                }
			}
		} 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();
		String xsdHeaders = ("""
				
				
				
				
				
				
				
				    
				        
				    
				
				
				
				    
				        
				    
				
				
				""").formatted(namespace, namespace, Schemas.class.getName()).replace("\n", "\r\n");
		w.append("""
                
                """).append(xsdHeaders);
		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
					\r
					""";
		} else {
			w.append("\r\n");
			footer = "\r\n";
		}

		w.append("\r\n").append("\r\n");

		if (i.getAnnotation().mixed() && !i.getCeis().isEmpty()) {
			throw new ProcessingException(
					"@MCElement(..., mixed=true) and @MCTextContent is not compatible with @MCChildElement.",
					i.getElement());
		}
		assembleElementInfo(w, m, main, i);

		w.append("""
				\r
				\r
				""");
		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, cei);
			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");
		}

		// For mixed content like XML in template interceptor: e.g. 
		if (i.getAnnotation().mixed()) {
			w.append("");
		}

		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.isEmpty()) return key; return Character.toUpperCase(key.charAt(0)) + key.substring(1); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy