Java Code Examples for javax.xml.transform.Transformer#getOutputProperty()

The following examples show how to use javax.xml.transform.Transformer#getOutputProperty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: XsltView.java    From spring-analysis-note with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example 2
Source File: XsltView.java    From java-technology-stack with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example 3
Source File: XsltView.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: XsltView.java    From spring4-understanding with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: TransformerUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 6
Source File: TransformerUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@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);
}
 
Example 7
Source File: TransformerUtilsTests.java    From spring-analysis-note with MIT License 5 votes vote down vote up
@Test
public void disableIndentingSunnyDay() throws Exception {
	Transformer transformer = new StubTransformer();
	TransformerUtils.disableIndenting(transformer);
	String indent = transformer.getOutputProperty(OutputKeys.INDENT);
	assertNotNull(indent);
	assertEquals("no", indent);
}
 
Example 8
Source File: TransformerUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 9
Source File: TransformerUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@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);
}
 
Example 10
Source File: TransformerUtilsTests.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Test
public void disableIndentingSunnyDay() throws Exception {
	Transformer transformer = new StubTransformer();
	TransformerUtils.disableIndenting(transformer);
	String indent = transformer.getOutputProperty(OutputKeys.INDENT);
	assertNotNull(indent);
	assertEquals("no", indent);
}
 
Example 11
Source File: TransformerUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 12
Source File: TransformerUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 13
Source File: TransformerUtilsTests.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
@Test
public void disableIndentingSunnyDay() throws Exception {
	Transformer transformer = new StubTransformer();
	TransformerUtils.disableIndenting(transformer);
	String indent = transformer.getOutputProperty(OutputKeys.INDENT);
	assertNotNull(indent);
	assertEquals("no", indent);
}
 
Example 14
Source File: XSLTResponseWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@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;
}