下面列出了javax.xml.transform.Transformer#getOutputProperty ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Configure the supplied {@link HttpServletResponse}.
* <p>The default implementation of this method sets the
* {@link HttpServletResponse#setContentType content type} and
* {@link HttpServletResponse#setCharacterEncoding encoding}
* from the "media-type" and "encoding" output properties
* specified in the {@link Transformer}.
* @param model merged output Map (never {@code null})
* @param response current HTTP response
* @param transformer the target transformer
*/
protected void configureResponse(Map<String, Object> model, HttpServletResponse response, Transformer transformer) {
String contentType = getContentType();
String mediaType = transformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
String encoding = transformer.getOutputProperty(OutputKeys.ENCODING);
if (StringUtils.hasText(mediaType)) {
contentType = mediaType;
}
if (StringUtils.hasText(encoding)) {
// Only apply encoding if content type is specified but does not contain charset clause already.
if (contentType != null && !contentType.toLowerCase().contains(WebUtils.CONTENT_TYPE_CHARSET_PREFIX)) {
contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding;
}
}
response.setContentType(contentType);
}
/**
* Configure the supplied {@link HttpServletResponse}.
* <p>The default implementation of this method sets the
* {@link HttpServletResponse#setContentType content type} and
* {@link HttpServletResponse#setCharacterEncoding encoding}
* from the "media-type" and "encoding" output properties
* specified in the {@link Transformer}.
* @param model merged output Map (never {@code null})
* @param response current HTTP response
* @param transformer the target transformer
*/
protected void configureResponse(Map<String, Object> model, HttpServletResponse response, Transformer transformer) {
String contentType = getContentType();
String mediaType = transformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
String encoding = transformer.getOutputProperty(OutputKeys.ENCODING);
if (StringUtils.hasText(mediaType)) {
contentType = mediaType;
}
if (StringUtils.hasText(encoding)) {
// Only apply encoding if content type is specified but does not contain charset clause already.
if (contentType != null && !contentType.toLowerCase().contains(WebUtils.CONTENT_TYPE_CHARSET_PREFIX)) {
contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding;
}
}
response.setContentType(contentType);
}
/**
* Configure the supplied {@link HttpServletResponse}.
* <p>The default implementation of this method sets the
* {@link HttpServletResponse#setContentType content type} and
* {@link HttpServletResponse#setCharacterEncoding encoding}
* from the "media-type" and "encoding" output properties
* specified in the {@link Transformer}.
* @param model merged output Map (never {@code null})
* @param response current HTTP response
* @param transformer the target transformer
*/
protected void configureResponse(Map<String, Object> model, HttpServletResponse response, Transformer transformer) {
String contentType = getContentType();
String mediaType = transformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
String encoding = transformer.getOutputProperty(OutputKeys.ENCODING);
if (StringUtils.hasText(mediaType)) {
contentType = mediaType;
}
if (StringUtils.hasText(encoding)) {
// Only apply encoding if content type is specified but does not contain charset clause already.
if (contentType != null && !contentType.toLowerCase().contains(WebUtils.CONTENT_TYPE_CHARSET_PREFIX)) {
contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding;
}
}
response.setContentType(contentType);
}
/**
* Configure the supplied {@link HttpServletResponse}.
* <p>The default implementation of this method sets the
* {@link HttpServletResponse#setContentType content type} and
* {@link HttpServletResponse#setCharacterEncoding encoding}
* from the "media-type" and "encoding" output properties
* specified in the {@link Transformer}.
* @param model merged output Map (never {@code null})
* @param response current HTTP response
* @param transformer the target transformer
*/
protected void configureResponse(Map<String, Object> model, HttpServletResponse response, Transformer transformer) {
String contentType = getContentType();
String mediaType = transformer.getOutputProperty(OutputKeys.MEDIA_TYPE);
String encoding = transformer.getOutputProperty(OutputKeys.ENCODING);
if (StringUtils.hasText(mediaType)) {
contentType = mediaType;
}
if (StringUtils.hasText(encoding)) {
// Only apply encoding if content type is specified but does not contain charset clause already.
if (contentType != null && !contentType.toLowerCase().contains(WebUtils.CONTENT_TYPE_CHARSET_PREFIX)) {
contentType = contentType + WebUtils.CONTENT_TYPE_CHARSET_PREFIX + encoding;
}
}
response.setContentType(contentType);
}
@Test
public void enableIndentingSunnyDay() throws Exception {
Transformer transformer = new StubTransformer();
TransformerUtils.enableIndenting(transformer);
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("yes", indent);
String indentAmount = transformer.getOutputProperty("{http://xml.apache.org/xalan}indent-amount");
assertNotNull(indentAmount);
assertEquals(String.valueOf(TransformerUtils.DEFAULT_INDENT_AMOUNT), indentAmount);
}
@Test
public void enableIndentingSunnyDayWithCustomKosherIndentAmount() throws Exception {
final String indentAmountProperty = "10";
Transformer transformer = new StubTransformer();
TransformerUtils.enableIndenting(transformer, Integer.valueOf(indentAmountProperty));
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("yes", indent);
String indentAmount = transformer.getOutputProperty("{http://xml.apache.org/xalan}indent-amount");
assertNotNull(indentAmount);
assertEquals(indentAmountProperty, indentAmount);
}
@Test
public void disableIndentingSunnyDay() throws Exception {
Transformer transformer = new StubTransformer();
TransformerUtils.disableIndenting(transformer);
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("no", indent);
}
@Test
public void enableIndentingSunnyDay() throws Exception {
Transformer transformer = new StubTransformer();
TransformerUtils.enableIndenting(transformer);
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("yes", indent);
String indentAmount = transformer.getOutputProperty("{http://xml.apache.org/xslt}indent-amount");
assertNotNull(indentAmount);
assertEquals(String.valueOf(TransformerUtils.DEFAULT_INDENT_AMOUNT), indentAmount);
}
@Test
public void enableIndentingSunnyDayWithCustomKosherIndentAmount() throws Exception {
final String indentAmountProperty = "10";
Transformer transformer = new StubTransformer();
TransformerUtils.enableIndenting(transformer, Integer.valueOf(indentAmountProperty));
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("yes", indent);
String indentAmount = transformer.getOutputProperty("{http://xml.apache.org/xslt}indent-amount");
assertNotNull(indentAmount);
assertEquals(indentAmountProperty, indentAmount);
}
@Test
public void disableIndentingSunnyDay() throws Exception {
Transformer transformer = new StubTransformer();
TransformerUtils.disableIndenting(transformer);
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("no", indent);
}
@Test
public void enableIndentingSunnyDay() throws Exception {
Transformer transformer = new StubTransformer();
TransformerUtils.enableIndenting(transformer);
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("yes", indent);
String indentAmount = transformer.getOutputProperty("{http://xml.apache.org/xslt}indent-amount");
assertNotNull(indentAmount);
assertEquals(String.valueOf(TransformerUtils.DEFAULT_INDENT_AMOUNT), indentAmount);
}
@Test
public void enableIndentingSunnyDayWithCustomKosherIndentAmount() throws Exception {
final String indentAmountProperty = "10";
Transformer transformer = new StubTransformer();
TransformerUtils.enableIndenting(transformer, Integer.valueOf(indentAmountProperty));
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("yes", indent);
String indentAmount = transformer.getOutputProperty("{http://xml.apache.org/xslt}indent-amount");
assertNotNull(indentAmount);
assertEquals(indentAmountProperty, indentAmount);
}
@Test
public void disableIndentingSunnyDay() throws Exception {
Transformer transformer = new StubTransformer();
TransformerUtils.disableIndenting(transformer);
String indent = transformer.getOutputProperty(OutputKeys.INDENT);
assertNotNull(indent);
assertEquals("no", indent);
}
@Override
public String getContentType(SolrQueryRequest request, SolrQueryResponse response) {
Transformer t = null;
try {
t = getTransformer(request);
} catch(Exception e) {
// TODO should our parent interface throw (IO)Exception?
throw new RuntimeException("getTransformer fails in getContentType",e);
}
String mediaType = t.getOutputProperty("media-type");
if (mediaType == null || mediaType.length()==0) {
// This did not happen in my tests, mediaTypeFromXslt is set to "text/xml"
// if the XSLT transform does not contain an xsl:output element. Not sure
// if this is standard behavior or if it's just my JVM/libraries
mediaType = DEFAULT_CONTENT_TYPE;
}
if (!mediaType.contains("charset")) {
String encoding = t.getOutputProperty("encoding");
if (encoding == null || encoding.length()==0) {
encoding = "UTF-8";
}
mediaType = mediaType + "; charset=" + encoding;
}
return mediaType;
}