View Javadoc
1   package org.apache.maven.archetype.old.descriptor;
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 java.io.OutputStreamWriter;
23  import java.nio.charset.Charset;
24  import java.nio.charset.IllegalCharsetNameException;
25  import java.nio.charset.UnsupportedCharsetException;
26  
27  /**
28   * Contains the attributes of an archetype's template (either a source or resource file).
29   * The attributes indicate if the template should be filtered and it's encoding.
30   */
31  public class TemplateDescriptor
32  {
33  
34      /**
35       * Determines if the template should be filtered or not.
36       */
37      private boolean filtered = true;
38  
39      /**
40       * Determines the template's encoding.
41       */
42      private String encoding;
43  
44      /**
45       * Creates a new instance of <code>TemplateDescriptor</code> that should be filtered
46       * and has the default encoding.
47       */
48      public TemplateDescriptor()
49      {
50          setFiltered( true );
51  
52          setEncoding( getDefaultEncoding() );
53      }
54  
55      /**
56       * Returns the canonical name of the default character encoding of this Java
57       * virtual machine.
58       *
59       * @return the name of the default character encoding.
60       */
61      private static String getDefaultEncoding()
62      {
63          String name = System.getProperty( "file.encoding" );
64  
65          if ( name == null )
66          {
67              OutputStreamWriter out = new OutputStreamWriter( System.out );
68  
69              name = out.getEncoding();
70          }
71  
72          name = Charset.forName( name ).name();
73  
74          return name;
75      }
76  
77      /**
78       * Returns <code>true</code> if the template should be filtered and
79       * <code>false</code> otherwise.
80       *
81       * @return <code>true</code> if the template should be filtered and
82       *         <code>false</code> otherwise.
83       */
84      public boolean isFiltered()
85      {
86          return this.filtered;
87      }
88  
89      /**
90       * Defines whether the template should be filtered (processed by Velocity)
91       * or not.
92       *
93       * @param filtered <code>true</code> if it should be processed by Velocity and
94       *                 <code>fales</code> otherwise.
95       */
96      public void setFiltered( boolean filtered )
97      {
98          this.filtered = filtered;
99      }
100 
101     /**
102      * Returns the name of the  encoding of the template file (e.g.
103      * <code>us-ascci</code>, <code>utf-8</code>, <code>iso-8859-1</code>).
104      *
105      * @return the name of the  encoding of the template file.
106      */
107     public String getEncoding()
108     {
109         return this.encoding;
110     }
111 
112     /**
113      * Sets the name of the encoding of the template file.
114      *
115      * @param encoding New value of property encoding.
116      * @throws IllegalCharsetNameException if the given charset name is illegal
117      * @throws UnsupportedCharsetException if no support for the named encoding
118      *                                     is available in this instance of the Java virtual machine
119      */
120     public void setEncoding( String encoding )
121         throws IllegalCharsetNameException, UnsupportedCharsetException
122     {
123         Charset.forName( encoding );
124 
125         this.encoding = encoding;
126     }
127 
128 }