/** * Copyright © 2006-2016 Web Cohesion (info@webcohesion.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.webcohesion.enunciate.modules.idl; import com.webcohesion.enunciate.api.InterfaceDescriptionFile; import com.webcohesion.enunciate.facets.FacetFilter; import com.webcohesion.enunciate.modules.jaxb.util.PrefixMethod; import com.webcohesion.enunciate.util.freemarker.IsFacetExcludedMethod; import freemarker.cache.URLTemplateLoader; import freemarker.core.Environment; import freemarker.template.Configuration; import freemarker.template.Template; import freemarker.template.TemplateException; import freemarker.template.TemplateExceptionHandler; import java.io.*; import java.net.MalformedURLException; import java.net.URL; import java.util.HashMap; import java.util.Map; /** * @author Ryan Heaton */ public abstract class BaseXMLInterfaceDescriptionFile implements InterfaceDescriptionFile { protected final FacetFilter facetFilter; protected final Map<String, String> namespacePrefixes; protected final String filename; private String contents; public BaseXMLInterfaceDescriptionFile(String filename, Map<String, String> namespacePrefixes, FacetFilter facetFilter) { this.namespacePrefixes = namespacePrefixes; this.facetFilter = facetFilter; this.filename = filename; } @Override public String getHref() { if (contents == null) { throw new IllegalStateException(String.format("%s hasn't been written yet.", filename)); } return filename; } @Override public void writeTo(File directory) throws IOException { FileWriter writer = new FileWriter(new File(directory, this.filename)); writeTo(writer); writer.flush(); writer.close(); } protected void writeTo(Writer writer) throws IOException { if (this.contents != null) { writer.write(this.contents); } else { Map<String, Object> model = createModel(); URL template = getTemplateURL(); String idl = processTemplate(template, model); writer.write(idl); } } protected Map<String, Object> createModel() { Map<String, Object> model = new HashMap<String, Object>(); model.put("prefix", new PrefixMethod(namespacePrefixes)); model.put("isFacetExcluded", new IsFacetExcludedMethod(this.facetFilter)); return model; } protected abstract URL getTemplateURL(); /** * Processes the specified template with the given model. * * @param model The root model. */ protected String processTemplate(URL templateURL, Map<String, Object> model) throws IOException { Configuration configuration = new Configuration(Configuration.VERSION_2_3_22); configuration.setTemplateLoader(new URLTemplateLoader() { protected URL getURL(String name) { try { return new URL(name); } catch (MalformedURLException e) { return null; } } }); configuration.setTemplateExceptionHandler(new TemplateExceptionHandler() { public void handleTemplateException(TemplateException templateException, Environment environment, Writer writer) throws TemplateException { throw templateException; } }); configuration.setLocalizedLookup(false); configuration.setDefaultEncoding("UTF-8"); configuration.setObjectWrapper(new IDLObjectWrapper(this.namespacePrefixes)); Template template = configuration.getTemplate(templateURL.toString()); StringWriter output = new StringWriter(); try { template.process(model, output); } catch (TemplateException e) { throw new RuntimeException(e); } String idl = output.toString(); this.contents = idl; return idl; } }