Skip to content

Commit

Permalink
#55 XSL.with(String,String)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yegor Bugayenko committed Feb 20, 2015
1 parent ea0fc79 commit dc60529
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 10 deletions.
11 changes: 11 additions & 0 deletions src/main/java/com/jcabi/xml/XSL.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,15 @@ public interface XSL {
@NotNull(message = "XSL is never NULL")
XSL with(@NotNull(message = "sources can't be NULL") Sources src);

/**
* With this parameter.
* @param name Name of XSL parameter
* @param value Value of XSL parameter
* @return New XSL document
* @since 0.16
*/
@NotNull(message = "XSL is never NULL")
XSL with(@NotNull(message = "parameter name can't be NULL") String name,
@NotNull(message = "parameter value can't be NULL") String value);

}
5 changes: 5 additions & 0 deletions src/main/java/com/jcabi/xml/XSLChain.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ public String applyTo(final XML xml) {
public XSL with(final Sources src) {
throw new UnsupportedOperationException("#with()");
}

@Override
public XSL with(final String name, final String value) {
throw new UnsupportedOperationException("#with(name, value)");
}
}
59 changes: 49 additions & 10 deletions src/main/java/com/jcabi/xml/XSLDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
package com.jcabi.xml;

import com.jcabi.aspects.Immutable;
import com.jcabi.immutable.ArrayMap;
import com.jcabi.log.Logger;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
Expand All @@ -38,6 +39,7 @@
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URL;
import java.util.Map;
import javax.validation.constraints.NotNull;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
Expand Down Expand Up @@ -70,22 +72,28 @@
public final class XSLDocument implements XSL {

/**
* Strips spaces of whitespace-only text nodes. This will NOT remove
* Strips spaces of whitespace-only text nodes.
*
* <p>This will NOT remove
* existing indentation between Element nodes currently introduced by the
* constructor of {@link com.jcabi.xml.XMLDocument}. For example:
*
* <pre>
* {@code
* <a>
* <b> TXT </b>
* </a>}
* &lt;a&gt;
* &lt;b> TXT &lt;/b>
* &lt;/a>}
* </pre>
*
* becomes
*
* <pre>
* {@code
* <a>
* <b> TXT </b>
* </a>}
* &lt;a>
* &lt;b> TXT &lt;/b>
* &lt;/a>}
* </pre>
*
* @since 0.14
*/
public static final XSL STRIP = XSLDocument.make(
Expand Down Expand Up @@ -126,6 +134,11 @@ public void fatalError(final TransformerException exception) {
*/
private final transient Sources sources;

/**
* Parameters.
*/
private final transient ArrayMap<String, String> params;

/**
* Public ctor, from XML as a source.
* @param src XSL document body
Expand Down Expand Up @@ -179,18 +192,40 @@ public XSLDocument(final String src) {
* @param srcs Sources
* @since 0.9
*/
public XSLDocument(final String src, final Sources srcs) {
this(src, srcs, new ArrayMap<String, String>());
}

/**
* Public ctor, from XSL as a string.
* @param src XML document body
* @param srcs Sources
* @param map Map of XSL params
* @since 0.16
*/
public XSLDocument(
@NotNull(message = "XSL can't be NULL") final String src,
@NotNull(message = "sources can't be NULL")
final Sources srcs) {
@NotNull(message = "sources can't be NULL") final Sources srcs,
@NotNull(message = "map of params can't be NULL")
final Map<String, String> map) {
this.xsl = src;
this.sources = srcs;
this.params = new ArrayMap<String, String>(map);
}

@Override
public XSL with(@NotNull(message = "sources can't be NULL")
final Sources src) {
return new XSLDocument(this.xsl, src);
return new XSLDocument(this.xsl, src, this.params);
}

@Override
public XSL with(
@NotNull(message = "name can't be NULL") final String name,
@NotNull(message = "value can't be NULL") final String value) {
return new XSLDocument(
this.xsl, this.sources, this.params.with(name, value)
);
}

/**
Expand Down Expand Up @@ -282,6 +317,10 @@ private void transformInto(final XML xml, final Result result) {
new StreamSource(new StringReader(this.xsl))
);
trans.setURIResolver(this.sources);
for (final Map.Entry<String, String> ent
: this.params.entrySet()) {
trans.setParameter(ent.getKey(), ent.getValue());
}
trans.transform(new DOMSource(xml.node()), result);
} catch (final TransformerConfigurationException ex) {
throw new IllegalStateException(
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/com/jcabi/xml/XSLDocumentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -155,4 +155,27 @@ public void stripsXml() throws Exception {
);
}

/**
* XSLDocument can transform into text, with params.
* @throws Exception If something goes wrong inside
* @since 0.16
*/
@Test
public void transformsIntoTextWithParams() throws Exception {
final XSL xsl = new XSLDocument(
StringUtils.join(
"<xsl:stylesheet ",
" xmlns:xsl='http://www.w3.org/1999/XSL/Transform' ",
" version='2.0'><xsl:output method='text' />",
"<xsl:param name='boom' />",
"<xsl:template match='/'>[<xsl:value-of select='$boom'/>]",
"</xsl:template> </xsl:stylesheet>"
)
);
MatcherAssert.assertThat(
xsl.with("boom", "Donny").applyTo(new XMLDocument("<ehe/>")),
Matchers.equalTo("[Donny]")
);
}

}

0 comments on commit dc60529

Please sign in to comment.