View Javadoc
1   package org.codehaus.mojo.webstart.generator;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.apache.maven.plugin.MojoExecutionException;
23  import org.codehaus.mojo.webstart.ResolvedJarResource;
24  import org.codehaus.plexus.util.WriterFactory;
25  
26  import java.io.BufferedWriter;
27  import java.io.File;
28  import java.io.IOException;
29  import java.util.Collection;
30  
31  /**
32   * This class generates a <code>version.xml</code> file for a given collection of
33   * <code>JarResource</code> objects in the format expected by the <code>JnlpDownloadServlet</code>.
34   * <p/>
35   * <p>
36   * For a full description of the version.xml syntax, refer to the
37   * <a href="http://java.sun.com/javase/6/docs/technotes/guides/javaws/developersguide/downloadservletguide.html">
38   * JnlpDownloadServlet Guide</a>
39   * </p>
40   *
41   * @author Kevin Stembridge
42   * @version $Revision$
43   * @since 1.0-alpha-2
44   */
45  public class VersionXmlGenerator
46  {
47  
48      private final String encoding;
49  
50      /**
51       * Creates a new {@code VersionXmlGenerator}.
52       *
53       * @param encoding encoding used to write version.xml file
54       */
55      public VersionXmlGenerator( String encoding )
56      {
57          this.encoding = encoding;
58      }
59  
60      /**
61       * Generates a file named <code>version.xml</code> in the given <code>outputDir</code>.
62       * The generated file will contain resource elements for each of the JarResource
63       * objects in the given collection.
64       *
65       * @param outputDir    The directory in which the file will be generated. Must not be null.
66       * @param jarResources The collection of JarResources for which a resource
67       *                     element will be created in the generated file.
68       * @throws MojoExecutionException if an error occurs generating the file.
69       */
70      public void generate( File outputDir, Collection<ResolvedJarResource> jarResources )
71          throws MojoExecutionException
72      {
73  
74          if ( outputDir == null )
75          {
76              throw new IllegalArgumentException( "outputDir must not be null" );
77          }
78  
79          BufferedWriter writer = null;
80  
81          try
82          {
83              File versionXmlFile = new File( outputDir, "version.xml" );
84              writer = new BufferedWriter( WriterFactory.newWriter( versionXmlFile, encoding ) );
85  
86              generateXml( writer, jarResources );
87  
88          }
89          catch ( IOException e )
90          {
91              throw new MojoExecutionException( "Unable to create the version.xml file", e );
92          }
93          finally
94          {
95              if ( writer != null )
96              {
97                  try
98                  {
99                      writer.close();
100                 }
101                 catch ( IOException e )
102                 {
103                     // do nothing
104                 }
105             }
106         }
107 
108     }
109 
110     private void generateXml( BufferedWriter writer, Collection<ResolvedJarResource> jarResources )
111         throws IOException
112     {
113 
114         writer.write( "<?xml version=\"1.0\"?>" );
115         writer.newLine();
116         writer.write( "<jnlp-versions>" );
117         writer.newLine();
118 
119         for ( ResolvedJarResource jarResource : jarResources )
120         {
121             writer.write( "  <resource>" );
122             writer.newLine();
123             writer.write( "    <pattern>" );
124             writer.newLine();
125             writer.write( "      <name>" );
126             writer.write( jarResource.getHrefValue() );
127             writer.write( "</name>" );
128             writer.newLine();
129             writer.write( "      <version-id>" );
130             writer.write( jarResource.getVersion() );
131             writer.write( "</version-id>" );
132             writer.newLine();
133             writer.write( "    </pattern>" );
134             writer.newLine();
135             writer.write( "    <file>" );
136             writer.write( jarResource.getArtifact().getFile().getName() );
137             writer.write( "</file>" );
138             writer.newLine();
139             writer.write( "  </resource>" );
140             writer.newLine();
141         }
142 
143         writer.write( "</jnlp-versions>" );
144         writer.newLine();
145     }
146 }