Skip to content

Commit 18099a1

Browse files
jjYBdx4ILjjYBdx4IL
jjYBdx4IL
authored and
jjYBdx4IL
committed
add examples: commons-logging, ttf2svg batik, wildfly-maven-plugin
1 parent 2706cea commit 18099a1

File tree

21 files changed

+911
-3
lines changed

21 files changed

+911
-3
lines changed

.gitignore

100755100644
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,5 @@
33
.settings
44
.classpath
55
/gwt-example*/*/target/
6+
/*/db.mv.db
7+
/*/db.trace.db

README.md

100755100644
Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
# Example maven project setups
22

3+
## Selenium+Chrome Test Unit Example
4+
5+
See gwt-exmple-2.
6+
7+
## Setting a new (!) maven build (pom.xml) property programmatically via groovy maven plugin
8+
9+
See wildfly-maven-plugin-example.
10+
311
[![Build Status](https://travis-ci.org/jjYBdx4IL/example-maven-project-setups.png?branch=master)](https://travis-ci.org/jjYBdx4IL/example-maven-project-setups)
412

513

614
--
715
[![Build Status](https://travis-ci.org/jjYBdx4IL/example-maven-project-setups.png?branch=master)](https://travis-ci.org/jjYBdx4IL/example-maven-project-setups)
8-
devel/java/github/example-maven-project-setups@7840
16+
devel/java/github/example-maven-project-setups@7850

commons-logging-example/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# commons-logging Example
2+
3+
## Conclusion
4+
5+
* No placeholder support (slf4j has it)
6+
* No runtime configuration officially supported/intended, though mileage may very.
7+
* Don't see any reason to use it over slf4j-api if one is looking for a backend-agnostic logging frontend.
8+
* slf4j's isDebugEnabled reflects dynamic logging configuration changes in the WildFly server. Haven't tested this with commons-logging.

commons-logging-example/pom.xml

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="
4+
http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/maven-v4_0_0.xsd
6+
">
7+
8+
<modelVersion>4.0.0</modelVersion>
9+
10+
<parent>
11+
<groupId>com.github.jjYBdx4IL.maven.examples</groupId>
12+
<artifactId>github-maven-examples</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<artifactId>commons-logging-example</artifactId>
17+
18+
<name>Maven Examples :: commons-logging Example</name>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>commons-logging</groupId>
23+
<artifactId>commons-logging</artifactId>
24+
<version>1.2</version>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>junit</groupId>
29+
<artifactId>junit</artifactId>
30+
<scope>test</scope>
31+
</dependency>
32+
</dependencies>
33+
</project>
34+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package testroot;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.junit.Assert.assertNull;
5+
import static org.junit.Assert.assertTrue;
6+
7+
import org.apache.commons.logging.Log;
8+
import org.apache.commons.logging.LogFactory;
9+
import org.junit.Test;
10+
11+
import java.io.IOException;
12+
13+
public class LoggingTest {
14+
15+
private static final Log LOG = LogFactory.getLog(LoggingTest.class);
16+
17+
// it's possible to configure commons-logging programmatically. However, you have to make sure that
18+
// the factory hasn't been used yet. You can also call LogFactory.releaseAll(). That doesn't seem to affect
19+
// previously acquired logger instances, though it's likely not its intended purpose.
20+
static {
21+
assertNull(System.getProperty("org.apache.commons.logging.Log"));
22+
assertNull(System.getProperty("org.apache.commons.logging.simplelog.showdatetime"));
23+
LogFactory.releaseAll();
24+
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
25+
System.setProperty("org.apache.commons.logging.simplelog.log.testroot", "DEBUG");
26+
}
27+
private static final Log LOG2 = LogFactory.getLog(LoggingTest.class);
28+
29+
// once SimpleLog is initialized, updating sys props won't have any effect
30+
static {
31+
LogFactory.releaseAll();
32+
System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");
33+
System.setProperty("org.apache.commons.logging.simplelog.showdatetime", "true");
34+
}
35+
private static final Log LOG3 = LogFactory.getLog(LoggingTest.class);
36+
37+
@Test
38+
public void test1() throws Exception {
39+
40+
LOG.trace("Hello, World!");
41+
42+
LOG.debug("Hello, World!");
43+
assertFalse(LOG.isDebugEnabled());
44+
45+
LOG.info("Hello, World!");
46+
assertTrue(LOG.isInfoEnabled());
47+
48+
LOG.warn("Hello, World!");
49+
50+
LOG.error("Hello, World!");
51+
52+
LOG.fatal("Hello, World!");
53+
LOG.fatal("Hello, World!", new IOException("test exception"));
54+
55+
LOG.fatal("Hello, World!");
56+
}
57+
58+
@Test
59+
public void test2() throws Exception {
60+
assertFalse(LOG2.isTraceEnabled());
61+
assertTrue(LOG2.isDebugEnabled());
62+
63+
LOG2.info("Hello, World! - now using SimpleLog without timestamps");
64+
}
65+
66+
@Test
67+
public void test3() throws Exception {
68+
LOG3.info("Hello, World! - now using SimpleLog still without timestamps");
69+
}
70+
}

pom.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
<module>aop-dynamic-proxy-jetty-example</module>
3131
<module>appbundler-example</module>
3232
<module>classpath-jar</module>
33+
<module>commons-logging-example</module>
3334
<module>compile-time-annotation-processing</module>
3435
<module>download-plugin-example</module>
3536
<module>gwt-example</module>
@@ -46,6 +47,8 @@
4647
<module>multimodule-archetype-example</module>
4748
<!-- <module>nar-maven-plugin-jni-example</module> not working atm -->
4849
<module>os-profiles-example</module>
50+
<module>ttf2svg-batik-example</module>
51+
<module>wildfly-maven-plugin-example</module>
4952
</modules>
5053

5154
<!-- these profiles are for modules that only work in specific circumstances (older jvms, specific platforms) -->
@@ -139,12 +142,12 @@
139142
<dependency>
140143
<groupId>junit</groupId>
141144
<artifactId>junit</artifactId>
142-
<version>4.12</version>
145+
<version>4.13.2</version>
143146
</dependency>
144147
<dependency>
145148
<groupId>commons-io</groupId>
146149
<artifactId>commons-io</artifactId>
147-
<version>2.6</version>
150+
<version>2.8.0</version>
148151
</dependency>
149152
</dependencies>
150153
</dependencyManagement>

ttf2svg-batik-example/pom.xml

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="
4+
http://maven.apache.org/POM/4.0.0
5+
http://maven.apache.org/maven-v4_0_0.xsd
6+
">
7+
8+
<modelVersion>4.0.0</modelVersion>
9+
10+
<parent>
11+
<groupId>com.github.jjYBdx4IL.maven.examples</groupId>
12+
<artifactId>github-maven-examples</artifactId>
13+
<version>1.0-SNAPSHOT</version>
14+
</parent>
15+
16+
<artifactId>ttf2svg-batik-example</artifactId>
17+
18+
<name>Maven Examples :: TTF2SVG Batik Example</name>
19+
20+
<properties>
21+
<maven.compiler.source>15</maven.compiler.source>
22+
<maven.compiler.target>15</maven.compiler.target>
23+
</properties>
24+
25+
<dependencies>
26+
<dependency>
27+
<groupId>org.apache.xmlgraphics</groupId>
28+
<artifactId>batik-rasterizer</artifactId>
29+
<version>1.14</version>
30+
<exclusions>
31+
<exclusion>
32+
<groupId>xml-apis</groupId>
33+
<artifactId>xml-apis</artifactId>
34+
</exclusion>
35+
</exclusions>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.apache.xmlgraphics</groupId>
39+
<artifactId>batik-svggen</artifactId>
40+
<version>1.14</version>
41+
</dependency>
42+
<dependency>
43+
<groupId>commons-io</groupId>
44+
<artifactId>commons-io</artifactId>
45+
</dependency>
46+
47+
<dependency>
48+
<groupId>junit</groupId>
49+
<artifactId>junit</artifactId>
50+
<scope>test</scope>
51+
</dependency>
52+
</dependencies>
53+
</project>
54+
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
package testroot;
2+
3+
import static java.nio.charset.StandardCharsets.UTF_8;
4+
5+
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
6+
import org.apache.batik.apps.rasterizer.Main;
7+
import org.apache.batik.svggen.font.SVGFont;
8+
import org.apache.batik.util.XMLResourceDescriptor;
9+
import org.apache.commons.io.FileUtils;
10+
import org.junit.Before;
11+
import org.junit.Test;
12+
import org.w3c.dom.svg.SVGDocument;
13+
14+
import java.awt.Desktop;
15+
import java.awt.FontFormatException;
16+
import java.io.File;
17+
import java.io.IOException;
18+
import java.net.URL;
19+
import java.util.HashMap;
20+
import java.util.Iterator;
21+
import java.util.Locale;
22+
import java.util.Map;
23+
import java.util.zip.ZipFile;
24+
25+
import javax.xml.namespace.NamespaceContext;
26+
import javax.xml.xpath.XPath;
27+
import javax.xml.xpath.XPathConstants;
28+
import javax.xml.xpath.XPathExpressionException;
29+
import javax.xml.xpath.XPathFactory;
30+
31+
public class Ttf2SvgTest {
32+
33+
public static final String fontUrl = "https://fonts.google.com/download?family=Pattaya";
34+
private static final File workDir = new File("./target");
35+
private static final File fontFile = new File(workDir, "font.ttf");
36+
private static final File svgOutFile = new File(workDir, "output.svg");
37+
private static final File svgOutFile2 = new File(workDir, "output2.svg");
38+
private static final File imgOutFile = new File(workDir, "output.png");
39+
40+
@Test
41+
public void testTtf2Svg() throws Exception {
42+
// convert ttf -> svg (font def)
43+
// https://xmlgraphics.apache.org/batik/tools/font-converter.html
44+
SVGFont.main(new String[] { fontFile.getAbsolutePath(), "-o", svgOutFile.getAbsolutePath() });
45+
46+
String fontFamily = getFontFamily(svgOutFile);
47+
48+
// lets 'draw' some text
49+
String appendCommands = String.format(Locale.ROOT, """
50+
<g style="font-family:%s;font-size:40;fill:black">
51+
<text x="20" y="120">Hello, World!</text>
52+
</g>
53+
""", fontFamily);
54+
55+
String contents = FileUtils.readFileToString(svgOutFile, UTF_8);
56+
contents = contents.replace("</svg>", appendCommands + "</svg>");
57+
FileUtils.writeStringToFile(svgOutFile2, contents, UTF_8);
58+
59+
// rasterize to image
60+
// https://xmlgraphics.apache.org/batik/tools/rasterizer.html
61+
(new Main(new String[] { "-d", imgOutFile.getAbsolutePath(), "-scriptSecurityOff", "-bg", "255.255.255.255",
62+
"-m", "image/png", svgOutFile2.getAbsolutePath() })).execute();
63+
64+
if (isEclipseJunitRuner()) {
65+
Desktop.getDesktop().open(imgOutFile);
66+
}
67+
}
68+
69+
@Before
70+
public void before() throws IOException, FontFormatException {
71+
// download TTF from Google Fonts and cache it in ./target/
72+
File f = new File(workDir, "font.zip");
73+
if (!f.exists()) {
74+
FileUtils.copyURLToFile(new URL(fontUrl), f);
75+
}
76+
77+
// extract the .ttf file
78+
ZipFile zf = new ZipFile(f);
79+
FileUtils.copyInputStreamToFile(zf.getInputStream(zf.getEntry("Pattaya-Regular.ttf")), fontFile);
80+
zf.close();
81+
}
82+
83+
// https://xmlgraphics.apache.org/batik/using/dom-api.html
84+
private String getFontFamily(File svgFile) throws IOException, XPathExpressionException {
85+
String parser = XMLResourceDescriptor.getXMLParserClassName();
86+
SAXSVGDocumentFactory f = new SAXSVGDocumentFactory(parser);
87+
SVGDocument doc = f.createSVGDocument(svgFile.toURI().toASCIIString());
88+
89+
XPath xPath = XPathFactory.newInstance().newXPath();
90+
91+
// now for the funny part...
92+
xPath.setNamespaceContext(new NamespaceContext() {
93+
private final Map<String, String> map = new HashMap<>();
94+
{
95+
map.put("svg", "http://www.w3.org/2000/svg");
96+
}
97+
98+
public String getNamespaceURI(String prefix) {
99+
return map.get(prefix);
100+
}
101+
102+
public String getPrefix(String uri) {
103+
throw new UnsupportedOperationException();
104+
}
105+
106+
public Iterator<String> getPrefixes(String uri) {
107+
throw new UnsupportedOperationException();
108+
}
109+
});
110+
// ... lol.
111+
112+
return (String) xPath.evaluate("string(/svg:svg[1]/svg:defs[1]/svg:font[1]/svg:font-face[1]/@font-family[1])",
113+
doc, XPathConstants.STRING);
114+
}
115+
116+
private static boolean isEclipseJunitRuner() {
117+
return System.getProperty("sun.java.command", "").contains("org.eclipse.jdt.internal.junit.runner");
118+
}
119+
}

0 commit comments

Comments
 (0)