Merge branch 'master' into BAEL-4300
This commit is contained in:
+2
-4
@@ -85,7 +85,5 @@ transaction.log
|
||||
*-shell.log
|
||||
|
||||
apache-cxf/cxf-aegis/baeldung.xml
|
||||
apache-fop/src/test/resources/input.xml
|
||||
apache-fop/src/test/resources/output_herold.pdf
|
||||
apache-fop/src/test/resources/output_html2fo.pdf
|
||||
apache-fop/src/test/resources/output_jtidy.pdf
|
||||
|
||||
libraries-2/*.db
|
||||
+13
-2
@@ -1,16 +1,21 @@
|
||||
package com.baeldung.jgrapht;
|
||||
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.awt.Color;
|
||||
import java.awt.image.BufferedImage;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.imageio.ImageIO;
|
||||
|
||||
import org.jgrapht.ext.JGraphXAdapter;
|
||||
import org.jgrapht.graph.DefaultDirectedGraph;
|
||||
import org.jgrapht.graph.DefaultEdge;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.mxgraph.layout.mxCircleLayout;
|
||||
import com.mxgraph.layout.mxIGraphLayout;
|
||||
import com.mxgraph.util.mxCellRenderer;
|
||||
@@ -20,7 +25,7 @@ public class GraphImageGenerationUnitTest {
|
||||
|
||||
@Before
|
||||
public void createGraph() throws IOException {
|
||||
File imgFile = new File("src/test/resources/graph.png");
|
||||
File imgFile = new File("src/test/resources/graph1.png");
|
||||
imgFile.createNewFile();
|
||||
g = new DefaultDirectedGraph<String, DefaultEdge>(DefaultEdge.class);
|
||||
String x1 = "x1";
|
||||
@@ -34,12 +39,18 @@ public class GraphImageGenerationUnitTest {
|
||||
g.addEdge(x3, x1);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanup() {
|
||||
File imgFile = new File("src/test/resources/graph1.png");
|
||||
imgFile.deleteOnExit();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAdaptedGraph_whenWriteBufferedImage_ThenFileShouldExist() throws IOException {
|
||||
JGraphXAdapter<String, DefaultEdge> graphAdapter = new JGraphXAdapter<String, DefaultEdge>(g);
|
||||
mxIGraphLayout layout = new mxCircleLayout(graphAdapter);
|
||||
layout.execute(graphAdapter.getDefaultParent());
|
||||
File imgFile = new File("src/test/resources/graph.png");
|
||||
File imgFile = new File("src/test/resources/graph1.png");
|
||||
BufferedImage image = mxCellRenderer.createBufferedImage(graphAdapter, null, 2, Color.WHITE, true, null);
|
||||
ImageIO.write(image, "PNG", imgFile);
|
||||
assertTrue(imgFile.exists());
|
||||
|
||||
@@ -0,0 +1,208 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Board {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Board.class);
|
||||
|
||||
private final int[][] board;
|
||||
|
||||
private final int score;
|
||||
|
||||
public Board(int size) {
|
||||
assert(size > 0);
|
||||
|
||||
this.board = new int[size][];
|
||||
this.score = 0;
|
||||
|
||||
for (int x = 0; x < size; ++x) {
|
||||
this.board[x] = new int[size];
|
||||
for (int y = 0; y < size; ++y) {
|
||||
board[x][y] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Board(int[][] board, int score) {
|
||||
this.score = score;
|
||||
this.board = new int[board.length][];
|
||||
|
||||
for (int x = 0; x < board.length; ++x) {
|
||||
this.board[x] = Arrays.copyOf(board[x], board[x].length);
|
||||
}
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return board.length;
|
||||
}
|
||||
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
public int getCell(Cell cell) {
|
||||
int x = cell.getX();
|
||||
int y = cell.getY();
|
||||
assert(x >= 0 && x < board.length);
|
||||
assert(y >= 0 && y < board.length);
|
||||
|
||||
return board[x][y];
|
||||
}
|
||||
|
||||
public boolean isEmpty(Cell cell) {
|
||||
return getCell(cell) == 0;
|
||||
}
|
||||
|
||||
public List<Cell> emptyCells() {
|
||||
List<Cell> result = new ArrayList<>();
|
||||
for (int x = 0; x < board.length; ++x) {
|
||||
for (int y = 0; y < board[x].length; ++y) {
|
||||
Cell cell = new Cell(x, y);
|
||||
if (isEmpty(cell)) {
|
||||
result.add(cell);
|
||||
}
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Board placeTile(Cell cell, int number) {
|
||||
if (!isEmpty(cell)) {
|
||||
throw new IllegalArgumentException("That cell is not empty");
|
||||
}
|
||||
|
||||
Board result = new Board(this.board, this.score);
|
||||
result.board[cell.getX()][cell.getY()] = number;
|
||||
return result;
|
||||
}
|
||||
|
||||
public Board move(Move move) {
|
||||
// Clone the board
|
||||
int[][] tiles = new int[this.board.length][];
|
||||
for (int x = 0; x < this.board.length; ++x) {
|
||||
tiles[x] = Arrays.copyOf(this.board[x], this.board[x].length);
|
||||
}
|
||||
|
||||
LOG.debug("Before move: {}", Arrays.deepToString(tiles));
|
||||
// If we're doing an Left/Right move then transpose the board to make it a Up/Down move
|
||||
if (move == Move.LEFT || move == Move.RIGHT) {
|
||||
tiles = transpose(tiles);
|
||||
LOG.debug("After transpose: {}", Arrays.deepToString(tiles));
|
||||
}
|
||||
// If we're doing a Right/Down move then reverse the board.
|
||||
// With the above we're now always doing an Up move
|
||||
if (move == Move.DOWN || move == Move.RIGHT) {
|
||||
tiles = reverse(tiles);
|
||||
LOG.debug("After reverse: {}", Arrays.deepToString(tiles));
|
||||
}
|
||||
LOG.debug("Ready to move: {}", Arrays.deepToString(tiles));
|
||||
|
||||
// Shift everything up
|
||||
int[][] result = new int[tiles.length][];
|
||||
int newScore = 0;
|
||||
for (int x = 0; x < tiles.length; ++x) {
|
||||
LinkedList<Integer> thisRow = new LinkedList<>();
|
||||
for (int y = 0; y < tiles[0].length; ++y) {
|
||||
if (tiles[x][y] > 0) {
|
||||
thisRow.add(tiles[x][y]);
|
||||
}
|
||||
}
|
||||
|
||||
LOG.debug("Unmerged row: {}", thisRow);
|
||||
LinkedList<Integer> newRow = new LinkedList<>();
|
||||
while (thisRow.size() >= 2) {
|
||||
int first = thisRow.pop();
|
||||
int second = thisRow.peek();
|
||||
LOG.debug("Looking at numbers {} and {}", first, second);
|
||||
if (second == first) {
|
||||
LOG.debug("Numbers match, combining");
|
||||
int newNumber = first * 2;
|
||||
newRow.add(newNumber);
|
||||
newScore += newNumber;
|
||||
thisRow.pop();
|
||||
} else {
|
||||
LOG.debug("Numbers don't match");
|
||||
newRow.add(first);
|
||||
}
|
||||
}
|
||||
newRow.addAll(thisRow);
|
||||
LOG.debug("Merged row: {}", newRow);
|
||||
|
||||
result[x] = new int[tiles[0].length];
|
||||
for (int y = 0; y < tiles[0].length; ++y) {
|
||||
if (newRow.isEmpty()) {
|
||||
result[x][y] = 0;
|
||||
} else {
|
||||
result[x][y] = newRow.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
LOG.debug("After moves: {}", Arrays.deepToString(result));
|
||||
|
||||
// Un-reverse the board
|
||||
if (move == Move.DOWN || move == Move.RIGHT) {
|
||||
result = reverse(result);
|
||||
LOG.debug("After reverse: {}", Arrays.deepToString(result));
|
||||
}
|
||||
// Un-transpose the board
|
||||
if (move == Move.LEFT || move == Move.RIGHT) {
|
||||
result = transpose(result);
|
||||
LOG.debug("After transpose: {}", Arrays.deepToString(result));
|
||||
}
|
||||
return new Board(result, this.score + newScore);
|
||||
}
|
||||
|
||||
private static int[][] transpose(int[][] input) {
|
||||
int[][] result = new int[input.length][];
|
||||
|
||||
for (int x = 0; x < input.length; ++x) {
|
||||
result[x] = new int[input[0].length];
|
||||
for (int y = 0; y < input[0].length; ++y) {
|
||||
result[x][y] = input[y][x];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
private static int[][] reverse(int[][] input) {
|
||||
int[][] result = new int[input.length][];
|
||||
|
||||
for (int x = 0; x < input.length; ++x) {
|
||||
result[x] = new int[input[0].length];
|
||||
for (int y = 0; y < input[0].length; ++y) {
|
||||
result[x][y] = input[x][input.length - y - 1];
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return Arrays.deepToString(board);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
return true;
|
||||
}
|
||||
if (o == null || getClass() != o.getClass()) {
|
||||
return false;
|
||||
}
|
||||
Board board1 = (Board) o;
|
||||
return Arrays.deepEquals(board, board1.board);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Arrays.deepHashCode(board);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.util.StringJoiner;
|
||||
|
||||
public class Cell {
|
||||
private int x;
|
||||
private int y;
|
||||
|
||||
public Cell(int x, int y) {
|
||||
this.x = x;
|
||||
this.y = y;
|
||||
}
|
||||
|
||||
public int getX() {
|
||||
return x;
|
||||
}
|
||||
|
||||
public int getY() {
|
||||
return y;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return new StringJoiner(", ", Cell.class.getSimpleName() + "[", "]").add("x=" + x).add("y=" + y).toString();
|
||||
}
|
||||
}
|
||||
+27
@@ -0,0 +1,27 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.security.SecureRandom;
|
||||
import java.util.List;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Computer {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Computer.class);
|
||||
|
||||
private final SecureRandom rng = new SecureRandom();
|
||||
|
||||
public Board makeMove(Board input) {
|
||||
List<Cell> emptyCells = input.emptyCells();
|
||||
LOG.info("Number of empty cells: {}", emptyCells.size());
|
||||
|
||||
double numberToPlace = rng.nextDouble();
|
||||
LOG.info("New number probability: {}", numberToPlace);
|
||||
|
||||
int indexToPlace = rng.nextInt(emptyCells.size());
|
||||
Cell cellToPlace = emptyCells.get(indexToPlace);
|
||||
LOG.info("Placing number into empty cell: {}", cellToPlace);
|
||||
|
||||
return input.placeTile(cellToPlace, numberToPlace >= 0.9 ? 4 : 2);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,126 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.apache.commons.math3.util.Pair;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
public class Human {
|
||||
private static final Logger LOG = LoggerFactory.getLogger(Human.class);
|
||||
|
||||
public Board makeMove(Board input) {
|
||||
// For each move in MOVE
|
||||
// Generate board from move
|
||||
// Generate Score for Board
|
||||
// Return board with the best score
|
||||
//
|
||||
// Generate Score
|
||||
// If Depth Limit
|
||||
// Return Final Score
|
||||
// Total Score = 0
|
||||
// For every empty square in new board
|
||||
// Generate board with "2" in square
|
||||
// Calculate Score
|
||||
// Total Score += (Score * 0.9)
|
||||
// Generate board with "4" in square
|
||||
// Calculate Score
|
||||
// Total Score += (Score * 0.1)
|
||||
//
|
||||
// Calculate Score
|
||||
// For each move in MOVE
|
||||
// Generate board from move
|
||||
// Generate score for board
|
||||
// Return the best generated score
|
||||
|
||||
return Arrays.stream(Move.values())
|
||||
.parallel()
|
||||
.map(input::move)
|
||||
.filter(board -> !board.equals(input))
|
||||
.max(Comparator.comparingInt(board -> generateScore(board, 0)))
|
||||
.orElse(input);
|
||||
}
|
||||
|
||||
private int generateScore(Board board, int depth) {
|
||||
if (depth >= 3) {
|
||||
int finalScore = calculateFinalScore(board);
|
||||
LOG.debug("Final score for board {}: {}", board,finalScore);
|
||||
return finalScore;
|
||||
}
|
||||
|
||||
return board.emptyCells().stream()
|
||||
.parallel()
|
||||
.flatMap(cell -> Stream.of(new Pair<>(cell, 2), new Pair<>(cell, 4)))
|
||||
.mapToInt(move -> {
|
||||
LOG.debug("Simulating move {} at depth {}", move, depth);
|
||||
Board newBoard = board.placeTile(move.getFirst(), move.getSecond());
|
||||
int boardScore = calculateScore(newBoard, depth + 1);
|
||||
int calculatedScore = (int) (boardScore * (move.getSecond() == 2 ? 0.9 : 0.1));
|
||||
LOG.debug("Calculated score for board {} and move {} at depth {}: {}", newBoard, move, depth, calculatedScore);
|
||||
return calculatedScore;
|
||||
})
|
||||
.sum();
|
||||
}
|
||||
|
||||
private int calculateScore(Board board, int depth) {
|
||||
return Arrays.stream(Move.values())
|
||||
.parallel()
|
||||
.map(board::move)
|
||||
.filter(moved -> !moved.equals(board))
|
||||
.mapToInt(newBoard -> generateScore(newBoard, depth))
|
||||
.max()
|
||||
.orElse(0);
|
||||
}
|
||||
|
||||
private int calculateFinalScore(Board board) {
|
||||
List<List<Integer>> rowsToScore = new ArrayList<>();
|
||||
for (int i = 0; i < board.getSize(); ++i) {
|
||||
List<Integer> row = new ArrayList<>();
|
||||
List<Integer> col = new ArrayList<>();
|
||||
|
||||
for (int j = 0; j < board.getSize(); ++j) {
|
||||
row.add(board.getCell(new Cell(i, j)));
|
||||
col.add(board.getCell(new Cell(j, i)));
|
||||
}
|
||||
|
||||
rowsToScore.add(row);
|
||||
rowsToScore.add(col);
|
||||
}
|
||||
|
||||
return rowsToScore.stream()
|
||||
.parallel()
|
||||
.mapToInt(row -> {
|
||||
List<Integer> preMerged = row.stream()
|
||||
.filter(value -> value != 0)
|
||||
.collect(Collectors.toList());
|
||||
|
||||
int numMerges = 0;
|
||||
int monotonicityLeft = 0;
|
||||
int monotonicityRight = 0;
|
||||
for (int i = 0; i < preMerged.size() - 1; ++i) {
|
||||
Integer first = preMerged.get(i);
|
||||
Integer second = preMerged.get(i + 1);
|
||||
if (first.equals(second)) {
|
||||
++numMerges;
|
||||
} else if (first > second) {
|
||||
monotonicityLeft += first - second;
|
||||
} else {
|
||||
monotonicityRight += second - first;
|
||||
}
|
||||
}
|
||||
|
||||
int score = 1000;
|
||||
score += 250 * row.stream().filter(value -> value == 0).count();
|
||||
score += 750 * numMerges;
|
||||
score -= 10 * row.stream().mapToInt(value -> value).sum();
|
||||
score -= 50 * Math.min(monotonicityLeft, monotonicityRight);
|
||||
return score;
|
||||
})
|
||||
.sum();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
public enum Move {
|
||||
UP,
|
||||
DOWN,
|
||||
LEFT,
|
||||
RIGHT
|
||||
}
|
||||
+73
@@ -0,0 +1,73 @@
|
||||
package com.baeldung.algorithms.play2048;
|
||||
|
||||
public class Play2048 {
|
||||
private static final int SIZE = 4;
|
||||
private static final int INITIAL_NUMBERS = 2;
|
||||
|
||||
public static void main(String[] args) {
|
||||
// The board and players
|
||||
Board board = new Board(SIZE);
|
||||
Computer computer = new Computer();
|
||||
Human human = new Human();
|
||||
|
||||
// The computer has two moves first
|
||||
System.out.println("Setup");
|
||||
System.out.println("=====");
|
||||
for (int i = 0; i < INITIAL_NUMBERS; ++i) {
|
||||
board = computer.makeMove(board);
|
||||
}
|
||||
|
||||
printBoard(board);
|
||||
do {
|
||||
board = human.makeMove(board);
|
||||
System.out.println("Human move");
|
||||
System.out.println("==========");
|
||||
printBoard(board);
|
||||
|
||||
board = computer.makeMove(board);
|
||||
System.out.println("Computer move");
|
||||
System.out.println("=============");
|
||||
printBoard(board);
|
||||
} while (!board.emptyCells().isEmpty());
|
||||
|
||||
System.out.println("Final Score: " + board.getScore());
|
||||
|
||||
}
|
||||
|
||||
private static void printBoard(Board board) {
|
||||
StringBuilder topLines = new StringBuilder();
|
||||
StringBuilder midLines = new StringBuilder();
|
||||
for (int x = 0; x < board.getSize(); ++x) {
|
||||
topLines.append("+--------");
|
||||
midLines.append("| ");
|
||||
}
|
||||
topLines.append("+");
|
||||
midLines.append("|");
|
||||
|
||||
|
||||
for (int y = 0; y < board.getSize(); ++y) {
|
||||
System.out.println(topLines);
|
||||
System.out.println(midLines);
|
||||
for (int x = 0; x < board.getSize(); ++x) {
|
||||
Cell cell = new Cell(x, y);
|
||||
System.out.print("|");
|
||||
if (board.isEmpty(cell)) {
|
||||
System.out.print(" ");
|
||||
} else {
|
||||
StringBuilder output = new StringBuilder(Integer.toString(board.getCell(cell)));
|
||||
while (output.length() < 8) {
|
||||
output.append(" ");
|
||||
if (output.length() < 8) {
|
||||
output.insert(0, " ");
|
||||
}
|
||||
}
|
||||
System.out.print(output);
|
||||
}
|
||||
}
|
||||
System.out.println("|");
|
||||
System.out.println(midLines);
|
||||
}
|
||||
System.out.println(topLines);
|
||||
System.out.println("Score: " + board.getScore());
|
||||
}
|
||||
}
|
||||
+5
-5
@@ -5,21 +5,21 @@ import static com.baeldung.algorithms.quicksort.SortingUtils.swap;
|
||||
|
||||
public class DutchNationalFlagPartioning {
|
||||
|
||||
public static Partition partition(int[] a, int begin, int end) {
|
||||
public static Partition partition(int[] input, int begin, int end) {
|
||||
int lt = begin, current = begin, gt = end;
|
||||
int partitioningValue = a[begin];
|
||||
int partitioningValue = input[begin];
|
||||
|
||||
while (current <= gt) {
|
||||
int compareCurrent = compare(a[current], partitioningValue);
|
||||
int compareCurrent = compare(input[current], partitioningValue);
|
||||
switch (compareCurrent) {
|
||||
case -1:
|
||||
swap(a, current++, lt++);
|
||||
swap(input, current++, lt++);
|
||||
break;
|
||||
case 0:
|
||||
current++;
|
||||
break;
|
||||
case 1:
|
||||
swap(a, current, gt--);
|
||||
swap(input, current, gt--);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
## Apache Avro
|
||||
|
||||
This module contains articles about Apache Avro
|
||||
|
||||
### Relevant Articles:
|
||||
- [Guide to Apache Avro](https://www.baeldung.com/java-apache-avro)
|
||||
@@ -1,72 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>apache-avro</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-avro</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-simple</artifactId>
|
||||
<version>${slf4j.version}</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro-compiler</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro-maven-plugin</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.avro</groupId>
|
||||
<artifactId>avro-maven-plugin</artifactId>
|
||||
<version>${avro.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>schemas</id>
|
||||
<phase>generate-sources</phase>
|
||||
<goals>
|
||||
<goal>schema</goal>
|
||||
<goal>protocol</goal>
|
||||
<goal>idl-protocol</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<sourceDirectory>${project.basedir}/src/main/resources/</sourceDirectory>
|
||||
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<avro.version>1.8.2</avro.version>
|
||||
<slf4j.version>1.7.25</slf4j.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,3 +0,0 @@
|
||||
### Relevant Articles:
|
||||
|
||||
- [Introduction to Apache Beam](https://www.baeldung.com/apache-beam)
|
||||
@@ -1,43 +0,0 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<groupId>com.baeldung.apache</groupId>
|
||||
<artifactId>apache-beam</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.beam</groupId>
|
||||
<artifactId>beam-sdks-java-core</artifactId>
|
||||
<version>${beam.version}</version>
|
||||
</dependency>
|
||||
<!-- runtime scoped -->
|
||||
<dependency>
|
||||
<groupId>org.apache.beam</groupId>
|
||||
<artifactId>beam-runners-direct-java</artifactId>
|
||||
<version>${beam.version}</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<beam.version>2.19.0</beam.version>
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,7 +0,0 @@
|
||||
## Apache BVal
|
||||
|
||||
This module contains articles about Apache BVal
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Intro to Apache BVal](https://www.baeldung.com/apache-bval)
|
||||
@@ -1,40 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>apache-bval</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-bval</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.bval</groupId>
|
||||
<artifactId>bval-jsr</artifactId>
|
||||
<version>${bval.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.validation</groupId>
|
||||
<artifactId>validation-api</artifactId>
|
||||
<version>${javax.validation.validation-api.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.bval</groupId>
|
||||
<artifactId>bval-extras</artifactId>
|
||||
<version>${bval.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<bval.version>1.1.2</bval.version>
|
||||
<javax.validation.validation-api.version>1.1.0.Final</javax.validation.validation-api.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,7 +0,0 @@
|
||||
## Apache Curator
|
||||
|
||||
This module contains articles about Apache Curator
|
||||
|
||||
### Relevant Articles:
|
||||
|
||||
- [Introduction to Apache Curator](https://www.baeldung.com/apache-curator)
|
||||
@@ -1,70 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>apache-curator</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>apache-curator</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- curator -->
|
||||
<dependency>
|
||||
<groupId>org.apache.curator</groupId>
|
||||
<artifactId>curator-x-async</artifactId>
|
||||
<version>${curator.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.curator</groupId>
|
||||
<artifactId>curator-recipes</artifactId>
|
||||
<version>${curator.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.apache.zookeeper</groupId>
|
||||
<artifactId>zookeeper</artifactId>
|
||||
<version>${zookeeper.version}</version>
|
||||
</dependency>
|
||||
<!-- utils -->
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
<artifactId>jackson-databind</artifactId>
|
||||
<version>${jackson.version}</version>
|
||||
</dependency>
|
||||
<!-- test scoped -->
|
||||
<dependency>
|
||||
<groupId>org.assertj</groupId>
|
||||
<artifactId>assertj-core</artifactId>
|
||||
<version>${assertj.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.jayway.awaitility</groupId>
|
||||
<artifactId>awaitility</artifactId>
|
||||
<version>${avaitility.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<properties>
|
||||
<curator.version>4.0.1</curator.version>
|
||||
<zookeeper.version>3.4.11</zookeeper.version>
|
||||
<!-- testing -->
|
||||
<assertj.version>3.6.1</assertj.version>
|
||||
<avaitility.version>1.7.0</avaitility.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,15 +0,0 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.war
|
||||
*.ear
|
||||
|
||||
# Files generated by integration tests
|
||||
*.txt
|
||||
@@ -1,93 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project
|
||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>apache-fop</artifactId>
|
||||
<version>0.1-SNAPSHOT</version>
|
||||
<name>apache-fop</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<!-- new dependencies -->
|
||||
<dependency>
|
||||
<groupId>org.apache.xmlgraphics</groupId>
|
||||
<artifactId>fop</artifactId>
|
||||
<version>${fop.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.apache.avalon.framework</groupId>
|
||||
<artifactId>avalon-framework-api</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>org.apache.avalon.framework</groupId>
|
||||
<artifactId>avalon-framework-impl</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>avalon-framework</groupId>
|
||||
<artifactId>avalon-framework-api</artifactId>
|
||||
<version>${avalon-framework.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>avalon-framework</groupId>
|
||||
<artifactId>avalon-framework-impl</artifactId>
|
||||
<version>${avalon-framework.version}</version>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<artifactId>commons-logging</artifactId>
|
||||
<groupId>commons-logging</groupId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dbdoclet</groupId>
|
||||
<artifactId>dbdoclet</artifactId>
|
||||
<version>${dbdoclet.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dbdoclet</groupId>
|
||||
<artifactId>herold</artifactId>
|
||||
<version>${org.dbdoclet.herold.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>net.sf.jtidy</groupId>
|
||||
<artifactId>jtidy</artifactId>
|
||||
<version>${jtidy.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<finalName>apache-fop</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<fop.version>1.1</fop.version>
|
||||
<avalon-framework.version>4.3</avalon-framework.version>
|
||||
<dbdoclet.version>8.0.2</dbdoclet.version>
|
||||
<jtidy.version>r938</jtidy.version>
|
||||
<org.dbdoclet.herold.version>8.0.4</org.dbdoclet.herold.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -1,19 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<logger name="org.springframework" level="WARN" />
|
||||
<logger name="org.springframework.transaction" level="WARN" />
|
||||
|
||||
<!-- in order to debug some marshalling issues, this needs to be TRACE -->
|
||||
<logger name="org.springframework.web.servlet.mvc" level="WARN" />
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -1,6 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<beans xmlns="http://www.springframework.org/schema/beans"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd" >
|
||||
|
||||
</beans>
|
||||
@@ -1,41 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
|
||||
xsi:schemaLocation="
|
||||
http://java.sun.com/xml/ns/javaee
|
||||
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"
|
||||
>
|
||||
|
||||
<display-name>Spring MVC Application</display-name>
|
||||
|
||||
<!-- Spring root -->
|
||||
<context-param>
|
||||
<param-name>contextClass</param-name>
|
||||
<param-value>
|
||||
org.springframework.web.context.support.AnnotationConfigWebApplicationContext
|
||||
</param-value>
|
||||
</context-param>
|
||||
<context-param>
|
||||
<param-name>contextConfigLocation</param-name>
|
||||
<param-value>com.baeldung.config</param-value>
|
||||
</context-param>
|
||||
|
||||
<listener>
|
||||
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
|
||||
</listener>
|
||||
|
||||
<!-- Spring child -->
|
||||
<servlet>
|
||||
<servlet-name>api</servlet-name>
|
||||
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
|
||||
<load-on-startup>1</load-on-startup>
|
||||
</servlet>
|
||||
<servlet-mapping>
|
||||
<servlet-name>api</servlet-name>
|
||||
<url-pattern>/</url-pattern>
|
||||
</servlet-mapping>
|
||||
|
||||
<welcome-file-list>
|
||||
<welcome-file/>
|
||||
</welcome-file-list>
|
||||
|
||||
</web-app>
|
||||
@@ -1,122 +0,0 @@
|
||||
package com.baeldung.java;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.OutputStream;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.apache.fop.apps.Fop;
|
||||
import org.apache.fop.apps.FopFactory;
|
||||
import org.apache.xmlgraphics.util.MimeConstants;
|
||||
import org.dbdoclet.trafo.html.docbook.HtmlDocBookTrafo;
|
||||
import org.dbdoclet.trafo.script.Script;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.tidy.Tidy;
|
||||
|
||||
public class ApacheFOPConvertHTMLIntegrationTest {
|
||||
private final String inputFile = "src/test/resources/input.html";
|
||||
private final String style = "src/test/resources/xhtml2fo.xsl";
|
||||
private final String style1 = "src/test/resources/docbook-xsl/fo/docbook.xsl";
|
||||
private final String output_jtidy = "src/test/resources/output_jtidy.pdf";
|
||||
private final String output_html2fo = "src/test/resources/output_html2fo.pdf";
|
||||
private final String output_herold = "src/test/resources/output_herold.pdf";
|
||||
private final String foFile = "src/test/resources/input.fo";
|
||||
private final String xmlFile = "src/test/resources/input.xml";
|
||||
|
||||
@Test
|
||||
public void whenTransformHTMLToPDFUsingJTidy_thenCorrect() throws Exception {
|
||||
final Document xhtml = fromHTMLToXHTML();
|
||||
final Document fo = fromXHTMLToFO(xhtml);
|
||||
fromFODocumentToPDF(fo, output_jtidy);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTransformFromHTML2FOToPDF_thenCorrect() throws Exception {
|
||||
fromFOFileToPDF();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenTransformFromHeroldToPDF_thenCorrect() throws Exception {
|
||||
fromHTMLTOXMLUsingHerold();
|
||||
final Document fo = fromXMLFileToFO();
|
||||
fromFODocumentToPDF(fo, output_herold);
|
||||
}
|
||||
|
||||
private Document fromHTMLToXHTML() throws FileNotFoundException {
|
||||
final FileInputStream reader = new FileInputStream(inputFile);
|
||||
final Tidy tidy = new Tidy();
|
||||
final Document xml = tidy.parseDOM(reader, null);
|
||||
return xml;
|
||||
}
|
||||
|
||||
private Document fromXHTMLToFO(final Document xhtml) throws Exception {
|
||||
final DOMSource source = new DOMSource(xhtml);
|
||||
final DOMResult result = new DOMResult();
|
||||
final Transformer transformer = createTransformer(style);
|
||||
transformer.transform(source, result);
|
||||
return (Document) result.getNode();
|
||||
}
|
||||
|
||||
private void fromFODocumentToPDF(final Document fo, final String outputFile) throws Exception {
|
||||
final FopFactory fopFactory = FopFactory.newInstance();
|
||||
final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(outputFile)));
|
||||
|
||||
final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream);
|
||||
final TransformerFactory factory = TransformerFactory.newInstance();
|
||||
final Transformer transformer = factory.newTransformer();
|
||||
final DOMSource src = new DOMSource(fo);
|
||||
final Result res = new SAXResult(fop.getDefaultHandler());
|
||||
transformer.transform(src, res);
|
||||
|
||||
outStream.close();
|
||||
}
|
||||
|
||||
private Transformer createTransformer(final String styleFile) throws Exception {
|
||||
final TransformerFactory factory = TransformerFactory.newInstance();
|
||||
final Transformer transformer = factory.newTransformer(new StreamSource(styleFile));
|
||||
|
||||
return transformer;
|
||||
}
|
||||
|
||||
private void fromFOFileToPDF() throws Exception {
|
||||
final FopFactory fopFactory = FopFactory.newInstance();
|
||||
final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(output_html2fo)));
|
||||
final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream);
|
||||
|
||||
final TransformerFactory factory = TransformerFactory.newInstance();
|
||||
final Transformer transformer = factory.newTransformer();
|
||||
final Source src = new StreamSource(new FileInputStream(foFile));
|
||||
final Result res = new SAXResult(fop.getDefaultHandler());
|
||||
transformer.transform(src, res);
|
||||
|
||||
outStream.close();
|
||||
}
|
||||
|
||||
private Document fromXMLFileToFO() throws Exception {
|
||||
final Source source = new StreamSource(new FileInputStream(xmlFile));
|
||||
final DOMResult result = new DOMResult();
|
||||
final Transformer transformer = createTransformer(style1);
|
||||
transformer.transform(source, result);
|
||||
return (Document) result.getNode();
|
||||
}
|
||||
|
||||
private void fromHTMLTOXMLUsingHerold() throws Exception {
|
||||
final Script script = new Script();
|
||||
final HtmlDocBookTrafo transformer = new HtmlDocBookTrafo();
|
||||
transformer.setInputStream(new FileInputStream(inputFile));
|
||||
transformer.setOutputStream(new FileOutputStream(xmlFile));
|
||||
transformer.transform(script);
|
||||
}
|
||||
}
|
||||
@@ -1,143 +0,0 @@
|
||||
package com.baeldung.java;
|
||||
|
||||
import java.io.BufferedOutputStream;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
import java.net.HttpURLConnection;
|
||||
import java.net.URL;
|
||||
|
||||
import javax.xml.transform.Result;
|
||||
import javax.xml.transform.Source;
|
||||
import javax.xml.transform.Transformer;
|
||||
import javax.xml.transform.TransformerFactory;
|
||||
import javax.xml.transform.dom.DOMResult;
|
||||
import javax.xml.transform.dom.DOMSource;
|
||||
import javax.xml.transform.sax.SAXResult;
|
||||
import javax.xml.transform.stream.StreamSource;
|
||||
|
||||
import org.apache.fop.apps.Fop;
|
||||
import org.apache.fop.apps.FopFactory;
|
||||
import org.apache.xmlgraphics.util.MimeConstants;
|
||||
import org.dbdoclet.trafo.TrafoScriptManager;
|
||||
import org.dbdoclet.trafo.html.docbook.HtmlDocBookTrafo;
|
||||
import org.dbdoclet.trafo.script.Script;
|
||||
import org.junit.Test;
|
||||
import org.w3c.dom.Document;
|
||||
|
||||
public class ApacheFOPHeroldLiveTest {
|
||||
private final String[] inputUrls = {// @formatter:off
|
||||
// "http://www.baeldung.com/spring-security-basic-authentication",
|
||||
"http://www.baeldung.com/spring-security-digest-authentication"
|
||||
//"http://www.baeldung.com/spring-httpmessageconverter-rest",
|
||||
//"http://www.baeldung.com/2011/11/06/restful-web-service-discoverability-part-4/",
|
||||
//"http://www.baeldung.com/2011/11/13/rest-service-discoverability-with-spring-part-5/",
|
||||
//"http://www.baeldung.com/2013/01/11/etags-for-rest-with-spring/",
|
||||
// "http://www.baeldung.com/2012/01/18/rest-pagination-in-spring/",
|
||||
//"http://inprogress.baeldung.com/?p=1430",
|
||||
//"http://www.baeldung.com/2013/01/31/exception-handling-for-rest-with-spring-3-2/",
|
||||
//"http://www.baeldung.com/rest-versioning",
|
||||
//"http://www.baeldung.com/2013/01/18/testing-rest-with-multiple-mime-types/"
|
||||
}; // @formatter:on
|
||||
|
||||
private final String style_file = "src/test/resources/docbook-xsl/fo/docbook.xsl";
|
||||
private final String output_file = "src/test/resources/final_output.pdf";
|
||||
private final String xmlInput = "src/test/resources/input.xml";
|
||||
private final String xmlOutput = "src/test/resources/output.xml";
|
||||
|
||||
// tests
|
||||
|
||||
@Test
|
||||
public void whenTransformFromHeroldToPDF_thenCorrect() throws Exception {
|
||||
final int len = inputUrls.length;
|
||||
fromHTMLTOXMLUsingHerold(inputUrls[0], false);
|
||||
for (int i = 1; i < len; i++) {
|
||||
fromHTMLTOXMLUsingHerold(inputUrls[i], true);
|
||||
}
|
||||
fixXML(xmlInput, xmlOutput);
|
||||
final Document fo = fromXMLFileToFO();
|
||||
fromFODocumentToPDF(fo, output_file);
|
||||
}
|
||||
|
||||
// UTIL
|
||||
|
||||
private void fromHTMLTOXMLUsingHerold(final String input, final boolean append) throws Exception {
|
||||
Script script;
|
||||
final TrafoScriptManager mgr = new TrafoScriptManager();
|
||||
final File profileFile = new File("src/test/resources/default.her");
|
||||
script = mgr.parseScript(profileFile);
|
||||
final HtmlDocBookTrafo transformer = new HtmlDocBookTrafo();
|
||||
transformer.setInputStream(getInputStream(input));
|
||||
transformer.setOutputStream(new FileOutputStream(xmlInput, append));
|
||||
|
||||
transformer.transform(script);
|
||||
}
|
||||
|
||||
private Document fromXMLFileToFO() throws Exception {
|
||||
final Source source = new StreamSource(new FileInputStream(xmlOutput));
|
||||
final DOMResult result = new DOMResult();
|
||||
final Transformer transformer = createTransformer(style_file);
|
||||
transformer.transform(source, result);
|
||||
return (Document) result.getNode();
|
||||
}
|
||||
|
||||
private void fromFODocumentToPDF(final Document fo, final String outputFile) throws Exception {
|
||||
final FopFactory fopFactory = FopFactory.newInstance();
|
||||
final OutputStream outStream = new BufferedOutputStream(new FileOutputStream(new File(outputFile)));
|
||||
|
||||
final Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, outStream);
|
||||
final TransformerFactory factory = TransformerFactory.newInstance();
|
||||
final Transformer transformer = factory.newTransformer();
|
||||
final DOMSource src = new DOMSource(fo);
|
||||
final Result res = new SAXResult(fop.getDefaultHandler());
|
||||
transformer.transform(src, res);
|
||||
|
||||
outStream.close();
|
||||
}
|
||||
|
||||
private Transformer createTransformer(final String styleFile) throws Exception {
|
||||
final TransformerFactory factory = TransformerFactory.newInstance();
|
||||
final Transformer transformer = factory.newTransformer(new StreamSource(styleFile));
|
||||
|
||||
return transformer;
|
||||
}
|
||||
|
||||
private InputStream getInputStream(final String input) throws IOException {
|
||||
final URL url = new URL(input);
|
||||
final HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
|
||||
httpcon.addRequestProperty("User-Agent", "Mozilla/4.0");
|
||||
return httpcon.getInputStream();
|
||||
}
|
||||
|
||||
private void fixXML(final String input, final String output) throws IOException {
|
||||
final BufferedReader reader = new BufferedReader(new FileReader(input));
|
||||
final FileWriter writer = new FileWriter(output);
|
||||
String line = reader.readLine();
|
||||
int count = 0;
|
||||
while (line != null) {
|
||||
line = line.replaceAll("”", "\"");
|
||||
line = line.replaceAll("“", "\"");
|
||||
// line = line.replaceAll("[^\\x00-\\x7F]", "");
|
||||
|
||||
if (line.contains("info>")) {
|
||||
writer.write(line.replace("info>", "section>"));
|
||||
} else if (!((line.startsWith("<?xml") || line.startsWith("<article") || line.startsWith("</article")) && (count > 4))) {
|
||||
writer.write(line.replaceAll("xml:id=\"", "xml:id=\"" + count));
|
||||
}
|
||||
writer.write("\n");
|
||||
|
||||
line = reader.readLine();
|
||||
count++;
|
||||
}
|
||||
writer.write("</article>");
|
||||
reader.close();
|
||||
writer.close();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,13 +0,0 @@
|
||||
transformation html2docbook;
|
||||
|
||||
section HTML {
|
||||
encoding = "windows-1252";
|
||||
exclude = ["//h3[contains(.,'I usually')]", "//*[@id='toc_container']", "//h2/span[@id='Table_of_Contents']/../following-sibling::ul", "//h2/span[@id='Table_of_Contents']/..", "//*[@id='disqus_thread']","//head","//*[@class='custom-design-100']","//*[@class='custom-design-114']", "//form","//*[@src]","//*[@id='inner-wrapper']/*[position()<7]" , "//*[@class='post-meta']" , "//*[@class='entry-title']","//*[@id='respond']" ,"//*[@id='comments']","//*[@class='post-entries']","//*[@class='social']/../../h3[last()]" ,"//*[@class='social']/.."];
|
||||
section-numbering-pattern = "(((\d\.)+)?\d?\.?\p{Z}*).*";
|
||||
}
|
||||
|
||||
section DocBook {
|
||||
add-index = false;
|
||||
decompose-tables = false;
|
||||
encoding = "UTF-8";
|
||||
}
|
||||
@@ -1,4 +0,0 @@
|
||||
The DocBook XSL stylesheets are maintained by Norman Walsh,
|
||||
<[email protected]>, and members of the DocBook Project,
|
||||
<[email protected]>
|
||||
|
||||
@@ -1,21 +0,0 @@
|
||||
To view a list of all open DocBook Project XSL stylesheet bugs:
|
||||
|
||||
http://docbook.sf.net/tracker/xsl/bugs
|
||||
|
||||
To submit a bug report against the stylesheets:
|
||||
|
||||
http://docbook.sf.net/tracker/submit/bug
|
||||
|
||||
To do a full-text search of all DocBook Project issues:
|
||||
|
||||
http://docbook.sf.net/tracker/search
|
||||
|
||||
Discussion about the DocBook Project XSL stylesheets takes place
|
||||
on the docbook-apps mailing list:
|
||||
|
||||
http://wiki.docbook.org/topic/DocBookAppsMailingList
|
||||
|
||||
Real-time discussion takes place on IRC:
|
||||
|
||||
http://wiki.docbook.org/topic/DocBookIrcChannel
|
||||
irc://irc.freenode.net/docbook
|
||||
@@ -1,48 +0,0 @@
|
||||
Copyright
|
||||
---------
|
||||
Copyright (C) 1999-2007 Norman Walsh
|
||||
Copyright (C) 2003 Jiří Kosek
|
||||
Copyright (C) 2004-2007 Steve Ball
|
||||
Copyright (C) 2005-2008 The DocBook Project
|
||||
Copyright (C) 2011-2012 O'Reilly Media
|
||||
|
||||
Permission is hereby granted, free of charge, to any person
|
||||
obtaining a copy of this software and associated documentation
|
||||
files (the ``Software''), to deal in the Software without
|
||||
restriction, including without limitation the rights to use,
|
||||
copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
sell copies of the Software, and to permit persons to whom the
|
||||
Software is furnished to do so, subject to the following
|
||||
conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be
|
||||
included in all copies or substantial portions of the Software.
|
||||
|
||||
Except as contained in this notice, the names of individuals
|
||||
credited with contribution to this software shall not be used in
|
||||
advertising or otherwise to promote the sale, use or other
|
||||
dealings in this Software without prior written authorization
|
||||
from the individuals in question.
|
||||
|
||||
Any stylesheet derived from this Software that is publically
|
||||
distributed will be identified with a different name and the
|
||||
version strings in any derived Software will be changed so that
|
||||
no possibility of confusion between the derived package and this
|
||||
Software will exist.
|
||||
|
||||
Warranty
|
||||
--------
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
NONINFRINGEMENT. IN NO EVENT SHALL NORMAN WALSH OR ANY OTHER
|
||||
CONTRIBUTOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
Contacting the Author
|
||||
---------------------
|
||||
The DocBook XSL stylesheets are maintained by Norman Walsh,
|
||||
<[email protected]>, and members of the DocBook Project,
|
||||
<[email protected]>
|
||||
@@ -1,88 +0,0 @@
|
||||
$Id: INSTALL 6145 2006-08-06 13:13:03Z xmldoc $
|
||||
|
||||
INSTALL file for the DocBook XSL stylesheets distribution
|
||||
|
||||
----------------------------------------------------------------------
|
||||
Case #1: Installation using a package management system
|
||||
----------------------------------------------------------------------
|
||||
If you have installed the DocBook XSL distribution using "apt-get",
|
||||
"yum", "urpmi", or some similar package-management front-end,
|
||||
then, as part of the package installation, the stylesheets have
|
||||
already been automatically installed in the appropriate location
|
||||
for your system, and your XML catalog environment has probably
|
||||
been updated to use that location.
|
||||
|
||||
----------------------------------------------------------------------
|
||||
Case #2: Installing manually
|
||||
----------------------------------------------------------------------
|
||||
If you have downloaded a docbook-xsl zip, tar.gz, or tar.bz2
|
||||
file, use the following steps to install it.
|
||||
|
||||
1. Move the zip, tar.gz, or tar.bz2 file to the directory where
|
||||
you'd like to install it (not to a temporary directory).
|
||||
|
||||
2. unzip or untar/uncompress the file
|
||||
|
||||
That will create a docbook-xsl-$VERSION directory (where
|
||||
$VERSION is the version number for the release).
|
||||
|
||||
The remaining steps are all OPTIONAL. They are intended to
|
||||
automatically update your user environment with XML Catalog
|
||||
information about the DocBook XSL distribution. You are NOT
|
||||
REQUIRED to complete these remaining steps. However, if you do
|
||||
not, and you want to use XML catalogs with the DocBook XSL
|
||||
stylesheets, you will need to manually update your XML catalog
|
||||
environment
|
||||
|
||||
3. Change to the docbook-xsl-$VERSION directory and execute the
|
||||
install.sh script:
|
||||
|
||||
./install.sh
|
||||
|
||||
That will launch an interactive installer, which will emit a
|
||||
series of prompts for you to respond to.
|
||||
|
||||
To instead run it non-interactively without being prompted
|
||||
for confirmation of the changes it makes, invoke it with the
|
||||
"--batch" switch, like this:
|
||||
|
||||
./install.sh --batch
|
||||
|
||||
After the process is complete, the installer will emit a
|
||||
message with a command you need to run in order to source
|
||||
your environment for use with the stylesheets.
|
||||
|
||||
4. To test that he installation has updated your environment
|
||||
correctly, execute the test.sh script:
|
||||
|
||||
./test.sh
|
||||
|
||||
That will test your XML catalog environment, using both the
|
||||
xmlcatalog application and the Apache XML Commons Resolver.
|
||||
|
||||
NOTE: The test.sh file is not created until the install.sh
|
||||
file is run for the first time.
|
||||
|
||||
5. (UNINSTALLING) If/when you want to uninstall the release,
|
||||
execute the uninstall.sh script.
|
||||
|
||||
./uninstall.sh
|
||||
|
||||
To instead run it non-interactively without being prompted
|
||||
for confirmation of the changes it makes, invoke it with the
|
||||
"--batch" switch, like this:
|
||||
|
||||
./uninstall.sh --batch
|
||||
|
||||
NOTE: The uninstall.sh file is not created until the install.sh
|
||||
file is run for the first time.
|
||||
|
||||
|
||||
----------------------------------------------------------------------
|
||||
Note to packagers
|
||||
----------------------------------------------------------------------
|
||||
The install.sh, .CatalogManager.properties.example, and .urilist
|
||||
files should not be packaged. They are useful only to users who
|
||||
are installing the stylesheets manually.
|
||||
|
||||
The catalog.xml file should be packaged.
|
||||
@@ -1,89 +0,0 @@
|
||||
# $Id: Makefile.tests 8481 2009-07-13 20:18:41Z abdelazer $
|
||||
#
|
||||
# This makefile does a "smoketest" of stylesheets for various
|
||||
# output formats in the DocBook XSL Stylesheets release package.
|
||||
# It doesn't actually check the output -- it's just useful for
|
||||
# confirming whether each XSLT transformation actually executes
|
||||
# successfully without any errors.
|
||||
#
|
||||
# To use it, run "make check" or just "make"
|
||||
|
||||
XSLTPROC=xsltproc
|
||||
XSLTPROC_FLAGS=
|
||||
|
||||
TESTFILE=tests/refentry.007.xml
|
||||
TESTFILE_NS=tests/refentry.007.ns.xml
|
||||
|
||||
NORMAL_STYLES=fo/docbook.xsl html/docbook.xsl xhtml/docbook.xsl
|
||||
NORMAL_PROFILE_STYLES=fo/profile-docbook.xsl html/profile-docbook.xsl xhtml/profile-docbook.xsl
|
||||
CHUNK_STYLES=html/chunk.xsl html/onechunk.xsl xhtml/chunk.xsl xhtml/onechunk.xsl
|
||||
HELP_STYLES=htmlhelp/htmlhelp.xsl javahelp/javahelp.xsl eclipse/eclipse.xsl
|
||||
MULTIFILE_STYLES=$(CHUNK_STYLES) $(HELP_STYLES)
|
||||
CHUNK_PROFILE_STYLES=html/profile-chunk.xsl html/profile-onechunk.xsl xhtml/profile-chunk.xsl xhtml/profile-onechunk.xsl
|
||||
HELP_PROFILE_STYLES=htmlhelp/profile-htmlhelp.xsl eclipse/profile-eclipse.xsl javahelp/profile-javahelp.xsl
|
||||
MULTIFILE_PROFILE_STYLES=$(CHUNK_PROFILE_STYLES) $(HELP_PROFILE_STYLES)
|
||||
|
||||
MAN_STYLE=manpages/docbook.xsl
|
||||
MAN_PROFILE_STYLE=manpages/profile-docbook.xsl
|
||||
|
||||
TWO_PROFILE_STYLE=profiling/profile.xsl
|
||||
|
||||
ROUNDTRIP_STYLES=roundtrip/dbk2ooo.xsl roundtrip/dbk2pages.xsl roundtrip/dbk2wordml.xsl
|
||||
SLIDES_STYLES=slides/html/default.xsl slides/xhtml/default.xsl slides/fo/plain.xsl
|
||||
WEBSITE_STYLES=website/website.xsl
|
||||
WEBSITE_CHUNK_STYLES=website/chunk-website.xsl
|
||||
|
||||
# chunked output gets written to TMP_OUTPUT_DIR
|
||||
TMP_OUTPUT_DIR=/tmp/smoketest-output/
|
||||
# if you don't want TMP_OUTPUT_DIR and its contents deleted, unset
|
||||
# SMOKETEST_CLEAN_TARGET; e.g. "make check SMOKETEST_CLEAN_TARGET=''"
|
||||
SMOKETEST_CLEAN_TARGET=smoketest-clean
|
||||
|
||||
check: smoketest-make-tmp-dir smoketest-normal smoketest-normal-profile smoketest-chunk smoketest-chunk-profile smoketest-man smoketest-man-profile smoketest-two-profile $(SMOKETEST_CLEAN_TARGET)
|
||||
|
||||
smoketest-make-tmp-dir:
|
||||
$(RM) -r $(TMP_OUTPUT_DIR)
|
||||
mkdir '$(TMP_OUTPUT_DIR)'
|
||||
|
||||
smoketest-normal:
|
||||
for stylesheet in $(NORMAL_STYLES); do \
|
||||
echo "$(XSLT) $(TESTFILE) $$stylesheet > /dev/null"; \
|
||||
$(XSLT) $(TESTFILE) $$stylesheet > /dev/null; \
|
||||
echo "$(XSLT) $(TESTFILE_NS) $$stylesheet > /dev/null"; \
|
||||
$(XSLT) $(TESTFILE_NS) $$stylesheet > /dev/null; \
|
||||
done
|
||||
|
||||
smoketest-normal-profile:
|
||||
for stylesheet in $(NORMAL_PROFILE_STYLES); do \
|
||||
echo "$(XSLT) $(TESTFILE) $$stylesheet > /dev/null"; \
|
||||
$(XSLT) $(TESTFILE) $$stylesheet > /dev/null; \
|
||||
echo "$(XSLT) $(TESTFILE_NS) $$stylesheet > /dev/null"; \
|
||||
$(XSLT) $(TESTFILE_NS) $$stylesheet > /dev/null; \
|
||||
done
|
||||
|
||||
smoketest-chunk:
|
||||
for stylesheet in $(MULTIFILE_STYLES) ; do \
|
||||
$(XSLT) $(TESTFILE) $$stylesheet manifest.in.base.dir=1 base.dir=$(TMP_OUTPUT_DIR) ; \
|
||||
$(XSLT) $(TESTFILE_NS) $$stylesheet manifest.in.base.dir=1 base.dir=$(TMP_OUTPUT_DIR) ; \
|
||||
done;
|
||||
|
||||
smoketest-chunk-profile:
|
||||
for stylesheet in $(MULTIFILE_PROFILE_STYLES) ; do \
|
||||
$(XSLT) $(TESTFILE) $$stylesheet manifest.in.base.dir=1 base.dir=$(TMP_OUTPUT_DIR) ; \
|
||||
$(XSLT) $(TESTFILE_NS) $$stylesheet manifest.in.base.dir=1 base.dir=$(TMP_OUTPUT_DIR) ; \
|
||||
done;
|
||||
|
||||
smoketest-man:
|
||||
$(XSLT) $(TESTFILE) $(MAN_STYLE) man.output.in.separate.dir=1 man.output.base.dir=$(TMP_OUTPUT_DIR) ; \
|
||||
$(XSLT) $(TESTFILE_NS) $(MAN_STYLE) man.output.in.separate.dir=1 man.output.base.dir=$(TMP_OUTPUT_DIR) ;
|
||||
|
||||
smoketest-man-profile:
|
||||
$(XSLT) $(TESTFILE) $(MAN_PROFILE_STYLE) man.output.in.separate.dir=1 man.output.base.dir=$(TMP_OUTPUT_DIR) ; \
|
||||
$(XSLT) $(TESTFILE_NS) $(MAN_PROFILE_STYLE) man.output.in.separate.dir=1 man.output.base.dir=$(TMP_OUTPUT_DIR) ;
|
||||
|
||||
smoketest-two-profile:
|
||||
$(XSLT) $(TESTFILE_NS) $(TWO_PROFILE_STYLE) > /dev/null ;
|
||||
|
||||
smoketest-clean:
|
||||
$(RM) -r $(TMP_OUTPUT_DIR)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user