/** * 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.jaxb; import com.webcohesion.enunciate.Enunciate; import com.webcohesion.enunciate.artifacts.BaseArtifact; import com.webcohesion.enunciate.modules.jaxb.model.SchemaInfo; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Date; import java.util.Map; import java.util.Properties; /** * @author Ryan Heaton */ public class NamespacePropertiesArtifact extends BaseArtifact { private final EnunciateJaxbContext jaxbContext; private final Date created = new Date(); public NamespacePropertiesArtifact(EnunciateJaxbContext jaxbContext) { super(JaxbModule.NAME, "namespaces.properties"); this.jaxbContext = jaxbContext; setBelongsOnServerSideClasspath(true); } @Override public String getName() { return "namespaces.properties"; } @Override public String getDescription() { return "A properties file that contains metadata about JAXB namespaces."; } @Override public boolean isPublic() { return false; } @Override public Date getCreated() { return this.created; } @Override public void exportTo(File fileOrDirectory, Enunciate enunciate) throws IOException { Properties properties = new Properties(); for (Map.Entry<String, String> ns2prefix : this.jaxbContext.getNamespacePrefixes().entrySet()) { if (ns2prefix.getKey() != null) { properties.put(ns2prefix.getKey(), ns2prefix.getValue()); } } String defaultNs = jaxbContext.getContext().getConfiguration().getDefaultNamespace(); if (defaultNs == null) { SchemaInfo schemaWithTheMostTypes = null; for (SchemaInfo schemaInfo : this.jaxbContext.getSchemas().values()) { if (schemaWithTheMostTypes == null) { schemaWithTheMostTypes = schemaInfo; } else if (schemaWithTheMostTypes.getTypeDefinitions().size() < schemaInfo.getTypeDefinitions().size()) { schemaWithTheMostTypes = schemaInfo; } } if (schemaWithTheMostTypes != null && schemaWithTheMostTypes.getNamespace() != null && !properties.containsValue(schemaWithTheMostTypes.getNamespace())) { defaultNs = schemaWithTheMostTypes.getNamespace(); } } if (defaultNs != null) { properties.put("{default}", defaultNs); } FileOutputStream out = new FileOutputStream(fileOrDirectory.isDirectory() ? new File(fileOrDirectory, getName()) : fileOrDirectory); properties.store(out, "Namespace properties, generated by Enunciate."); out.flush(); out.close(); } @Override public long getSize() { return -1; } }