This commit is contained in:
Chirag Dewan 2019-08-07 21:25:57 +05:30
commit e56c5f8f1c
228 changed files with 1583 additions and 1981 deletions

View File

@ -1,4 +1,4 @@
## Relevant articles:
## Relevant Articles:
- [Java Two Pointer Technique](https://www.baeldung.com/java-two-pointer-technique)
- [Implementing Simple State Machines with Java Enums](https://www.baeldung.com/java-enum-simple-state-machine)

View File

@ -0,0 +1,45 @@
package com.baeldung.metaprogramming
import groovy.transform.AutoClone
import groovy.transform.Canonical
import groovy.transform.EqualsAndHashCode
import groovy.transform.ToString
import groovy.transform.TupleConstructor
import groovy.util.logging.*
@Canonical
@TupleConstructor
@EqualsAndHashCode
@ToString(includePackage=false, excludes=['id'])
@Log
@AutoClone
class Employee {
long id
String firstName
String lastName
int age
//method to catch missing property's getter
def propertyMissing(String propertyName) {
log.info "$propertyName is not available"
"property '$propertyName' is not available"
}
//method to catch missing property's setter
def propertyMissing(String propertyName, propertyValue) {
println "property '$propertyName' is not available"
log.info "$propertyName is not available"
"property '$propertyName' is not available"
}
def methodMissing(String methodName, def methodArgs) {
log.info "$methodName is not defined"
"method '$methodName' is not defined"
}
def logEmp() {
log.info "Employee: $lastName, $firstName is of $age years age"
}
}

View File

@ -0,0 +1,30 @@
package com.baeldung.metaprogramming.extension
import com.baeldung.metaprogramming.Employee
class BasicExtensions {
static int getYearOfBirth(Employee self) {
return (new Date().getYear() + 1900) - self.age;
}
static String capitalize(String self) {
return self.substring(0, 1).toUpperCase() + self.substring(1)
}
static void printCounter(Integer self) {
while (self>0) {
println self
self--
}
}
static Long square(Long self) {
return self*self
}
static BigDecimal cube(BigDecimal self) {
return self*self*self
}
}

View File

@ -0,0 +1,10 @@
package com.baeldung.metaprogramming.extension
import com.baeldung.metaprogramming.Employee
class StaticEmployeeExtension {
static Employee getDefaultObj(Employee self) {
return new Employee(firstName: "firstName", lastName: "lastName", age: 20)
}
}

View File

@ -0,0 +1,4 @@
moduleName=core-groovy-2
moduleVersion=1.0-SNAPSHOT
extensionClasses=com.baeldung.metaprogramming.extension.BasicExtensions
staticExtensionClasses=com.baeldung.metaprogramming.extension.StaticEmployeeExtension

View File

@ -0,0 +1,118 @@
package com.baeldung.metaprogramming
import groovy.time.TimeCategory
class MetaprogrammingUnitTest extends GroovyTestCase {
Employee emp = new Employee(firstName: "Norman", lastName: "Lewis")
void testPropertyMissing() {
assert emp.address == "property 'address' is not available"
}
void testMethodMissing() {
Employee emp = new Employee()
try {
emp.getFullName()
} catch(MissingMethodException e) {
println "method is not defined"
}
assert emp.getFullName() == "method 'getFullName' is not defined"
}
void testMetaClassProperty() {
Employee.metaClass.address = ""
emp = new Employee(firstName: "Norman", lastName: "Lewis", address: "US")
assert emp.address == "US"
}
void testMetaClassMethod() {
emp.metaClass.getFullName = {
"$lastName, $firstName"
}
assert emp.getFullName() == "Lewis, Norman"
}
void testMetaClassConstructor() {
try {
Employee emp = new Employee("Norman")
} catch(GroovyRuntimeException e) {
assert e.message == "Could not find matching constructor for: com.baeldung.metaprogramming.Employee(String)"
}
Employee.metaClass.constructor = { String firstName ->
new Employee(firstName: firstName)
}
Employee norman = new Employee("Norman")
assert norman.firstName == "Norman"
assert norman.lastName == null
}
void testJavaMetaClass() {
String.metaClass.capitalize = { String str ->
str.substring(0, 1).toUpperCase() + str.substring(1);
}
assert "norman".capitalize() == "Norman"
}
void testEmployeeExtension() {
Employee emp = new Employee(age: 28)
assert emp.getYearOfBirth() == 1991
}
void testJavaClassesExtensions() {
5.printCounter()
assert 40l.square() == 1600l
assert (2.98).cube() == 26.463592
}
void testStaticEmployeeExtension() {
assert Employee.getDefaultObj().firstName == "firstName"
assert Employee.getDefaultObj().lastName == "lastName"
assert Employee.getDefaultObj().age == 20
}
void testToStringAnnotation() {
Employee employee = new Employee()
employee.id = 1
employee.firstName = "norman"
employee.lastName = "lewis"
employee.age = 28
assert employee.toString() == "Employee(norman, lewis, 28)"
}
void testTupleConstructorAnnotation() {
Employee norman = new Employee(1, "norman", "lewis", 28)
assert norman.toString() == "Employee(norman, lewis, 28)"
Employee snape = new Employee(2, "snape")
assert snape.toString() == "Employee(snape, null, 0)"
}
void testEqualsAndHashCodeAnnotation() {
Employee norman = new Employee(1, "norman", "lewis", 28)
Employee normanCopy = new Employee(1, "norman", "lewis", 28)
assert norman.equals(normanCopy)
assert norman.hashCode() == normanCopy.hashCode()
}
void testAutoCloneAnnotation() {
try {
Employee norman = new Employee(1, "norman", "lewis", 28)
def normanCopy = norman.clone()
assert norman == normanCopy
} catch(CloneNotSupportedException e) {
e.printStackTrace()
}
}
void testLoggingAnnotation() {
Employee employee = new Employee(1, "Norman", "Lewis", 28)
employee.logEmp()
}
}

View File

@ -7,3 +7,4 @@
- [How to Delay Code Execution in Java](https://www.baeldung.com/java-delay-code-execution)
- [Run JAR Application With Command Line Arguments](https://www.baeldung.com/java-run-jar-with-arguments)
- [Java 8 Stream skip() vs limit()](https://www.baeldung.com/java-stream-skip-vs-limit)
- [Guide to Java BiFunction Interface](https://www.baeldung.com/java-bifunction-interface)

View File

@ -7,7 +7,7 @@
- [Check if a Java Array Contains a Value](http://www.baeldung.com/java-array-contains-value)
- [Initializing Arrays in Java](http://www.baeldung.com/java-initialize-array)
- [Guide to the java.util.Arrays Class](http://www.baeldung.com/java-util-arrays)
- [Jagged Arrays In Java](http://www.baeldung.com/java-jagged-arrays)
- [Multi-Dimensional Arrays In Java](http://www.baeldung.com/java-jagged-arrays)
- [Find Sum and Average in a Java Array](http://www.baeldung.com/java-array-sum-average)
- [Arrays in Java: A Reference Guide](https://www.baeldung.com/java-arrays-guide)
- [How to Invert an Array in Java](http://www.baeldung.com/java-invert-array)
@ -16,3 +16,4 @@
- [Sorting Arrays in Java](https://www.baeldung.com/java-sorting-arrays)
- [Convert a Float to a Byte Array in Java](https://www.baeldung.com/java-convert-float-to-byte-array)
- [Converting Between Stream and Array in Java](https://www.baeldung.com/java-stream-to-array)
- [Removing an Element from an Array in Java](https://www.baeldung.com/java-array-remove-element)

View File

@ -0,0 +1,3 @@
### Relevant Articles
- [Design Strategies for Decoupling Java Modules](https://www.baeldung.com/java-modules-decoupling-design-strategies)

View File

@ -0,0 +1,43 @@
<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>
<groupId>com.baeldung</groupId>
<artifactId>core-java-lang-operators</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-lang-operators</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<relativePath>../../parent-java</relativePath>
</parent>
<dependencies>
<!-- test scoped -->
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>core-java-lang-operators</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
<properties>
<!-- testing -->
<assertj-core.version>3.10.0</assertj-core.version>
</properties>
</project>

View File

@ -1,6 +1,7 @@
package com.baeldung.incrementdecrementunaryoperator;
package com.baeldung.incrementdecrementunaryoperators;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.Test;
public class IncrementDecrementUnaryOperatorUnitTest {

View File

@ -18,3 +18,4 @@
- [The Modulo Operator in Java](https://www.baeldung.com/modulo-java)
- [Ternary Operator In Java](https://www.baeldung.com/java-ternary-operator)
- [Java instanceof Operator](https://www.baeldung.com/java-instanceof)
- [Breaking Out of Nested Loops](https://www.baeldung.com/java-breaking-out-nested-loop)

View File

@ -1,3 +1,3 @@
## Relevant articles:
## Relevant Articles:
- [Determine File Creating Date in Java](https://www.baeldung.com/java-file-creation-date)
- [Determine File Creation Date in Java](https://www.baeldung.com/java-file-creation-date)

View File

@ -10,3 +10,4 @@
- [SSL Handshake Failures](https://www.baeldung.com/java-ssl-handshake-failures)
- [SHA-256 and SHA3-256 Hashing in Java](https://www.baeldung.com/sha-256-hashing-java)
- [Enabling TLS v1.2 in Java 7](https://www.baeldung.com/java-7-tls-v12)
- [The Java SecureRandom Class](https://www.baeldung.com/java-secure-random)

View File

@ -17,6 +17,7 @@
<module>pre-jpms</module>
<module>core-java-exceptions</module>
<module>core-java-optional</module>
<module>core-java-lang-operators</module>
<module>core-java-networking-2</module>
</modules>

View File

@ -1,4 +1,5 @@
## Relevant articles:
[The Trie Data Structure in Java](https://www.baeldung.com/trie-java)
[Implementing a Binary Tree in Java](https://www.baeldung.com/java-binary-tree)
- [The Trie Data Structure in Java](https://www.baeldung.com/trie-java)
- [Implementing a Binary Tree in Java](https://www.baeldung.com/java-binary-tree)
- [Depth First Search in Java](https://www.baeldung.com/java-depth-first-search)

View File

@ -1,19 +1,15 @@
<code_scheme name="baeldung">
<code_scheme name="baeldung-formatter" version="173">
<option name="RIGHT_MARGIN" value="260" />
<option name="ENABLE_JAVADOC_FORMATTING" value="false" />
<option name="FORMATTER_TAGS_ENABLED" value="true" />
<JavaCodeStyleSettings>
<option name="SPACE_AFTER_CLOSING_ANGLE_BRACKET_IN_TYPE_ARGUMENT" value="true" />
<option name="DO_NOT_WRAP_AFTER_SINGLE_ANNOTATION" value="true" />
<option name="ALIGN_MULTILINE_ANNOTATION_PARAMETERS" value="true" />
<option name="CLASS_COUNT_TO_USE_IMPORT_ON_DEMAND" value="500" />
<option name="NAMES_COUNT_TO_USE_IMPORT_ON_DEMAND" value="300" />
<option name="ENABLE_JAVADOC_FORMATTING" value="false" />
</JavaCodeStyleSettings>
<JetCodeStyleSettings>
<option name="NAME_COUNT_TO_USE_STAR_IMPORT" value="99" />
<option name="NAME_COUNT_TO_USE_STAR_IMPORT_FOR_MEMBERS" value="99" />
</JetCodeStyleSettings>
<codeStyleSettings language="JAVA">
<option name="KEEP_LINE_BREAKS" value="false" />
<option name="KEEP_FIRST_COLUMN_COMMENT" value="false" />
<option name="KEEP_CONTROL_STATEMENT_IN_ONE_LINE" value="false" />
<option name="KEEP_BLANK_LINES_IN_DECLARATIONS" value="1" />
<option name="KEEP_BLANK_LINES_IN_CODE" value="1" />
<option name="KEEP_BLANK_LINES_BEFORE_RBRACE" value="1" />
@ -29,19 +25,18 @@
<option name="THROWS_LIST_WRAP" value="1" />
<option name="EXTENDS_KEYWORD_WRAP" value="1" />
<option name="THROWS_KEYWORD_WRAP" value="1" />
<option name="METHOD_CALL_CHAIN_WRAP" value="2" />
<option name="WRAP_FIRST_METHOD_IN_CALL_CHAIN" value="true" />
<option name="METHOD_CALL_CHAIN_WRAP" value="5" />
<option name="BINARY_OPERATION_WRAP" value="1" />
<option name="BINARY_OPERATION_SIGN_ON_NEXT_LINE" value="true" />
<option name="TERNARY_OPERATION_WRAP" value="5" />
<option name="TERNARY_OPERATION_SIGNS_ON_NEXT_LINE" value="true" />
<option name="ARRAY_INITIALIZER_WRAP" value="1" />
<option name="ARRAY_INITIALIZER_LBRACE_ON_NEXT_LINE" value="true" />
<option name="ARRAY_INITIALIZER_RBRACE_ON_NEXT_LINE" value="true" />
<option name="PLACE_ASSIGNMENT_SIGN_ON_NEXT_LINE" value="true" />
<option name="METHOD_ANNOTATION_WRAP" value="0" />
<option name="CLASS_ANNOTATION_WRAP" value="0" />
<option name="FIELD_ANNOTATION_WRAP" value="0" />
<indentOptions>
<option name="CONTINUATION_INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="8" />
<option name="INDENT_SIZE" value="8" />
<option name="CONTINUATION_INDENT_SIZE" value="4" />
<option name="SMART_TABS" value="true" />
</indentOptions>
</codeStyleSettings>
</code_scheme>

View File

@ -2,7 +2,7 @@
## Jackson Cookbooks and Examples
###The Course
### The Course
The "REST With Spring" Classes: http://bit.ly/restwithspring
### Relevant Articles:
@ -10,3 +10,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
- [How to Process YAML with Jackson](https://www.baeldung.com/jackson-yaml)
- [Working with Tree Model Nodes in Jackson](https://www.baeldung.com/jackson-json-node-tree-model)
- [Converting JSON to CSV in Java](https://www.baeldung.com/java-converting-json-to-csv)
- [Compare Two JSON Objects with Jackson](https://www.baeldung.com/jackson-compare-two-json-objects)

View File

@ -1,3 +1,4 @@
## Relevant Articles:
- [Converting Between LocalDate and XMLGregorianCalendar](https://www.baeldung.com/java-localdate-to-xmlgregoriancalendar)
- [Convert Time to Milliseconds in Java](https://www.baeldung.com/java-time-milliseconds)
- [Check If a String Is a Valid Date in Java](https://www.baeldung.com/java-string-valid-date)

View File

@ -29,3 +29,4 @@
- [A Guide to SimpleDateFormat](https://www.baeldung.com/java-simple-date-format)
- [ZoneOffset in Java](https://www.baeldung.com/java-zone-offset)
- [Differences Between ZonedDateTime and OffsetDateTime](https://www.baeldung.com/java-zoneddatetime-offsetdatetime)
- [Introduction to Joda-Time](http://www.baeldung.com/joda-time)

View File

@ -17,6 +17,26 @@
<artifactId>commons-math3</artifactId>
<version>${commons-math3.version}</version>
</dependency>
<dependency>
<groupId>org.ejml</groupId>
<artifactId>ejml-all</artifactId>
<version>${ejml.version}</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native</artifactId>
<version>${nd4j.version}</version>
</dependency>
<dependency>
<groupId>org.la4j</groupId>
<artifactId>la4j</artifactId>
<version>${la4j.version}</version>
</dependency>
<dependency>
<groupId>colt</groupId>
<artifactId>colt</artifactId>
<version>${colt.version}</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
@ -44,6 +64,17 @@
<artifactId>combinatoricslib3</artifactId>
<version>${combinatoricslib3.version}</version>
</dependency>
<!-- Benchmarking -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</dependency>
</dependencies>
<build>
@ -64,6 +95,11 @@
<commons-codec.version>1.11</commons-codec.version>
<guava.version>27.0.1-jre</guava.version>
<combinatoricslib3.version>3.3.0</combinatoricslib3.version>
<ejml.version>0.38</ejml.version>
<nd4j.version>1.0.0-beta4</nd4j.version>
<colt.version>1.2.0</colt.version>
<la4j.version>0.6.0</la4j.version>
<jmh.version>1.19</jmh.version>
</properties>
</project>

View File

@ -16,18 +16,16 @@ public class BinaryNumbers {
}
StringBuilder binaryNumber = new StringBuilder();
Integer quotient = decimalNumber;
while (decimalNumber > 0) {
int remainder = decimalNumber % 2;
int result = decimalNumber / 2;
while (quotient > 0) {
int remainder = quotient % 2;
binaryNumber.append(remainder);
decimalNumber = result;
quotient /= 2;
}
binaryNumber = binaryNumber.reverse();
return Integer.valueOf(binaryNumber.toString());
}
@ -40,7 +38,7 @@ public class BinaryNumbers {
*/
public Integer convertBinaryToDecimal(Integer binaryNumber) {
Integer result = 0;
Integer decimalNumber = 0;
Integer base = 1;
while (binaryNumber > 0) {
@ -48,11 +46,10 @@ public class BinaryNumbers {
int lastDigit = binaryNumber % 10;
binaryNumber = binaryNumber / 10;
result += lastDigit * base;
decimalNumber += lastDigit * base;
base = base * 2;
}
return result;
return decimalNumber;
}
/**
@ -99,9 +96,7 @@ public class BinaryNumbers {
public Integer substractBinaryNumber(Integer firstNum, Integer secondNum) {
int onesComplement = Integer.valueOf(getOnesComplement(secondNum));
StringBuilder output = new StringBuilder();
int carry = 0;
int temp;
@ -124,13 +119,11 @@ public class BinaryNumbers {
} else {
return getOnesComplement(Integer.valueOf(additionOfFirstNumAndOnesComplement));
}
}
public Integer getOnesComplement(Integer num) {
StringBuilder onesComplement = new StringBuilder();
while (num > 0) {
int lastDigit = num % 10;
if (lastDigit == 0) {
@ -140,7 +133,6 @@ public class BinaryNumbers {
}
num = num / 10;
}
return Integer.valueOf(onesComplement.reverse()
.toString());
}

View File

@ -1,4 +1,4 @@
## Relevant Articles
## Relevant Articles:
- [Java Localization Formatting Messages](https://www.baeldung.com/java-localization-messages-formatting)
- [Check If a String Contains a Substring](https://www.baeldung.com/java-string-contains-substring)
@ -18,5 +18,8 @@
- [Java String Interview Questions and Answers](https://www.baeldung.com/java-string-interview-questions)
- [Check if a String is a Pangram in Java](https://www.baeldung.com/java-string-pangram)
- [Check If a String Contains Multiple Keywords](https://www.baeldung.com/string-contains-multiple-words)
- [Blank and Empty Strings in Java](https://www.baeldung.com/java-blank-empty-strings)
- [Checking for Empty or Blank Strings in Java](https://www.baeldung.com/java-blank-empty-strings)
- [String Initialization in Java](https://www.baeldung.com/java-string-initialization)
- [Java Multi-line String](https://www.baeldung.com/java-multiline-string)
- [Checking If a String Is a Repeated Substring](https://www.baeldung.com/java-repeated-substring)
- [How to Reverse a String in Java](https://www.baeldung.com/java-reverse-string)

View File

@ -1,5 +1,4 @@
### Relevant Articles:
## Relevant Articles:
- [A Guide to jBPM with Java](https://www.baeldung.com/jbpm-java)
- [Guide to Classgraph Library](https://www.baeldung.com/classgraph)
@ -8,3 +7,7 @@
- [Templating with Handlebars](https://www.baeldung.com/handlebars)
- [A Guide to Crawler4j](https://www.baeldung.com/crawler4j)
- [Decode an OkHttp JSON Response](https://www.baeldung.com/okhttp-json-response)
- [Key Value Store with Chronicle Map](https://www.baeldung.com/java-chronicle-map)
- [Matrix Multiplication in Java](https://www.baeldung.com/java-matrix-multiplication)
- [Guide to MapDB](https://www.baeldung.com/mapdb)
- [A Guide to Apache Mesos](https://www.baeldung.com/apache-mesos)

View File

@ -59,26 +59,6 @@
<artifactId>picocli</artifactId>
<version>${picocli.version}</version>
</dependency>
<dependency>
<groupId>org.ejml</groupId>
<artifactId>ejml-all</artifactId>
<version>${ejml.version}</version>
</dependency>
<dependency>
<groupId>org.nd4j</groupId>
<artifactId>nd4j-native</artifactId>
<version>${nd4j.version}</version>
</dependency>
<dependency>
<groupId>org.la4j</groupId>
<artifactId>la4j</artifactId>
<version>${la4j.version}</version>
</dependency>
<dependency>
<groupId>colt</groupId>
<artifactId>colt</artifactId>
<version>${colt.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
@ -127,17 +107,6 @@
<artifactId>handlebars</artifactId>
<version>${handlebars.version}</version>
</dependency>
<!-- Benchmarking -->
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.apache.mesos</groupId>
<artifactId>mesos</artifactId>
@ -160,11 +129,6 @@
<chronicle.map.version>3.17.2</chronicle.map.version>
<crawler4j.version>4.4.0</crawler4j.version>
<spring-boot-starter.version>2.1.4.RELEASE</spring-boot-starter.version>
<ejml.version>0.38</ejml.version>
<nd4j.version>1.0.0-beta4</nd4j.version>
<colt.version>1.2.0</colt.version>
<la4j.version>0.6.0</la4j.version>
<jmh.version>1.19</jmh.version>
<mesos.library.version>0.28.3</mesos.library.version>
<parallel-collectors.version>1.1.0</parallel-collectors.version>
<okhttp.version>3.14.2</okhttp.version>

View File

@ -0,0 +1,11 @@
### Relevant articles
- [Introduction to Apache Flink with Java](http://www.baeldung.com/apache-flink)
- [Guide to the HyperLogLog Algorithm](http://www.baeldung.com/java-hyperloglog)
- [Introduction to Conflict-Free Replicated Data Types](http://www.baeldung.com/java-conflict-free-replicated-data-types)
- [Introduction to javax.measure](http://www.baeldung.com/javax-measure)
- [Introduction To Docx4J](http://www.baeldung.com/docx4j)
- [Interact with Google Sheets from Java](http://www.baeldung.com/google-sheets-java-client)
- [Introduction To OpenCSV](http://www.baeldung.com/opencsv)
- [Introduction to Smooks](http://www.baeldung.com/smooks)
- [A Guide to Infinispan in Java](http://www.baeldung.com/infinispan)

View File

@ -0,0 +1 @@
log4j.rootLogger=INFO, stdout

145
libraries-data-2/pom.xml Normal file
View File

@ -0,0 +1,145 @@
<?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>libraries-data-2</artifactId>
<name>libraries-data-2</name>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-connector-kafka-0.11_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-streaming-java_2.11</artifactId>
<version>${flink.version}</version>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-core</artifactId>
<version>${flink.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-java</artifactId>
<version>${flink.version}</version>
<exclusions>
<exclusion>
<artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.flink</groupId>
<artifactId>flink-test-utils_2.11</artifactId>
<version>${flink.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>net.agkn</groupId>
<artifactId>hll</artifactId>
<version>${hll.version}</version>
</dependency>
<dependency>
<groupId>com.netopyr.wurmloch</groupId>
<artifactId>wurmloch-crdt</artifactId>
<version>${crdt.version}</version>
</dependency>
<dependency>
<groupId>tec.units</groupId>
<artifactId>unit-ri</artifactId>
<version>${unit-ri.version}</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb-api.version}</version>
</dependency>
<dependency>
<groupId>org.docx4j</groupId>
<artifactId>docx4j</artifactId>
<version>${docx4j.version}</version>
</dependency>
<!-- google api -->
<dependency>
<groupId>com.google.api-client</groupId>
<artifactId>google-api-client</artifactId>
<version>${google-api.version}</version>
</dependency>
<dependency>
<groupId>com.google.oauth-client</groupId>
<artifactId>google-oauth-client-jetty</artifactId>
<version>${google-api.version}</version>
</dependency>
<dependency>
<groupId>com.google.apis</groupId>
<artifactId>google-api-services-sheets</artifactId>
<version>${google-sheets.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.opencsv/opencsv -->
<dependency>
<groupId>com.opencsv</groupId>
<artifactId>opencsv</artifactId>
<version>${opencsv.version}</version>
</dependency>
<dependency>
<groupId>org.milyn</groupId>
<artifactId>milyn-smooks-all</artifactId>
<version>${smooks.version}</version>
</dependency>
<dependency>
<groupId>org.infinispan</groupId>
<artifactId>infinispan-core</artifactId>
<version>${infinispan.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
<properties>
<flink.version>1.5.0</flink.version>
<hll.version>1.6.0</hll.version>
<crdt.version>0.1.0</crdt.version>
<unit-ri.version>1.0.3</unit-ri.version>
<docx4j.version>3.3.5</docx4j.version>
<jaxb-api.version>2.1</jaxb-api.version>
<google-api.version>1.23.0</google-api.version>
<opencsv.version>4.1</opencsv.version>
<smooks.version>1.7.0</smooks.version>
<infinispan.version>9.1.5.Final</infinispan.version>
<google-sheets.version>v4-rev493-1.21.0</google-sheets.version>
<jackson.version>2.9.8</jackson.version>
<spring.version>4.3.8.RELEASE</spring.version>
</properties>
</project>

View File

@ -0,0 +1,105 @@
package com.baeldung.docx;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.jaxb.Context;
import org.docx4j.model.table.TblFactory;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPartAbstractImage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.BooleanDefaultTrue;
import org.docx4j.wml.Color;
import org.docx4j.wml.Drawing;
import org.docx4j.wml.ObjectFactory;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.RPr;
import org.docx4j.wml.Tbl;
import org.docx4j.wml.Tc;
import org.docx4j.wml.Text;
import org.docx4j.wml.Tr;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
class Docx4jExample {
void createDocumentPackage(String outputPath, String imagePath) throws Exception {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
mainDocumentPart.addParagraphOfText("Welcome To Baeldung!");
ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
R r = factory.createR();
Text t = factory.createText();
t.setValue("Welcome To Baeldung");
r.getContent().add(t);
p.getContent().add(r);
RPr rpr = factory.createRPr();
BooleanDefaultTrue b = new BooleanDefaultTrue();
rpr.setB(b);
rpr.setI(b);
rpr.setCaps(b);
Color red = factory.createColor();
red.setVal("green");
rpr.setColor(red);
r.setRPr(rpr);
mainDocumentPart.getContent().add(p);
File image = new File(imagePath);
byte[] fileContent = Files.readAllBytes(image.toPath());
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, fileContent);
Inline inline = imagePart.createImageInline("Baeldung Image", "Alt Text", 1, 2, false);
P Imageparagraph = addImageToParagraph(inline);
mainDocumentPart.getContent().add(Imageparagraph);
int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions().getWritableWidthTwips();
int columnNumber = 3;
Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber);
List<Object> rows = tbl.getContent();
for (Object row : rows) {
Tr tr = (Tr) row;
List<Object> cells = tr.getContent();
for (Object cell : cells) {
Tc td = (Tc) cell;
td.getContent().add(p);
}
}
mainDocumentPart.getContent().add(tbl);
File exportFile = new File(outputPath);
wordPackage.save(exportFile);
}
boolean isTextExist(String testText) throws Docx4JException, JAXBException {
File doc = new File("helloWorld.docx");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();
String textNodesXPath = "//w:t";
List<Object> paragraphs = mainDocumentPart.getJAXBNodesViaXPath(textNodesXPath, true);
for (Object obj : paragraphs) {
Text text = (Text) ((JAXBElement) obj).getValue();
String textValue = text.getValue();
if (textValue != null && textValue.contains(testText)) {
return true;
}
}
return false;
}
private static P addImageToParagraph(Inline inline) {
ObjectFactory factory = new ObjectFactory();
P p = factory.createP();
R r = factory.createR();
p.getContent().add(r);
Drawing drawing = factory.createDrawing();
r.getContent().add(drawing);
drawing.getAnchorOrInline().add(inline);
return p;
}
}

Some files were not shown because too many files have changed in this diff Show More