View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.plugin.eclipse.writers.wtp;
20  
21  import java.io.File;
22  import java.io.FileOutputStream;
23  import java.io.IOException;
24  import java.io.OutputStreamWriter;
25  import java.io.Writer;
26  import java.util.Iterator;
27  import java.util.Map.Entry;
28  
29  import org.apache.maven.plugin.MojoExecutionException;
30  import org.apache.maven.plugin.eclipse.Constants;
31  import org.apache.maven.plugin.eclipse.Messages;
32  import org.apache.maven.plugin.ide.IdeUtils;
33  import org.apache.maven.plugin.ide.JeeUtils;
34  import org.codehaus.plexus.util.IOUtil;
35  import org.codehaus.plexus.util.xml.PrettyPrintXMLWriter;
36  import org.codehaus.plexus.util.xml.XMLWriter;
37  
38  /**
39   * Creates a .settings folder for Eclipse WTP 1.x release and writes out the configuration under it.
40   * 
41   * @author <a href="mailto:rahul.thakur.xdev@gmail.com">Rahul Thakur</a>
42   * @author <a href="mailto:fgiust@apache.org">Fabrizio Giustina</a>
43   * @version $Id: EclipseWtpFacetsWriter.java 597410 2007-11-22 13:59:27Z aheritier $
44   */
45  public class EclipseWtpFacetsWriter
46      extends AbstractWtpResourceWriter
47  {
48  
49      private static final String FACET_JST_EAR = "jst.ear"; //$NON-NLS-1$
50  
51      private static final String FACET_JST_UTILITY = "jst.utility"; //$NON-NLS-1$
52  
53      private static final String FACET_JST_EJB = "jst.ejb"; //$NON-NLS-1$
54  
55      private static final String FACET_JST_WEB = "jst.web"; //$NON-NLS-1$
56  
57      private static final String FACET_JST_JAVA = "jst.java"; //$NON-NLS-1$
58  
59      private static final String ATTR_VERSION = "version"; //$NON-NLS-1$
60  
61      private static final String ELT_INSTALLED = "installed"; //$NON-NLS-1$
62  
63      private static final String ATTR_FACET = "facet"; //$NON-NLS-1$
64  
65      private static final String ELT_FIXED = "fixed"; //$NON-NLS-1$
66  
67      private static final String ELT_FACETED_PROJECT = "faceted-project"; //$NON-NLS-1$
68  
69      /**
70       * The .settings folder for Web Tools Project 1.x release.
71       */
72      private static final String DIR_WTP_SETTINGS = ".settings"; //$NON-NLS-1$
73  
74      /**
75       * File name where Eclipse Project's Facet configuration will be stored.
76       */
77      private static final String FILE_FACET_CORE_XML = "org.eclipse.wst.common.project.facet.core.xml"; //$NON-NLS-1$
78  
79      /**
80       * @see org.apache.maven.plugin.eclipse.writers.EclipseWriter#write()
81       */
82      public void write()
83          throws MojoExecutionException
84      {
85  
86          // create a .settings directory (if not existing)
87          File settingsDir = new File( config.getEclipseProjectDirectory(), DIR_WTP_SETTINGS );
88          settingsDir.mkdirs();
89  
90          Writer w;
91  
92          String packaging = config.getPackaging();
93  
94          // Write out facet core xml
95          try
96          {
97              w = new OutputStreamWriter( new FileOutputStream( new File( settingsDir, FILE_FACET_CORE_XML ) ), "UTF-8" );
98          }
99          catch ( IOException ex )
100         {
101             throw new MojoExecutionException( Messages.getString( "EclipsePlugin.erroropeningfile" ), ex ); //$NON-NLS-1$
102         }
103         XMLWriter writer = new PrettyPrintXMLWriter( w );
104         writeModuleTypeFacetCore( writer, packaging );
105         IOUtil.close( w );
106     }
107 
108     /**
109      * Writes out the facet info for a faceted-project based on the packaging.
110      * 
111      * @param writer
112      * @param packaging
113      */
114     private void writeModuleTypeFacetCore( XMLWriter writer, String packaging )
115     {
116         writer.startElement( ELT_FACETED_PROJECT );
117         // common facet
118         writer.startElement( ELT_FIXED );
119         writer.addAttribute( ATTR_FACET, FACET_JST_JAVA );
120         writer.endElement(); // element fixed
121         if ( Constants.PROJECT_PACKAGING_WAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
122         {
123             writer.startElement( ELT_FIXED );
124             writer.addAttribute( ATTR_FACET, FACET_JST_WEB );
125             writer.endElement(); // fixed
126             writer.startElement( ELT_INSTALLED );
127             writer.addAttribute( ATTR_FACET, FACET_JST_WEB );
128             writer.addAttribute( ATTR_VERSION, JeeUtils.resolveServletVersion( config.getProject() ) );
129             writer.endElement(); // installed
130         }
131         else if ( Constants.PROJECT_PACKAGING_EJB.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
132         {
133             writer.startElement( ELT_FIXED );
134             writer.addAttribute( ATTR_FACET, FACET_JST_EJB );
135             writer.endElement(); // fixed
136             writer.startElement( ELT_INSTALLED );
137             writer.addAttribute( ATTR_FACET, FACET_JST_EJB );
138             writer.addAttribute( ATTR_VERSION, JeeUtils.resolveEjbVersion( config.getProject() ) );
139             writer.endElement(); // installed
140         }
141         else if ( Constants.PROJECT_PACKAGING_EAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
142         {
143             writer.startElement( ELT_FIXED );
144             writer.addAttribute( ATTR_FACET, FACET_JST_EAR );
145             writer.endElement(); // fixed
146             writer.startElement( ELT_INSTALLED );
147             writer.addAttribute( ATTR_FACET, FACET_JST_EAR );
148             writer.addAttribute( ATTR_VERSION, JeeUtils.resolveJeeVersion( config.getProject() ) );
149             writer.endElement(); // installed
150         }
151         else if ( Constants.PROJECT_PACKAGING_JAR.equalsIgnoreCase( packaging ) ) //$NON-NLS-1$
152         {
153             writer.startElement( ELT_FIXED );
154             writer.addAttribute( ATTR_FACET, FACET_JST_UTILITY );
155             writer.endElement(); // fixed
156             writer.startElement( ELT_INSTALLED );
157             writer.addAttribute( ATTR_FACET, FACET_JST_UTILITY );
158             writer.addAttribute( ATTR_VERSION, "1.0" ); //$NON-NLS-1$
159             writer.endElement(); // installed
160         }
161 
162         // common installed element
163         writer.startElement( ELT_INSTALLED );
164         writer.addAttribute( ATTR_FACET, FACET_JST_JAVA );
165         writer.addAttribute( ATTR_VERSION, IdeUtils.resolveJavaVersion( config.getProject() ) );
166         writer.endElement(); // installed
167 
168         writeAdditionalProjectFacets( writer );
169 
170         writer.endElement(); // faceted-project
171     }
172 
173     /**
174      * Writes out any additional project facets specified in the plugin configuration
175      * 
176      * @param writer
177      * @param packaging
178      */
179     private void writeAdditionalProjectFacets( XMLWriter writer )
180     {
181         if ( config.getProjectFacets() == null )
182         {
183             return;
184         }
185 
186         Iterator facetIterator = config.getProjectFacets().entrySet().iterator();
187         while ( facetIterator.hasNext() )
188         {
189             Entry facetEntry = (Entry) facetIterator.next();
190 
191             writer.startElement( ELT_INSTALLED );
192             writer.addAttribute( ATTR_FACET, (String) facetEntry.getKey() );
193             writer.addAttribute( ATTR_VERSION, (String) facetEntry.getValue() );
194             writer.endElement(); // installed
195         }
196     }
197 
198 }