001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018: package org.apache.ivy.plugins.parser.m2;
019:
020: import java.io.File;
021: import java.io.FileOutputStream;
022: import java.io.IOException;
023: import java.io.OutputStreamWriter;
024: import java.io.PrintWriter;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import org.apache.ivy.Ivy;
029: import org.apache.ivy.core.module.descriptor.DependencyDescriptor;
030: import org.apache.ivy.core.module.descriptor.ModuleDescriptor;
031: import org.apache.ivy.core.module.id.ModuleRevisionId;
032: import org.apache.ivy.util.StringUtils;
033:
034: public final class PomModuleDescriptorWriter {
035: // used to ease tests only
036: private static boolean addIvyVersion = true;
037:
038: static void setAddIvyVersion(boolean addIvyVersion) {
039: PomModuleDescriptorWriter.addIvyVersion = addIvyVersion;
040: }
041:
042: private PomModuleDescriptorWriter() {
043: }
044:
045: public static void write(ModuleDescriptor md,
046: ConfigurationScopeMapping mapping, File output)
047: throws IOException {
048: write(md, null, mapping, output);
049: }
050:
051: public static void write(ModuleDescriptor md, String licenseHeader,
052: ConfigurationScopeMapping mapping, File output)
053: throws IOException {
054: if (output.getParentFile() != null) {
055: output.getParentFile().mkdirs();
056: }
057: PrintWriter out = new PrintWriter(new OutputStreamWriter(
058: new FileOutputStream(output), "UTF-8"));
059: try {
060: out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
061: if (licenseHeader != null) {
062: out.print(licenseHeader);
063: }
064: out.println("<!--");
065: out
066: .println(" Apache Maven 2 POM generated by Apache Ivy");
067: out.println(" " + Ivy.getIvyHomeURL());
068: if (addIvyVersion) {
069: out.println(" Apache Ivy version: "
070: + Ivy.getIvyVersion() + " " + Ivy.getIvyDate());
071: }
072: out.println("-->");
073: out
074: .println("<project xmlns=\"http://maven.apache.org/POM/4.0.0\" "
075: + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"");
076: out
077: .println(" xsi:schemaLocation=\"http://maven.apache.org/POM/4.0.0 "
078: + "http://maven.apache.org/maven-v4_0_0.xsd\">\n");
079: out.println(" <modelVersion>4.0.0</modelVersion>");
080: printModuleId(md, out);
081: printDependencies(md, mapping, out);
082: out.println("</project>");
083: } finally {
084: out.close();
085: }
086: }
087:
088: private static void printModuleId(ModuleDescriptor md,
089: PrintWriter out) {
090: ModuleRevisionId mrid = md.getModuleRevisionId();
091: out.println(" <groupId>" + mrid.getOrganisation()
092: + "</groupId>");
093: out
094: .println(" <artifactId>" + mrid.getName()
095: + "</artifactId>");
096: out.println(" <packaging>jar</packaging>");
097: if (mrid.getRevision() != null) {
098: out.println(" <version>" + mrid.getRevision()
099: + "</version>");
100: }
101: if (md.getHomePage() != null) {
102: out.println(" <url>" + md.getHomePage() + "</url>");
103: }
104: }
105:
106: private static void printDependencies(ModuleDescriptor md,
107: ConfigurationScopeMapping mapping, PrintWriter out) {
108: DependencyDescriptor[] dds = md.getDependencies();
109: if (dds.length > 0) {
110: out.println(" <dependencies>");
111: for (int i = 0; i < dds.length; i++) {
112: ModuleRevisionId mrid = dds[i]
113: .getDependencyRevisionId();
114: out.println(" <dependency>");
115: out.println(" <groupId>" + mrid.getOrganisation()
116: + "</groupId>");
117: out.println(" <artifactId>" + mrid.getName()
118: + "</artifactId>");
119: out.println(" <version>" + mrid.getRevision()
120: + "</version>");
121: String scope = mapping.getScope(dds[i]
122: .getModuleConfigurations());
123: if (scope != null) {
124: out.println(" <scope>" + scope + "</scope>");
125: }
126: if (mapping
127: .isOptional(dds[i].getModuleConfigurations())) {
128: out.println(" <optional>true</optional>");
129: }
130: out.println(" </dependency>");
131: }
132: out.println(" </dependencies>");
133: }
134: }
135:
136: public static final ConfigurationScopeMapping DEFAULT_MAPPING = new ConfigurationScopeMapping(
137: new HashMap() {
138: {
139: put("compile, runtime", "compile");
140: put("runtime", "runtime");
141: put("provided", "provided");
142: put("test", "test");
143: put("system", "system");
144: }
145: });
146:
147: public static class ConfigurationScopeMapping {
148: private Map/*<String,String>*/scopes;
149:
150: public ConfigurationScopeMapping(
151: Map/*<String,String>*/scopesMapping) {
152: this .scopes = new HashMap(scopesMapping);
153: }
154:
155: /**
156: * Returns the scope mapped to the given configuration array.
157: *
158: * @param confs the configurations for which the scope should be returned
159: * @return the scope to which the conf is mapped
160: */
161: public String getScope(String[] confs) {
162: return (String) scopes.get(StringUtils.join(confs, ", "));
163: }
164:
165: public boolean isOptional(String[] confs) {
166: return getScope(confs) == null;
167: }
168: }
169: }
|