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