blob: 67da2d8fead22617c6484b5b0a21046e1c0820e0 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package freemarker.core;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import freemarker.template.TemplateException;
/**
* {@value #NAME} {@link CFormat}, for outputting XML that follows the conventions of XML Schema.
*
* <p><b>Experimental class!</b> This class is too new, and might will change over time. Therefore, for now the
* most methods are not exposed outside FreeMarker. The class itself and some members are exposed as they are needed for
* configuring FreeMarker.
*/
public final class XSCFormat extends CFormat {
public static final String NAME = "XS";
public static final XSCFormat INSTANCE = new XSCFormat();
private static final TemplateNumberFormat TEMPLATE_NUMBER_FORMAT = new CTemplateNumberFormat(
"INF", "-INF", "NaN",
"INF", "-INF", "NaN");
private static final DecimalFormat LEGACY_NUMBER_FORMAT_PROTOTYPE = (DecimalFormat) Default230CFormat.INSTANCE.getLegacyNumberFormat().clone();
static {
DecimalFormatSymbols symbols = LEGACY_NUMBER_FORMAT_PROTOTYPE.getDecimalFormatSymbols();
symbols.setInfinity("INF");
symbols.setNaN("NaN");
LEGACY_NUMBER_FORMAT_PROTOTYPE.setDecimalFormatSymbols(symbols);
}
@Override
NumberFormat getLegacyNumberFormat() {
return (NumberFormat) LEGACY_NUMBER_FORMAT_PROTOTYPE.clone();
}
private XSCFormat() {
}
@Override
TemplateNumberFormat getTemplateNumberFormat() {
return TEMPLATE_NUMBER_FORMAT;
}
@Override
String formatString(String s, Environment env) throws TemplateException {
return s; // So we don't escape here, as we assume that there's XML auto-escaping
}
@Override
String getTrueString() {
return "true";
}
@Override
String getFalseString() {
return "false";
}
@Override
String getNullString() {
// XSD has no null literal, and you have to leave out the whole element to represent null. We can achieve that
// here. The closet we can do is causing an empty element or attribute to be outputted. Some frameworks will
// interpret that as null if the XSD type is not string.
return "";
}
@Override
public String getName() {
return NAME;
}
}