Micha Kops avatar Micha Kops committed 2888a56

Test for xslt processing added, atlassian namespaces added to the parsed xml.

Comments (0)

Files changed (6)

 
 	<dependencies>
 		<dependency>
+			<groupId>org.hamcrest</groupId>
+			<artifactId>hamcrest-all</artifactId>
+			<version>1.3</version>
+			<scope>test</scope>
+		</dependency>
+		<dependency>
 			<groupId>junit</groupId>
 			<artifactId>junit</artifactId>
 			<version>4.11</version>

src/main/java/com/hascode/plugin/confluence/xslt_processor/transformer/XsltPageTransformer.java

 	private static final TransformerFactory factory = TransformerFactory
 			.newInstance();
 	private static final String DTD = "<!DOCTYPE doctypeName [<!ENTITY nbsp \"&#160;\">]>";
+	private static final String WRAPPER = "<wrapper xmlns:ac=\"http://atlassian.com\" xmlns:ri=\"http://atlassian2.com\">%s</wrapper>";
 
 	public String transform(final String pageContent,
 			final XsltTemplate template) {
-		final String content = DTD + pageContent;
+		final String content = String.format(WRAPPER, pageContent);
 		Source xslt = new StreamSource(new StringReader(template.getTemplate()));
 		Transformer transformer;
 		try {

src/test/java/com/hascode/plugin/confluence/xslt_processor/transformer/XsltPageTransformerTest.java

+package com.hascode.plugin.confluence.xslt_processor.transformer;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.junit.Assert.assertFalse;
+
+import org.apache.commons.lang.StringUtils;
+import org.junit.Test;
+
+import com.atlassian.modzdetector.IOUtils;
+import com.hascode.plugin.confluence.xslt_processor.entity.BasicXsltTemplate;
+import com.hascode.plugin.confluence.xslt_processor.entity.XsltTemplate;
+
+public class XsltPageTransformerTest {
+	static String SIMPLE_XSLT_TEMPLATE = "/sample-template.xslt";
+	static String COMPLEX_XSLT_TEMPLATE = "/complex-template.xslt";
+	static String CONFLUENCE_PAGE_XML = "/sample-confluence-page-content.xml";
+
+	XsltPageTransformer transformer = new XsltPageTransformer();
+
+	@Test
+	public void shouldTransformConfluencePage() throws Exception {
+		String xsltTemplate = IOUtils.toString(getClass().getResourceAsStream(
+				SIMPLE_XSLT_TEMPLATE));
+		String xmlContent = IOUtils.toString(getClass().getResourceAsStream(
+				CONFLUENCE_PAGE_XML));
+
+		XsltTemplate template = new BasicXsltTemplate();
+		template.setTemplate(xsltTemplate);
+		String result = transformer.transform(xmlContent, template);
+		assertFalse("result should not be empty", StringUtils.isEmpty(result));
+		assertThat(result, containsString("<h1>TEST</h1>"));
+	}
+
+	@Test
+	public void shouldTransformComplexTemplate() throws Exception {
+		String xsltTemplate = IOUtils.toString(getClass().getResourceAsStream(
+				COMPLEX_XSLT_TEMPLATE));
+		String xmlContent = IOUtils.toString(getClass().getResourceAsStream(
+				CONFLUENCE_PAGE_XML));
+
+		XsltTemplate template = new BasicXsltTemplate();
+		template.setTemplate(xsltTemplate);
+		String result = transformer.transform(xmlContent, template);
+		assertFalse("result should not be empty", StringUtils.isEmpty(result));
+		assertThat(result, containsString("What is a space?"));
+		assertThat(result, containsString("What is the Dashboard?"));
+		assertThat(result, containsString("What is a wiki?"));
+	}
+}

src/test/resources/complex-template.xslt

+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="1.0"
+	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ac="http://atlassian.com" xmlns:ri="http://atlassian2.com" exclude-result-prefixes="ac ri">
+	<xsl:output method="html" encoding="UTF-8" />
+	<xsl:template match="/">
+		<xsl:for-each select="//ac:rich-text-body//strong">
+			<xsl:value-of select="."></xsl:value-of>
+			<xsl:text>&#xa;</xsl:text>
+		</xsl:for-each>
+	</xsl:template>
+</xsl:stylesheet>

src/test/resources/sample-confluence-page-content.xml

+<ac:macro ac:name="section">
+	<ac:rich-text-body>
+		<ac:macro ac:name="column">
+			<ac:parameter ac:name="width">60%</ac:parameter>
+			<ac:rich-text-body>
+				<p>This tutorial will help you get started with Confluence:</p>
+				<ac:macro ac:name="children">
+					<ac:parameter ac:name="style">h5</ac:parameter>
+				</ac:macro>
+				<p>
+					<br class="atl-forced-newline" />
+					<br class="atl-forced-newline" />
+					<ac:link>
+						<ri:page ri:content-title="Using the Dashboard" />
+						<ac:link-body>
+							<ac:image>
+								<ri:attachment ri:filename="btn_next.png">
+									<ri:page ri:content-title="_Images" />
+								</ri:attachment>
+							</ac:image>
+						</ac:link-body>
+					</ac:link>
+				</p>
+			</ac:rich-text-body>
+		</ac:macro>
+		<ac:macro ac:name="column">
+			<ac:rich-text-body>
+				<ac:macro ac:name="info">
+					<ac:parameter ac:name="icon">false</ac:parameter>
+					<ac:rich-text-body>
+						<p>
+							<strong>What is a space?</strong>
+							<br />
+							A space is an area within Confluence, containing your wiki pages.
+							You can think of each space as a sub-site, or mini-site, each
+							with its own home page.
+						</p>
+						<p>
+							<strong>What is the Dashboard?</strong>
+							<br />
+							The Dashboard is the home page of your Confluence wiki. The
+							Dashboard contains a list of all the spaces within your wiki, as
+							well as a list of recently updated content.
+						</p>
+						<p>
+							<strong>What is a wiki?</strong>
+							<br />
+							A wiki is a piece of software that allows you and your colleagues
+							to create and edit web pages quickly and simply. Just click, edit
+							and save. Content is published immediately.
+						</p>
+						<p>
+							<strong>What is Confluence?</strong>
+							<br />
+							Confluence is an enterprise wiki with some extra communication
+							tools, including blog posts and RSS feeds.
+						</p>
+						<p>
+							<strong>What makes Confluence an enterprise wiki?</strong>
+							<br />
+							Different organisations use Confluence for different purposes: as
+							a company intranet, corporate website, project workspace,
+							knowledge base, documentation site and a host of other
+							implementations. All these are made possible by Confluence's
+							enterprise-appropriate features, including: security permissions
+							at the page, space and site level; content structuring and
+							restructuring options; LDAP compatibility and more.
+						</p>
+						<p>
+							<ac:emoticon ac:name="tick" />
+							If you prefer, read the tutorial
+							<ac:link>
+								<ri:page ri:content-title="Tutorial (all on one page)" />
+								<ac:plain-text-link-body><![CDATA[on one page]]></ac:plain-text-link-body>
+							</ac:link>
+							.
+						</p>
+					</ac:rich-text-body>
+				</ac:macro>
+			</ac:rich-text-body>
+		</ac:macro>
+	</ac:rich-text-body>
+</ac:macro>

src/test/resources/sample-template.xslt

+<?xml version="1.0" encoding="UTF-8"?>
+<xsl:stylesheet version="1.0"
+	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ac="http://atlassian.com" xmlns:ri="http://atlassian2.com" exclude-result-prefixes="ac ri">
+	<xsl:output method="html" encoding="UTF-8" />
+	<xsl:template match="/">
+		<h1>TEST</h1>
+	</xsl:template>
+</xsl:stylesheet>
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.