1 |
769 |
jeremybenn |
/* TransformerOutputProperties.java --
|
2 |
|
|
Copyright (C) 2004 Free Software Foundation, Inc.
|
3 |
|
|
|
4 |
|
|
This file is part of GNU Classpath.
|
5 |
|
|
|
6 |
|
|
GNU Classpath is free software; you can redistribute it and/or modify
|
7 |
|
|
it under the terms of the GNU General Public License as published by
|
8 |
|
|
the Free Software Foundation; either version 2, or (at your option)
|
9 |
|
|
any later version.
|
10 |
|
|
|
11 |
|
|
GNU Classpath is distributed in the hope that it will be useful, but
|
12 |
|
|
WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
|
|
General Public License for more details.
|
15 |
|
|
|
16 |
|
|
You should have received a copy of the GNU General Public License
|
17 |
|
|
along with GNU Classpath; see the file COPYING. If not, write to the
|
18 |
|
|
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
|
|
02110-1301 USA.
|
20 |
|
|
|
21 |
|
|
Linking this library statically or dynamically with other modules is
|
22 |
|
|
making a combined work based on this library. Thus, the terms and
|
23 |
|
|
conditions of the GNU General Public License cover the whole
|
24 |
|
|
combination.
|
25 |
|
|
|
26 |
|
|
As a special exception, the copyright holders of this library give you
|
27 |
|
|
permission to link this library with independent modules to produce an
|
28 |
|
|
executable, regardless of the license terms of these independent
|
29 |
|
|
modules, and to copy and distribute the resulting executable under
|
30 |
|
|
terms of your choice, provided that you also meet, for each linked
|
31 |
|
|
independent module, the terms and conditions of the license of that
|
32 |
|
|
module. An independent module is a module which is not derived from
|
33 |
|
|
or based on this library. If you modify this library, you may extend
|
34 |
|
|
this exception to your version of the library, but you are not
|
35 |
|
|
obligated to do so. If you do not wish to do so, delete this
|
36 |
|
|
exception statement from your version. */
|
37 |
|
|
|
38 |
|
|
package gnu.xml.transform;
|
39 |
|
|
|
40 |
|
|
import gnu.java.lang.CPStringBuilder;
|
41 |
|
|
|
42 |
|
|
import java.util.Collection;
|
43 |
|
|
import java.util.Iterator;
|
44 |
|
|
import java.util.LinkedHashSet;
|
45 |
|
|
import java.util.Properties;
|
46 |
|
|
import java.util.StringTokenizer;
|
47 |
|
|
import javax.xml.transform.OutputKeys;
|
48 |
|
|
|
49 |
|
|
/**
|
50 |
|
|
* Helper class to manage JAXP user setting of output properties.
|
51 |
|
|
*
|
52 |
|
|
* @author <a href='mailto:dog@gnu.org'>Chris Burdess</a>
|
53 |
|
|
*/
|
54 |
|
|
class TransformerOutputProperties
|
55 |
|
|
extends Properties
|
56 |
|
|
{
|
57 |
|
|
|
58 |
|
|
final Properties defaultProperties;
|
59 |
|
|
final Stylesheet stylesheet;
|
60 |
|
|
boolean dirty;
|
61 |
|
|
|
62 |
|
|
TransformerOutputProperties(Stylesheet stylesheet)
|
63 |
|
|
{
|
64 |
|
|
this.stylesheet = stylesheet;
|
65 |
|
|
defaultProperties = new Properties();
|
66 |
|
|
switch (stylesheet.outputMethod)
|
67 |
|
|
{
|
68 |
|
|
case Stylesheet.OUTPUT_XML:
|
69 |
|
|
defaultProperties.put(OutputKeys.METHOD, "xml");
|
70 |
|
|
break;
|
71 |
|
|
case Stylesheet.OUTPUT_HTML:
|
72 |
|
|
defaultProperties.put(OutputKeys.METHOD, "html");
|
73 |
|
|
break;
|
74 |
|
|
case Stylesheet.OUTPUT_TEXT:
|
75 |
|
|
defaultProperties.put(OutputKeys.METHOD, "text");
|
76 |
|
|
break;
|
77 |
|
|
}
|
78 |
|
|
if (stylesheet.outputVersion != null)
|
79 |
|
|
{
|
80 |
|
|
defaultProperties.put(OutputKeys.VERSION, stylesheet.outputVersion);
|
81 |
|
|
}
|
82 |
|
|
if (stylesheet.outputEncoding != null)
|
83 |
|
|
{
|
84 |
|
|
defaultProperties.put(OutputKeys.ENCODING, stylesheet.outputEncoding);
|
85 |
|
|
}
|
86 |
|
|
defaultProperties.put(OutputKeys.OMIT_XML_DECLARATION,
|
87 |
|
|
stylesheet.outputOmitXmlDeclaration ? "yes" : "no");
|
88 |
|
|
defaultProperties.put(OutputKeys.STANDALONE,
|
89 |
|
|
stylesheet.outputStandalone ? "yes" : "no");
|
90 |
|
|
if (stylesheet.outputPublicId != null)
|
91 |
|
|
{
|
92 |
|
|
defaultProperties.put(OutputKeys.DOCTYPE_PUBLIC,
|
93 |
|
|
stylesheet.outputPublicId);
|
94 |
|
|
}
|
95 |
|
|
if (stylesheet.outputSystemId != null)
|
96 |
|
|
{
|
97 |
|
|
defaultProperties.put(OutputKeys.DOCTYPE_SYSTEM,
|
98 |
|
|
stylesheet.outputSystemId);
|
99 |
|
|
}
|
100 |
|
|
CPStringBuilder buf = new CPStringBuilder();
|
101 |
|
|
for (Iterator i = stylesheet.outputCdataSectionElements.iterator();
|
102 |
|
|
i.hasNext(); )
|
103 |
|
|
{
|
104 |
|
|
if (buf.length() > 0)
|
105 |
|
|
{
|
106 |
|
|
buf.append(' ');
|
107 |
|
|
}
|
108 |
|
|
buf.append((String) i.next());
|
109 |
|
|
}
|
110 |
|
|
defaultProperties.put(OutputKeys.CDATA_SECTION_ELEMENTS, buf.toString());
|
111 |
|
|
defaultProperties.put(OutputKeys.INDENT,
|
112 |
|
|
stylesheet.outputIndent ? "yes" : "no");
|
113 |
|
|
if (stylesheet.outputMediaType != null)
|
114 |
|
|
{
|
115 |
|
|
defaultProperties.put(OutputKeys.MEDIA_TYPE,
|
116 |
|
|
stylesheet.outputMediaType);
|
117 |
|
|
}
|
118 |
|
|
}
|
119 |
|
|
|
120 |
|
|
public String getProperty(String key)
|
121 |
|
|
{
|
122 |
|
|
String val = super.getProperty(key);
|
123 |
|
|
if (val == null)
|
124 |
|
|
{
|
125 |
|
|
val = defaultProperties.getProperty(key);
|
126 |
|
|
}
|
127 |
|
|
return val;
|
128 |
|
|
}
|
129 |
|
|
|
130 |
|
|
public Object put(Object key, Object value)
|
131 |
|
|
{
|
132 |
|
|
Object ret = super.put(key, value);
|
133 |
|
|
dirty = true;
|
134 |
|
|
return ret;
|
135 |
|
|
}
|
136 |
|
|
|
137 |
|
|
public void clear()
|
138 |
|
|
{
|
139 |
|
|
super.clear();
|
140 |
|
|
dirty = true;
|
141 |
|
|
}
|
142 |
|
|
|
143 |
|
|
/**
|
144 |
|
|
* Applies the current set of properties to the underlying stylesheet.
|
145 |
|
|
*/
|
146 |
|
|
void apply()
|
147 |
|
|
{
|
148 |
|
|
if (!dirty)
|
149 |
|
|
{
|
150 |
|
|
return;
|
151 |
|
|
}
|
152 |
|
|
String method = getProperty(OutputKeys.METHOD);
|
153 |
|
|
if ("xml".equals(method))
|
154 |
|
|
{
|
155 |
|
|
stylesheet.outputMethod = Stylesheet.OUTPUT_XML;
|
156 |
|
|
}
|
157 |
|
|
else if ("html".equals(method))
|
158 |
|
|
{
|
159 |
|
|
stylesheet.outputMethod = Stylesheet.OUTPUT_HTML;
|
160 |
|
|
}
|
161 |
|
|
else if ("text".equals(method))
|
162 |
|
|
{
|
163 |
|
|
stylesheet.outputMethod = Stylesheet.OUTPUT_TEXT;
|
164 |
|
|
}
|
165 |
|
|
stylesheet.outputVersion = getProperty(OutputKeys.VERSION);
|
166 |
|
|
stylesheet.outputEncoding = getProperty(OutputKeys.ENCODING);
|
167 |
|
|
stylesheet.outputOmitXmlDeclaration =
|
168 |
|
|
"yes".equals(getProperty(OutputKeys.OMIT_XML_DECLARATION));
|
169 |
|
|
stylesheet.outputStandalone =
|
170 |
|
|
"yes".equals(getProperty(OutputKeys.STANDALONE));
|
171 |
|
|
stylesheet.outputPublicId = getProperty(OutputKeys.DOCTYPE_PUBLIC);
|
172 |
|
|
stylesheet.outputSystemId = getProperty(OutputKeys.DOCTYPE_SYSTEM);
|
173 |
|
|
StringTokenizer st =
|
174 |
|
|
new StringTokenizer(getProperty(OutputKeys.CDATA_SECTION_ELEMENTS));
|
175 |
|
|
Collection acc = new LinkedHashSet();
|
176 |
|
|
while (st.hasMoreTokens())
|
177 |
|
|
{
|
178 |
|
|
acc.add(st.nextToken());
|
179 |
|
|
}
|
180 |
|
|
stylesheet.outputCdataSectionElements = acc;
|
181 |
|
|
stylesheet.outputIndent = "yes".equals(getProperty(OutputKeys.INDENT));
|
182 |
|
|
stylesheet.outputMediaType = getProperty(OutputKeys.MEDIA_TYPE);
|
183 |
|
|
dirty = false;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
|
|
}
|