TransformerOutputProperties.java [plain text]
package gnu.xml.transform;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.xml.transform.OutputKeys;
class TransformerOutputProperties
extends Properties
{
final Properties defaultProperties;
final Stylesheet stylesheet;
boolean dirty;
TransformerOutputProperties(Stylesheet stylesheet)
{
this.stylesheet = stylesheet;
defaultProperties = new Properties();
switch (stylesheet.outputMethod)
{
case Stylesheet.OUTPUT_XML:
defaultProperties.put(OutputKeys.METHOD, "xml");
break;
case Stylesheet.OUTPUT_HTML:
defaultProperties.put(OutputKeys.METHOD, "html");
break;
case Stylesheet.OUTPUT_TEXT:
defaultProperties.put(OutputKeys.METHOD, "text");
break;
}
if (stylesheet.outputVersion != null)
{
defaultProperties.put(OutputKeys.VERSION, stylesheet.outputVersion);
}
if (stylesheet.outputEncoding != null)
{
defaultProperties.put(OutputKeys.ENCODING, stylesheet.outputEncoding);
}
defaultProperties.put(OutputKeys.OMIT_XML_DECLARATION,
stylesheet.outputOmitXmlDeclaration ? "yes" : "no");
defaultProperties.put(OutputKeys.STANDALONE,
stylesheet.outputStandalone ? "yes" : "no");
if (stylesheet.outputPublicId != null)
{
defaultProperties.put(OutputKeys.DOCTYPE_PUBLIC,
stylesheet.outputPublicId);
}
if (stylesheet.outputSystemId != null)
{
defaultProperties.put(OutputKeys.DOCTYPE_SYSTEM,
stylesheet.outputSystemId);
}
StringBuffer buf = new StringBuffer();
for (Iterator i = stylesheet.outputCdataSectionElements.iterator();
i.hasNext(); )
{
if (buf.length() > 0)
{
buf.append(' ');
}
buf.append((String) i.next());
}
defaultProperties.put(OutputKeys.CDATA_SECTION_ELEMENTS, buf.toString());
defaultProperties.put(OutputKeys.INDENT,
stylesheet.outputIndent ? "yes" : "no");
if (stylesheet.outputMediaType != null)
{
defaultProperties.put(OutputKeys.MEDIA_TYPE,
stylesheet.outputMediaType);
}
}
public String getProperty(String key)
{
String val = super.getProperty(key);
if (val == null)
{
val = defaultProperties.getProperty(key);
}
return val;
}
public Object put(Object key, Object value)
{
Object ret = super.put(key, value);
dirty = true;
return ret;
}
public void clear()
{
super.clear();
dirty = true;
}
void apply()
{
if (!dirty)
{
return;
}
String method = getProperty(OutputKeys.METHOD);
if ("xml".equals(method))
{
stylesheet.outputMethod = Stylesheet.OUTPUT_XML;
}
else if ("html".equals(method))
{
stylesheet.outputMethod = Stylesheet.OUTPUT_HTML;
}
else if ("text".equals(method))
{
stylesheet.outputMethod = Stylesheet.OUTPUT_TEXT;
}
stylesheet.outputVersion = getProperty(OutputKeys.VERSION);
stylesheet.outputEncoding = getProperty(OutputKeys.ENCODING);
stylesheet.outputOmitXmlDeclaration =
"yes".equals(getProperty(OutputKeys.OMIT_XML_DECLARATION));
stylesheet.outputStandalone =
"yes".equals(getProperty(OutputKeys.STANDALONE));
stylesheet.outputPublicId = getProperty(OutputKeys.DOCTYPE_PUBLIC);
stylesheet.outputSystemId = getProperty(OutputKeys.DOCTYPE_SYSTEM);
StringTokenizer st =
new StringTokenizer(getProperty(OutputKeys.CDATA_SECTION_ELEMENTS));
Collection acc = new LinkedHashSet();
while (st.hasMoreTokens())
{
acc.add(st.nextToken());
}
stylesheet.outputCdataSectionElements = acc;
stylesheet.outputIndent = "yes".equals(getProperty(OutputKeys.INDENT));
stylesheet.outputMediaType = getProperty(OutputKeys.MEDIA_TYPE);
dirty = false;
}
}