From 22fc85d3f3eeee18024c3ed4b46047f52e92d811 Mon Sep 17 00:00:00 2001 From: Java Online Projects Date: Tue, 17 Oct 2017 11:50:59 +0300 Subject: [PATCH 01/20] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 127b1304a..a6de25e17 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ - используя асинхронные сервлеты 3.0 - сохранение данных в PostgreSQL используя [jDBI](http://jdbi.org/) - миграция базы [LiquiBase](http://www.liquibase.org/) -- использование в проекте [Guava](https://github.com/google/guava/wiki), [Thymleaf](http://www.thymeleaf.org/), [Lombook](https://projectlombok.org/), [StreamEx](https://github.com/amaembo/streamex), +- использование в проекте [Guava](https://github.com/google/guava/wiki), [Thymleaf](http://www.thymeleaf.org/), [Lombok](https://projectlombok.org/), [StreamEx](https://github.com/amaembo/streamex), [Typesafe Config](https://github.com/typesafehub/config), [Java Microbenchmark JMH](http://openjdk.java.net/projects/code-tools/jmh) ### Требование к участникам @@ -134,14 +134,14 @@ MatrixBenchmark.concurrentMultiply3 1000 ss 100 186,827 ± 11,882 - Maven. Поиск и разрешение конфликтов зависимостей - Подключаем логирование с общими настройкам - Библиотеки и фреймворки для работы с JDBC. -- Модуль persist +- Модуль persistence ## Занятие 5 - Разбор ДЗ - Сохранение в базу в batch-моде с обработкой конфликтов - Вставка в несколько потоков - Конфигурирование приложения (Typesafe config) -- Lombook +- Lombok ## Занятие 6 - Разбор ДЗ (доработка модели и модуля export) From 66dbb57f307e8ac7f01f61af948a3a315a40b4f5 Mon Sep 17 00:00:00 2001 From: Java Online Projects Date: Mon, 23 Oct 2017 05:31:40 +0300 Subject: [PATCH 02/20] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index a6de25e17..2d26e390c 100644 --- a/README.md +++ b/README.md @@ -99,9 +99,9 @@ ----- ## ![error](https://cloud.githubusercontent.com/assets/13649199/13672935/ef09ec1e-e6e7-11e5-9f79-d1641c05cbe6.png) Подсказки по HW1 -- не делайте 1000 000 тасок, лучше их сделать крупнее -- у меня разница между 4 и 1000 тасками по времени незаметна, поэтому делайте просто и не делайте сложно -- наконец: можно не считать значение элемента результирующей матрицы C за раз, а накапливать (`concurrentMultiply3`). Мои результаты: +- 1: не делайте 1000 000 тасок, лучше их сделать крупнее +- 2: у меня разница между 4 и 1000 тасками по времени незаметна, поэтому делайте просто и не делайте сложно +- 3: наконец: можно не считать значение элемента результирующей матрицы C за раз, а накапливать (`concurrentMultiply3`). Тогда трансформация B не нужна. Мои результаты: ``` Benchmark (matrixSize) Mode Cnt Score Error Units MatrixBenchmark.singleThreadMultiplyOpt 1000 ss 100 837,867 ± 25,530 ms/op From 4b3a183635f38c7190971038b7a720395f808f41 Mon Sep 17 00:00:00 2001 From: Java Online Projects Date: Thu, 22 Feb 2018 18:07:56 +0300 Subject: [PATCH 03/20] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 2d26e390c..6157a56d3 100644 --- a/README.md +++ b/README.md @@ -77,6 +77,7 @@ #### Скачайте [1_1_MailService.patch](https://drive.google.com/open?id=0B9Ye2auQ_NsFTE5ZV3pzWElxTWM), положите его в проект, правой мышкой на нем сделайте Apply Patch ... ---------------------------- +- [Как сделать Java код проще и нагляднее](https://habrahabr.ru/company/wrike/blog/349652/) ### Ресурсы (основы) - Intuit, Потоки выполнения. Синхронизация From 6ab9291702e034fbf006767b8aad12cad50a40f3 Mon Sep 17 00:00:00 2001 From: AlexeyKorban Date: Wed, 19 Sep 2018 10:22:57 +0500 Subject: [PATCH 04/20] Patch 1 --- .../masterjava/service/MailService.java | 72 ++++++++++++++++++- 1 file changed, 69 insertions(+), 3 deletions(-) diff --git a/src/main/java/ru/javaops/masterjava/service/MailService.java b/src/main/java/ru/javaops/masterjava/service/MailService.java index 2b6aeb294..cef46e55e 100644 --- a/src/main/java/ru/javaops/masterjava/service/MailService.java +++ b/src/main/java/ru/javaops/masterjava/service/MailService.java @@ -1,8 +1,10 @@ package ru.javaops.masterjava.service; -import java.util.Collections; +import java.util.ArrayList; import java.util.List; import java.util.Set; +import java.util.concurrent.*; +import java.util.stream.Collectors; public class MailService { private static final String OK = "OK"; @@ -11,10 +13,74 @@ public class MailService { private static final String INTERRUPTED_BY_TIMEOUT = "+++ Interrupted by timeout"; private static final String INTERRUPTED_EXCEPTION = "+++ InterruptedException"; + private final ExecutorService mailExecutor = Executors.newFixedThreadPool(8); + public GroupResult sendToList(final String template, final Set emails) throws Exception { - return new GroupResult(0, Collections.emptyList(), null); - } + final CompletionService completionService = new ExecutorCompletionService<>(mailExecutor); + + List> futures = emails.stream() + .map(email -> completionService.submit(() -> sendToUser(template, email))) + .collect(Collectors.toList()); + return new Callable() { + private int success = 0; + private List failed = new ArrayList<>(); + + @Override + public GroupResult call() { + while (!futures.isEmpty()) { + try { + Future future = completionService.poll(10, TimeUnit.SECONDS); + if (future == null) { + return cancelWithFail(INTERRUPTED_BY_TIMEOUT); + } + futures.remove(future); + MailResult mailResult = future.get(); + if (mailResult.isOk()) { + success++; + } else { + failed.add(mailResult); + if (failed.size() >= 5) { + return cancelWithFail(INTERRUPTED_BY_FAULTS_NUMBER); + } + } + } catch (ExecutionException e) { + return cancelWithFail(e.getCause().toString()); + } catch (InterruptedException e) { + return cancelWithFail(INTERRUPTED_EXCEPTION); + } + } +/* + for (Future future : futures) { + MailResult mailResult; + try { + mailResult = future.get(10, TimeUnit.SECONDS); + } catch (InterruptedException e) { + return cancelWithFail(INTERRUPTED_EXCEPTION); + } catch (ExecutionException e) { + return cancelWithFail(e.getCause().toString()); + } catch (TimeoutException e) { + return cancelWithFail(INTERRUPTED_BY_TIMEOUT); + } + if (mailResult.isOk()) { + success++; + } else { + failed.add(mailResult); + if (failed.size() >= 5) { + return cancelWithFail(INTERRUPTED_BY_FAULTS_NUMBER); + } + } + } +*/ + return new GroupResult(success, failed, null); + } + + private GroupResult cancelWithFail(String cause) { + futures.forEach(f -> f.cancel(true)); + return new GroupResult(success, failed, cause); + } + }.call(); + } // dummy realization public MailResult sendToUser(String template, String email) throws Exception { From a5e7760d9c2e302455ee8251ef52c0a99b108f1a Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Wed, 19 Sep 2018 10:37:24 +0500 Subject: [PATCH 05/20] Comment --- src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java index 80a344ac2..9d637d35a 100644 --- a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java +++ b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java @@ -14,6 +14,7 @@ public class MatrixUtil { public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { final int matrixSize = matrixA.length; final int[][] matrixC = new int[matrixSize][matrixSize]; + //Comment return matrixC; } From ea0f2dd8163cbdb85d3862020da21a50d8a9c200 Mon Sep 17 00:00:00 2001 From: AlexeyKorban Date: Wed, 19 Sep 2018 10:39:50 +0500 Subject: [PATCH 06/20] Comment 2 --- src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java index 9d637d35a..8ca04adb1 100644 --- a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java +++ b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java @@ -15,6 +15,7 @@ public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, Execu final int matrixSize = matrixA.length; final int[][] matrixC = new int[matrixSize][matrixSize]; //Comment + //Comment 2 return matrixC; } From 957b1a44c6dc7d60ed7f99cebc98f79fc774554d Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Wed, 19 Sep 2018 16:56:00 +0500 Subject: [PATCH 07/20] 2_02_HW1_concurrentMultiply --- .../javaops/masterjava/matrix/MainMatrix.java | 2 +- .../javaops/masterjava/matrix/MatrixUtil.java | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java b/src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java index ec1c8a691..0695132ef 100644 --- a/src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java +++ b/src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java @@ -24,7 +24,7 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc while (count < 6) { System.out.println("Pass " + count); long start = System.currentTimeMillis(); - final int[][] matrixC = MatrixUtil.singleThreadMultiply(matrixA, matrixB); + final int[][] matrixC = MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB); double duration = (System.currentTimeMillis() - start) / 1000.; out("Single thread time, sec: %.3f", duration); singleThreadSum += duration; diff --git a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java index 8ca04adb1..7522d57e4 100644 --- a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java +++ b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java @@ -20,18 +20,24 @@ public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, Execu return matrixC; } - // TODO optimize by https://habrahabr.ru/post/114797/ - public static int[][] singleThreadMultiply(int[][] matrixA, int[][] matrixB) { + // Optimized by https://habrahabr.ru/post/114797/ + public static int[][] singleThreadMultiplyOpt(int[][] matrixA, int[][] matrixB) { final int matrixSize = matrixA.length; final int[][] matrixC = new int[matrixSize][matrixSize]; - for (int i = 0; i < matrixSize; i++) { - for (int j = 0; j < matrixSize; j++) { + for (int col = 0; col < matrixSize; col++) { + final int[] columnB = new int[matrixSize]; + for (int k = 0; k < matrixSize; k++) { + columnB[k] = matrixB[k][col]; + } + + for (int row = 0; row < matrixSize; row++) { int sum = 0; + final int[] rowA = matrixA[row]; for (int k = 0; k < matrixSize; k++) { - sum += matrixA[i][k] * matrixB[k][j]; + sum += rowA[k] * columnB[k]; } - matrixC[i][j] = sum; + matrixC[row][col] = sum; } } return matrixC; From b6bcbc5aaa1e4df7d17477391f842109722ac8a2 Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Wed, 19 Sep 2018 17:22:07 +0500 Subject: [PATCH 08/20] 2_02_HW1_concurrentMultiply --- 2_02_HW1_concurrentMultiply.patch | 182 ++++++++++++++++++ .../javaops/masterjava/matrix/MatrixUtil.java | 153 ++++++++++++++- 2 files changed, 330 insertions(+), 5 deletions(-) create mode 100644 2_02_HW1_concurrentMultiply.patch diff --git a/2_02_HW1_concurrentMultiply.patch b/2_02_HW1_concurrentMultiply.patch new file mode 100644 index 000000000..125962b60 --- /dev/null +++ b/2_02_HW1_concurrentMultiply.patch @@ -0,0 +1,182 @@ +Index: src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (date 1508266595000) ++++ src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (date 1508268723000) +@@ -1,8 +1,9 @@ + package ru.javaops.masterjava.matrix; + +-import java.util.Random; +-import java.util.concurrent.ExecutionException; +-import java.util.concurrent.ExecutorService; ++import java.util.*; ++import java.util.concurrent.*; ++import java.util.stream.Collectors; ++import java.util.stream.IntStream; + + /** + * gkislin +@@ -10,11 +11,153 @@ + */ + public class MatrixUtil { + +- // TODO implement parallel multiplication matrixA*matrixB + public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { + final int matrixSize = matrixA.length; + final int[][] matrixC = new int[matrixSize][matrixSize]; + ++ class ColumnMultipleResult { ++ private final int col; ++ private final int[] columnC; ++ ++ private ColumnMultipleResult(int col, int[] columnC) { ++ this.col = col; ++ this.columnC = columnC; ++ } ++ } ++ ++ final CompletionService completionService = new ExecutorCompletionService<>(executor); ++ ++ for (int j = 0; j < matrixSize; j++) { ++ final int col = j; ++ final int[] columnB = new int[matrixSize]; ++ for (int k = 0; k < matrixSize; k++) { ++ columnB[k] = matrixB[k][col]; ++ } ++ completionService.submit(() -> { ++ final int[] columnC = new int[matrixSize]; ++ ++ for (int row = 0; row < matrixSize; row++) { ++ final int[] rowA = matrixA[row]; ++ int sum = 0; ++ for (int k = 0; k < matrixSize; k++) { ++ sum += rowA[k] * columnB[k]; ++ } ++ columnC[row] = sum; ++ } ++ return new ColumnMultipleResult(col, columnC); ++ }); ++ } ++ ++ for (int i = 0; i < matrixSize; i++) { ++ ColumnMultipleResult res = completionService.take().get(); ++ for (int k = 0; k < matrixSize; k++) { ++ matrixC[k][res.col] = res.columnC[k]; ++ } ++ } ++ return matrixC; ++ } ++ ++ public static int[][] concurrentMultiplyCayman(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { ++ final int matrixSize = matrixA.length; ++ final int[][] matrixResult = new int[matrixSize][matrixSize]; ++ final int threadCount = Runtime.getRuntime().availableProcessors(); ++ final int maxIndex = matrixSize * matrixSize; ++ final int cellsInThread = maxIndex / threadCount; ++ final int[][] matrixBFinal = new int[matrixSize][matrixSize]; ++ ++ for (int i = 0; i < matrixSize; i++) { ++ for (int j = 0; j < matrixSize; j++) { ++ matrixBFinal[i][j] = matrixB[j][i]; ++ } ++ } ++ ++ Set> threads = new HashSet<>(); ++ int fromIndex = 0; ++ for (int i = 1; i <= threadCount; i++) { ++ final int toIndex = i == threadCount ? maxIndex : fromIndex + cellsInThread; ++ final int firstIndexFinal = fromIndex; ++ threads.add(() -> { ++ for (int j = firstIndexFinal; j < toIndex; j++) { ++ final int row = j / matrixSize; ++ final int col = j % matrixSize; ++ ++ int sum = 0; ++ for (int k = 0; k < matrixSize; k++) { ++ sum += matrixA[row][k] * matrixBFinal[col][k]; ++ } ++ matrixResult[row][col] = sum; ++ } ++ return true; ++ }); ++ fromIndex = toIndex; ++ } ++ executor.invokeAll(threads); ++ return matrixResult; ++ } ++ ++ public static int[][] concurrentMultiplyDarthVader(int[][] matrixA, int[][] matrixB, ExecutorService executor) ++ throws InterruptedException, ExecutionException { ++ ++ final int matrixSize = matrixA.length; ++ final int[][] matrixC = new int[matrixSize][matrixSize]; ++ ++ List> tasks = IntStream.range(0, matrixSize) ++ .parallel() ++ .mapToObj(i -> new Callable() { ++ private final int[] tempColumn = new int[matrixSize]; ++ ++ @Override ++ public Void call() throws Exception { ++ for (int c = 0; c < matrixSize; c++) { ++ tempColumn[c] = matrixB[c][i]; ++ } ++ for (int j = 0; j < matrixSize; j++) { ++ int row[] = matrixA[j]; ++ int sum = 0; ++ for (int k = 0; k < matrixSize; k++) { ++ sum += tempColumn[k] * row[k]; ++ } ++ matrixC[j][i] = sum; ++ } ++ return null; ++ } ++ }) ++ .collect(Collectors.toList()); ++ ++ executor.invokeAll(tasks); ++ return matrixC; ++ } ++ ++ public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { ++ final int matrixSize = matrixA.length; ++ final int[][] matrixC = new int[matrixSize][]; ++ ++ final int[][] matrixBT = new int[matrixSize][matrixSize]; ++ for (int i = 0; i < matrixSize; i++) { ++ for (int j = 0; j < matrixSize; j++) { ++ matrixBT[i][j] = matrixB[j][i]; ++ } ++ } ++ ++ List> tasks = new ArrayList<>(matrixSize); ++ for (int j = 0; j < matrixSize; j++) { ++ final int row = j; ++ tasks.add(() -> { ++ final int[] rowC = new int[matrixSize]; ++ for (int col = 0; col < matrixSize; col++) { ++ final int[] rowA = matrixA[row]; ++ final int[] columnB = matrixBT[col]; ++ int sum = 0; ++ for (int k = 0; k < matrixSize; k++) { ++ sum += rowA[k] * columnB[k]; ++ } ++ rowC[col] = sum; ++ } ++ matrixC[row] = rowC; ++ return null; ++ }); ++ } ++ executor.invokeAll(tasks); + return matrixC; + } + +@@ -64,4 +207,4 @@ + } + return true; + } +-} ++} +\ No newline at end of file diff --git a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java index 7522d57e4..9796e4555 100644 --- a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java +++ b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java @@ -1,8 +1,9 @@ package ru.javaops.masterjava.matrix; -import java.util.Random; -import java.util.concurrent.ExecutionException; -import java.util.concurrent.ExecutorService; +import java.util.*; +import java.util.concurrent.*; +import java.util.stream.Collectors; +import java.util.stream.IntStream; /** * gkislin @@ -10,13 +11,155 @@ */ public class MatrixUtil { - // TODO implement parallel multiplication matrixA*matrixB public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { final int matrixSize = matrixA.length; final int[][] matrixC = new int[matrixSize][matrixSize]; //Comment //Comment 2 + class ColumnMultipleResult { + private final int col; + private final int[] columnC; + + private ColumnMultipleResult(int col, int[] columnC) { + this.col = col; + this.columnC = columnC; + } + } + + final CompletionService completionService = new ExecutorCompletionService<>(executor); + + for (int j = 0; j < matrixSize; j++) { + final int col = j; + final int[] columnB = new int[matrixSize]; + for (int k = 0; k < matrixSize; k++) { + columnB[k] = matrixB[k][col]; + } + completionService.submit(() -> { + final int[] columnC = new int[matrixSize]; + + for (int row = 0; row < matrixSize; row++) { + final int[] rowA = matrixA[row]; + int sum = 0; + for (int k = 0; k < matrixSize; k++) { + sum += rowA[k] * columnB[k]; + } + columnC[row] = sum; + } + return new ColumnMultipleResult(col, columnC); + }); + } + + for (int i = 0; i < matrixSize; i++) { + ColumnMultipleResult res = completionService.take().get(); + for (int k = 0; k < matrixSize; k++) { + matrixC[k][res.col] = res.columnC[k]; + } + } + return matrixC; + } + + public static int[][] concurrentMultiplyCayman(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { + final int matrixSize = matrixA.length; + final int[][] matrixResult = new int[matrixSize][matrixSize]; + final int threadCount = Runtime.getRuntime().availableProcessors(); + final int maxIndex = matrixSize * matrixSize; + final int cellsInThread = maxIndex / threadCount; + final int[][] matrixBFinal = new int[matrixSize][matrixSize]; + + for (int i = 0; i < matrixSize; i++) { + for (int j = 0; j < matrixSize; j++) { + matrixBFinal[i][j] = matrixB[j][i]; + } + } + + Set> threads = new HashSet<>(); + int fromIndex = 0; + for (int i = 1; i <= threadCount; i++) { + final int toIndex = i == threadCount ? maxIndex : fromIndex + cellsInThread; + final int firstIndexFinal = fromIndex; + threads.add(() -> { + for (int j = firstIndexFinal; j < toIndex; j++) { + final int row = j / matrixSize; + final int col = j % matrixSize; + + int sum = 0; + for (int k = 0; k < matrixSize; k++) { + sum += matrixA[row][k] * matrixBFinal[col][k]; + } + matrixResult[row][col] = sum; + } + return true; + }); + fromIndex = toIndex; + } + executor.invokeAll(threads); + return matrixResult; + } + + public static int[][] concurrentMultiplyDarthVader(int[][] matrixA, int[][] matrixB, ExecutorService executor) + throws InterruptedException, ExecutionException { + + final int matrixSize = matrixA.length; + final int[][] matrixC = new int[matrixSize][matrixSize]; + + List> tasks = IntStream.range(0, matrixSize) + .parallel() + .mapToObj(i -> new Callable() { + private final int[] tempColumn = new int[matrixSize]; + + @Override + public Void call() throws Exception { + for (int c = 0; c < matrixSize; c++) { + tempColumn[c] = matrixB[c][i]; + } + for (int j = 0; j < matrixSize; j++) { + int row[] = matrixA[j]; + int sum = 0; + for (int k = 0; k < matrixSize; k++) { + sum += tempColumn[k] * row[k]; + } + matrixC[j][i] = sum; + } + return null; + } + }) + .collect(Collectors.toList()); + + executor.invokeAll(tasks); + return matrixC; + } + + public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { + final int matrixSize = matrixA.length; + final int[][] matrixC = new int[matrixSize][]; + + final int[][] matrixBT = new int[matrixSize][matrixSize]; + for (int i = 0; i < matrixSize; i++) { + for (int j = 0; j < matrixSize; j++) { + matrixBT[i][j] = matrixB[j][i]; + } + } + + List> tasks = new ArrayList<>(matrixSize); + for (int j = 0; j < matrixSize; j++) { + final int row = j; + tasks.add(() -> { + final int[] rowC = new int[matrixSize]; + for (int col = 0; col < matrixSize; col++) { + final int[] rowA = matrixA[row]; + final int[] columnB = matrixBT[col]; + int sum = 0; + for (int k = 0; k < matrixSize; k++) { + sum += rowA[k] * columnB[k]; + } + rowC[col] = sum; + } + matrixC[row] = rowC; + return null; + }); + } + executor.invokeAll(tasks); return matrixC; } @@ -66,4 +209,4 @@ public static boolean compare(int[][] matrixA, int[][] matrixB) { } return true; } -} +} \ No newline at end of file From 5776f81752be1aafb634885e35997526e682bd5d Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Wed, 19 Sep 2018 17:26:17 +0500 Subject: [PATCH 09/20] =?UTF-8?q?=D0=98=D1=81=D0=BF=D1=80=D0=B0=D0=B2?= =?UTF-8?q?=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1=D0=BE?= =?UTF-8?q?=D0=BA=20=D0=B4=D0=BB=D1=8F=20=D1=81=D0=BB=D0=B5=D0=B4=D1=83?= =?UTF-8?q?=D1=8E=D1=89=D0=B5=D0=B3=D0=BE=20=D0=BF=D0=B0=D1=82=D1=87=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java index 9796e4555..46fd00ede 100644 --- a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java +++ b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java @@ -14,8 +14,6 @@ public class MatrixUtil { public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { final int matrixSize = matrixA.length; final int[][] matrixC = new int[matrixSize][matrixSize]; - //Comment - //Comment 2 class ColumnMultipleResult { private final int col; From 655a9e68729d9542e67f745426cb28af8b7ab391 Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Thu, 20 Sep 2018 09:39:48 +0500 Subject: [PATCH 10/20] 2_03_HW1_concurrentMultiply2 --- 2_02_HW1_concurrentMultiply.patch | 182 ------------- 2_03_HW1_concurrentMultiply2.patch | 245 ++++++++++++++++++ .../javaops/masterjava/matrix/MainMatrix.java | 6 +- .../javaops/masterjava/matrix/MatrixUtil.java | 177 +++++-------- 4 files changed, 308 insertions(+), 302 deletions(-) delete mode 100644 2_02_HW1_concurrentMultiply.patch create mode 100644 2_03_HW1_concurrentMultiply2.patch diff --git a/2_02_HW1_concurrentMultiply.patch b/2_02_HW1_concurrentMultiply.patch deleted file mode 100644 index 125962b60..000000000 --- a/2_02_HW1_concurrentMultiply.patch +++ /dev/null @@ -1,182 +0,0 @@ -Index: src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (date 1508266595000) -+++ src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (date 1508268723000) -@@ -1,8 +1,9 @@ - package ru.javaops.masterjava.matrix; - --import java.util.Random; --import java.util.concurrent.ExecutionException; --import java.util.concurrent.ExecutorService; -+import java.util.*; -+import java.util.concurrent.*; -+import java.util.stream.Collectors; -+import java.util.stream.IntStream; - - /** - * gkislin -@@ -10,11 +11,153 @@ - */ - public class MatrixUtil { - -- // TODO implement parallel multiplication matrixA*matrixB - public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { - final int matrixSize = matrixA.length; - final int[][] matrixC = new int[matrixSize][matrixSize]; - -+ class ColumnMultipleResult { -+ private final int col; -+ private final int[] columnC; -+ -+ private ColumnMultipleResult(int col, int[] columnC) { -+ this.col = col; -+ this.columnC = columnC; -+ } -+ } -+ -+ final CompletionService completionService = new ExecutorCompletionService<>(executor); -+ -+ for (int j = 0; j < matrixSize; j++) { -+ final int col = j; -+ final int[] columnB = new int[matrixSize]; -+ for (int k = 0; k < matrixSize; k++) { -+ columnB[k] = matrixB[k][col]; -+ } -+ completionService.submit(() -> { -+ final int[] columnC = new int[matrixSize]; -+ -+ for (int row = 0; row < matrixSize; row++) { -+ final int[] rowA = matrixA[row]; -+ int sum = 0; -+ for (int k = 0; k < matrixSize; k++) { -+ sum += rowA[k] * columnB[k]; -+ } -+ columnC[row] = sum; -+ } -+ return new ColumnMultipleResult(col, columnC); -+ }); -+ } -+ -+ for (int i = 0; i < matrixSize; i++) { -+ ColumnMultipleResult res = completionService.take().get(); -+ for (int k = 0; k < matrixSize; k++) { -+ matrixC[k][res.col] = res.columnC[k]; -+ } -+ } -+ return matrixC; -+ } -+ -+ public static int[][] concurrentMultiplyCayman(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { -+ final int matrixSize = matrixA.length; -+ final int[][] matrixResult = new int[matrixSize][matrixSize]; -+ final int threadCount = Runtime.getRuntime().availableProcessors(); -+ final int maxIndex = matrixSize * matrixSize; -+ final int cellsInThread = maxIndex / threadCount; -+ final int[][] matrixBFinal = new int[matrixSize][matrixSize]; -+ -+ for (int i = 0; i < matrixSize; i++) { -+ for (int j = 0; j < matrixSize; j++) { -+ matrixBFinal[i][j] = matrixB[j][i]; -+ } -+ } -+ -+ Set> threads = new HashSet<>(); -+ int fromIndex = 0; -+ for (int i = 1; i <= threadCount; i++) { -+ final int toIndex = i == threadCount ? maxIndex : fromIndex + cellsInThread; -+ final int firstIndexFinal = fromIndex; -+ threads.add(() -> { -+ for (int j = firstIndexFinal; j < toIndex; j++) { -+ final int row = j / matrixSize; -+ final int col = j % matrixSize; -+ -+ int sum = 0; -+ for (int k = 0; k < matrixSize; k++) { -+ sum += matrixA[row][k] * matrixBFinal[col][k]; -+ } -+ matrixResult[row][col] = sum; -+ } -+ return true; -+ }); -+ fromIndex = toIndex; -+ } -+ executor.invokeAll(threads); -+ return matrixResult; -+ } -+ -+ public static int[][] concurrentMultiplyDarthVader(int[][] matrixA, int[][] matrixB, ExecutorService executor) -+ throws InterruptedException, ExecutionException { -+ -+ final int matrixSize = matrixA.length; -+ final int[][] matrixC = new int[matrixSize][matrixSize]; -+ -+ List> tasks = IntStream.range(0, matrixSize) -+ .parallel() -+ .mapToObj(i -> new Callable() { -+ private final int[] tempColumn = new int[matrixSize]; -+ -+ @Override -+ public Void call() throws Exception { -+ for (int c = 0; c < matrixSize; c++) { -+ tempColumn[c] = matrixB[c][i]; -+ } -+ for (int j = 0; j < matrixSize; j++) { -+ int row[] = matrixA[j]; -+ int sum = 0; -+ for (int k = 0; k < matrixSize; k++) { -+ sum += tempColumn[k] * row[k]; -+ } -+ matrixC[j][i] = sum; -+ } -+ return null; -+ } -+ }) -+ .collect(Collectors.toList()); -+ -+ executor.invokeAll(tasks); -+ return matrixC; -+ } -+ -+ public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { -+ final int matrixSize = matrixA.length; -+ final int[][] matrixC = new int[matrixSize][]; -+ -+ final int[][] matrixBT = new int[matrixSize][matrixSize]; -+ for (int i = 0; i < matrixSize; i++) { -+ for (int j = 0; j < matrixSize; j++) { -+ matrixBT[i][j] = matrixB[j][i]; -+ } -+ } -+ -+ List> tasks = new ArrayList<>(matrixSize); -+ for (int j = 0; j < matrixSize; j++) { -+ final int row = j; -+ tasks.add(() -> { -+ final int[] rowC = new int[matrixSize]; -+ for (int col = 0; col < matrixSize; col++) { -+ final int[] rowA = matrixA[row]; -+ final int[] columnB = matrixBT[col]; -+ int sum = 0; -+ for (int k = 0; k < matrixSize; k++) { -+ sum += rowA[k] * columnB[k]; -+ } -+ rowC[col] = sum; -+ } -+ matrixC[row] = rowC; -+ return null; -+ }); -+ } -+ executor.invokeAll(tasks); - return matrixC; - } - -@@ -64,4 +207,4 @@ - } - return true; - } --} -+} -\ No newline at end of file diff --git a/2_03_HW1_concurrentMultiply2.patch b/2_03_HW1_concurrentMultiply2.patch new file mode 100644 index 000000000..867926b60 --- /dev/null +++ b/2_03_HW1_concurrentMultiply2.patch @@ -0,0 +1,245 @@ +Index: src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (date 1508268723000) ++++ src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (revision ) +@@ -1,134 +1,39 @@ + package ru.javaops.masterjava.matrix; + +-import java.util.*; ++import java.util.ArrayList; ++import java.util.List; ++import java.util.Random; + import java.util.concurrent.*; +-import java.util.stream.Collectors; + import java.util.stream.IntStream; + +-/** +- * gkislin +- * 03.07.2016 +- */ + public class MatrixUtil { + +- public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { +- final int matrixSize = matrixA.length; +- final int[][] matrixC = new int[matrixSize][matrixSize]; +- +- class ColumnMultipleResult { +- private final int col; +- private final int[] columnC; +- +- private ColumnMultipleResult(int col, int[] columnC) { +- this.col = col; +- this.columnC = columnC; +- } +- } +- +- final CompletionService completionService = new ExecutorCompletionService<>(executor); +- +- for (int j = 0; j < matrixSize; j++) { +- final int col = j; +- final int[] columnB = new int[matrixSize]; +- for (int k = 0; k < matrixSize; k++) { +- columnB[k] = matrixB[k][col]; +- } +- completionService.submit(() -> { +- final int[] columnC = new int[matrixSize]; +- +- for (int row = 0; row < matrixSize; row++) { +- final int[] rowA = matrixA[row]; +- int sum = 0; +- for (int k = 0; k < matrixSize; k++) { +- sum += rowA[k] * columnB[k]; +- } +- columnC[row] = sum; +- } +- return new ColumnMultipleResult(col, columnC); +- }); +- } +- +- for (int i = 0; i < matrixSize; i++) { +- ColumnMultipleResult res = completionService.take().get(); +- for (int k = 0; k < matrixSize; k++) { +- matrixC[k][res.col] = res.columnC[k]; +- } +- } +- return matrixC; +- } +- +- public static int[][] concurrentMultiplyCayman(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { +- final int matrixSize = matrixA.length; +- final int[][] matrixResult = new int[matrixSize][matrixSize]; +- final int threadCount = Runtime.getRuntime().availableProcessors(); +- final int maxIndex = matrixSize * matrixSize; +- final int cellsInThread = maxIndex / threadCount; +- final int[][] matrixBFinal = new int[matrixSize][matrixSize]; +- +- for (int i = 0; i < matrixSize; i++) { +- for (int j = 0; j < matrixSize; j++) { +- matrixBFinal[i][j] = matrixB[j][i]; +- } +- } +- +- Set> threads = new HashSet<>(); +- int fromIndex = 0; +- for (int i = 1; i <= threadCount; i++) { +- final int toIndex = i == threadCount ? maxIndex : fromIndex + cellsInThread; +- final int firstIndexFinal = fromIndex; +- threads.add(() -> { +- for (int j = firstIndexFinal; j < toIndex; j++) { +- final int row = j / matrixSize; +- final int col = j % matrixSize; +- +- int sum = 0; +- for (int k = 0; k < matrixSize; k++) { +- sum += matrixA[row][k] * matrixBFinal[col][k]; +- } +- matrixResult[row][col] = sum; +- } +- return true; +- }); +- fromIndex = toIndex; +- } +- executor.invokeAll(threads); +- return matrixResult; +- } +- +- public static int[][] concurrentMultiplyDarthVader(int[][] matrixA, int[][] matrixB, ExecutorService executor) ++ public static int[][] concurrentMultiplyStreams(int[][] matrixA, int[][] matrixB, int threadNumber) + throws InterruptedException, ExecutionException { + + final int matrixSize = matrixA.length; + final int[][] matrixC = new int[matrixSize][matrixSize]; + +- List> tasks = IntStream.range(0, matrixSize) +- .parallel() +- .mapToObj(i -> new Callable() { +- private final int[] tempColumn = new int[matrixSize]; ++ new ForkJoinPool(threadNumber).submit( ++ () -> IntStream.range(0, matrixSize) ++ .parallel() ++ .forEach(row -> { ++ final int[] rowA = matrixA[row]; ++ final int[] rowC = matrixC[row]; + +- @Override +- public Void call() throws Exception { +- for (int c = 0; c < matrixSize; c++) { +- tempColumn[c] = matrixB[c][i]; +- } +- for (int j = 0; j < matrixSize; j++) { +- int row[] = matrixA[j]; +- int sum = 0; +- for (int k = 0; k < matrixSize; k++) { +- sum += tempColumn[k] * row[k]; ++ for (int idx = 0; idx < matrixSize; idx++) { ++ final int elA = rowA[idx]; ++ final int[] rowB = matrixB[idx]; ++ for (int col = 0; col < matrixSize; col++) { ++ rowC[col] += elA * rowB[col]; ++ } + } +- matrixC[j][i] = sum; +- } +- return null; +- } +- }) +- .collect(Collectors.toList()); ++ })).get(); + +- executor.invokeAll(tasks); + return matrixC; + } + +- public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { ++ public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { + final int matrixSize = matrixA.length; + final int[][] matrixC = new int[matrixSize][]; + +@@ -161,7 +66,30 @@ + return matrixC; + } + +- // Optimized by https://habrahabr.ru/post/114797/ ++ public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException { ++ final int matrixSize = matrixA.length; ++ final int[][] matrixC = new int[matrixSize][matrixSize]; ++ final CountDownLatch latch = new CountDownLatch(matrixSize); ++ ++ for (int row = 0; row < matrixSize; row++) { ++ final int[] rowA = matrixA[row]; ++ final int[] rowC = matrixC[row]; ++ ++ executor.submit(() -> { ++ for (int idx = 0; idx < matrixSize; idx++) { ++ final int elA = rowA[idx]; ++ final int[] rowB = matrixB[idx]; ++ for (int col = 0; col < matrixSize; col++) { ++ rowC[col] += elA * rowB[col]; ++ } ++ } ++ latch.countDown(); ++ }); ++ } ++ latch.await(); ++ return matrixC; ++ } ++ + public static int[][] singleThreadMultiplyOpt(int[][] matrixA, int[][] matrixB) { + final int matrixSize = matrixA.length; + final int[][] matrixC = new int[matrixSize][matrixSize]; +@@ -183,6 +111,25 @@ + } + return matrixC; + } ++ ++ public static int[][] singleThreadMultiplyOpt2(int[][] matrixA, int[][] matrixB) { ++ final int matrixSize = matrixA.length; ++ final int[][] matrixC = new int[matrixSize][matrixSize]; ++ ++ for (int row = 0; row < matrixSize; row++) { ++ final int[] rowA = matrixA[row]; ++ final int[] rowC = matrixC[row]; ++ ++ for (int idx = 0; idx < matrixSize; idx++) { ++ final int elA = rowA[idx]; ++ final int[] rowB = matrixB[idx]; ++ for (int col = 0; col < matrixSize; col++) { ++ rowC[col] += elA * rowB[col]; ++ } ++ } ++ } ++ return matrixC; ++ } + + public static int[][] create(int size) { + int[][] matrix = new int[size][size]; +Index: src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java (date 1508268723000) ++++ src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java (revision ) +@@ -4,10 +4,6 @@ + import java.util.concurrent.ExecutorService; + import java.util.concurrent.Executors; + +-/** +- * gkislin +- * 03.07.2016 +- */ + public class MainMatrix { + private static final int MATRIX_SIZE = 1000; + private static final int THREAD_NUMBER = 10; +@@ -30,7 +26,7 @@ + singleThreadSum += duration; + + start = System.currentTimeMillis(); +- final int[][] concurrentMatrixC = MatrixUtil.concurrentMultiply(matrixA, matrixB, executor); ++ final int[][] concurrentMatrixC = MatrixUtil.concurrentMultiplyStreams(matrixA, matrixB, Runtime.getRuntime().availableProcessors() - 1); + duration = (System.currentTimeMillis() - start) / 1000.; + out("Concurrent thread time, sec: %.3f", duration); + concurrentThreadSum += duration; diff --git a/src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java b/src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java index 0695132ef..1f1380674 100644 --- a/src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java +++ b/src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java @@ -4,10 +4,6 @@ import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; -/** - * gkislin - * 03.07.2016 - */ public class MainMatrix { private static final int MATRIX_SIZE = 1000; private static final int THREAD_NUMBER = 10; @@ -30,7 +26,7 @@ public static void main(String[] args) throws ExecutionException, InterruptedExc singleThreadSum += duration; start = System.currentTimeMillis(); - final int[][] concurrentMatrixC = MatrixUtil.concurrentMultiply(matrixA, matrixB, executor); + final int[][] concurrentMatrixC = MatrixUtil.concurrentMultiplyStreams(matrixA, matrixB, Runtime.getRuntime().availableProcessors() - 1); duration = (System.currentTimeMillis() - start) / 1000.; out("Concurrent thread time, sec: %.3f", duration); concurrentThreadSum += duration; diff --git a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java index 46fd00ede..d00a67a05 100644 --- a/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java +++ b/src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java @@ -1,134 +1,39 @@ package ru.javaops.masterjava.matrix; -import java.util.*; +import java.util.ArrayList; +import java.util.List; +import java.util.Random; import java.util.concurrent.*; -import java.util.stream.Collectors; import java.util.stream.IntStream; -/** - * gkislin - * 03.07.2016 - */ public class MatrixUtil { - public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { - final int matrixSize = matrixA.length; - final int[][] matrixC = new int[matrixSize][matrixSize]; - - class ColumnMultipleResult { - private final int col; - private final int[] columnC; - - private ColumnMultipleResult(int col, int[] columnC) { - this.col = col; - this.columnC = columnC; - } - } - - final CompletionService completionService = new ExecutorCompletionService<>(executor); - - for (int j = 0; j < matrixSize; j++) { - final int col = j; - final int[] columnB = new int[matrixSize]; - for (int k = 0; k < matrixSize; k++) { - columnB[k] = matrixB[k][col]; - } - completionService.submit(() -> { - final int[] columnC = new int[matrixSize]; - - for (int row = 0; row < matrixSize; row++) { - final int[] rowA = matrixA[row]; - int sum = 0; - for (int k = 0; k < matrixSize; k++) { - sum += rowA[k] * columnB[k]; - } - columnC[row] = sum; - } - return new ColumnMultipleResult(col, columnC); - }); - } - - for (int i = 0; i < matrixSize; i++) { - ColumnMultipleResult res = completionService.take().get(); - for (int k = 0; k < matrixSize; k++) { - matrixC[k][res.col] = res.columnC[k]; - } - } - return matrixC; - } - - public static int[][] concurrentMultiplyCayman(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { - final int matrixSize = matrixA.length; - final int[][] matrixResult = new int[matrixSize][matrixSize]; - final int threadCount = Runtime.getRuntime().availableProcessors(); - final int maxIndex = matrixSize * matrixSize; - final int cellsInThread = maxIndex / threadCount; - final int[][] matrixBFinal = new int[matrixSize][matrixSize]; - - for (int i = 0; i < matrixSize; i++) { - for (int j = 0; j < matrixSize; j++) { - matrixBFinal[i][j] = matrixB[j][i]; - } - } - - Set> threads = new HashSet<>(); - int fromIndex = 0; - for (int i = 1; i <= threadCount; i++) { - final int toIndex = i == threadCount ? maxIndex : fromIndex + cellsInThread; - final int firstIndexFinal = fromIndex; - threads.add(() -> { - for (int j = firstIndexFinal; j < toIndex; j++) { - final int row = j / matrixSize; - final int col = j % matrixSize; - - int sum = 0; - for (int k = 0; k < matrixSize; k++) { - sum += matrixA[row][k] * matrixBFinal[col][k]; - } - matrixResult[row][col] = sum; - } - return true; - }); - fromIndex = toIndex; - } - executor.invokeAll(threads); - return matrixResult; - } - - public static int[][] concurrentMultiplyDarthVader(int[][] matrixA, int[][] matrixB, ExecutorService executor) + public static int[][] concurrentMultiplyStreams(int[][] matrixA, int[][] matrixB, int threadNumber) throws InterruptedException, ExecutionException { final int matrixSize = matrixA.length; final int[][] matrixC = new int[matrixSize][matrixSize]; - List> tasks = IntStream.range(0, matrixSize) - .parallel() - .mapToObj(i -> new Callable() { - private final int[] tempColumn = new int[matrixSize]; - - @Override - public Void call() throws Exception { - for (int c = 0; c < matrixSize; c++) { - tempColumn[c] = matrixB[c][i]; - } - for (int j = 0; j < matrixSize; j++) { - int row[] = matrixA[j]; - int sum = 0; - for (int k = 0; k < matrixSize; k++) { - sum += tempColumn[k] * row[k]; + new ForkJoinPool(threadNumber).submit( + () -> IntStream.range(0, matrixSize) + .parallel() + .forEach(row -> { + final int[] rowA = matrixA[row]; + final int[] rowC = matrixC[row]; + + for (int idx = 0; idx < matrixSize; idx++) { + final int elA = rowA[idx]; + final int[] rowB = matrixB[idx]; + for (int col = 0; col < matrixSize; col++) { + rowC[col] += elA * rowB[col]; + } } - matrixC[j][i] = sum; - } - return null; - } - }) - .collect(Collectors.toList()); + })).get(); - executor.invokeAll(tasks); return matrixC; } - public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { + public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { final int matrixSize = matrixA.length; final int[][] matrixC = new int[matrixSize][]; @@ -161,7 +66,30 @@ public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, Exec return matrixC; } - // Optimized by https://habrahabr.ru/post/114797/ + public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException { + final int matrixSize = matrixA.length; + final int[][] matrixC = new int[matrixSize][matrixSize]; + final CountDownLatch latch = new CountDownLatch(matrixSize); + + for (int row = 0; row < matrixSize; row++) { + final int[] rowA = matrixA[row]; + final int[] rowC = matrixC[row]; + + executor.submit(() -> { + for (int idx = 0; idx < matrixSize; idx++) { + final int elA = rowA[idx]; + final int[] rowB = matrixB[idx]; + for (int col = 0; col < matrixSize; col++) { + rowC[col] += elA * rowB[col]; + } + } + latch.countDown(); + }); + } + latch.await(); + return matrixC; + } + public static int[][] singleThreadMultiplyOpt(int[][] matrixA, int[][] matrixB) { final int matrixSize = matrixA.length; final int[][] matrixC = new int[matrixSize][matrixSize]; @@ -184,6 +112,25 @@ public static int[][] singleThreadMultiplyOpt(int[][] matrixA, int[][] matrixB) return matrixC; } + public static int[][] singleThreadMultiplyOpt2(int[][] matrixA, int[][] matrixB) { + final int matrixSize = matrixA.length; + final int[][] matrixC = new int[matrixSize][matrixSize]; + + for (int row = 0; row < matrixSize; row++) { + final int[] rowA = matrixA[row]; + final int[] rowC = matrixC[row]; + + for (int idx = 0; idx < matrixSize; idx++) { + final int elA = rowA[idx]; + final int[] rowB = matrixB[idx]; + for (int col = 0; col < matrixSize; col++) { + rowC[col] += elA * rowB[col]; + } + } + } + return matrixC; + } + public static int[][] create(int size) { int[][] matrix = new int[size][size]; Random rn = new Random(); From cc0105036b55cd6ed1faf70dc2d244676806bb44 Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Thu, 20 Sep 2018 10:07:44 +0500 Subject: [PATCH 11/20] 2_04_JMH_Benchmark.patch --- 2_03_HW1_concurrentMultiply2.patch | 245 ------------------ 2_04_JMH_Benchmark.patch | 113 ++++++++ pom.xml | 13 +- .../masterjava/matrix/MatrixBenchmark.java | 70 +++++ 4 files changed, 195 insertions(+), 246 deletions(-) delete mode 100644 2_03_HW1_concurrentMultiply2.patch create mode 100644 2_04_JMH_Benchmark.patch create mode 100644 src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java diff --git a/2_03_HW1_concurrentMultiply2.patch b/2_03_HW1_concurrentMultiply2.patch deleted file mode 100644 index 867926b60..000000000 --- a/2_03_HW1_concurrentMultiply2.patch +++ /dev/null @@ -1,245 +0,0 @@ -Index: src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (date 1508268723000) -+++ src/main/java/ru/javaops/masterjava/matrix/MatrixUtil.java (revision ) -@@ -1,134 +1,39 @@ - package ru.javaops.masterjava.matrix; - --import java.util.*; -+import java.util.ArrayList; -+import java.util.List; -+import java.util.Random; - import java.util.concurrent.*; --import java.util.stream.Collectors; - import java.util.stream.IntStream; - --/** -- * gkislin -- * 03.07.2016 -- */ - public class MatrixUtil { - -- public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { -- final int matrixSize = matrixA.length; -- final int[][] matrixC = new int[matrixSize][matrixSize]; -- -- class ColumnMultipleResult { -- private final int col; -- private final int[] columnC; -- -- private ColumnMultipleResult(int col, int[] columnC) { -- this.col = col; -- this.columnC = columnC; -- } -- } -- -- final CompletionService completionService = new ExecutorCompletionService<>(executor); -- -- for (int j = 0; j < matrixSize; j++) { -- final int col = j; -- final int[] columnB = new int[matrixSize]; -- for (int k = 0; k < matrixSize; k++) { -- columnB[k] = matrixB[k][col]; -- } -- completionService.submit(() -> { -- final int[] columnC = new int[matrixSize]; -- -- for (int row = 0; row < matrixSize; row++) { -- final int[] rowA = matrixA[row]; -- int sum = 0; -- for (int k = 0; k < matrixSize; k++) { -- sum += rowA[k] * columnB[k]; -- } -- columnC[row] = sum; -- } -- return new ColumnMultipleResult(col, columnC); -- }); -- } -- -- for (int i = 0; i < matrixSize; i++) { -- ColumnMultipleResult res = completionService.take().get(); -- for (int k = 0; k < matrixSize; k++) { -- matrixC[k][res.col] = res.columnC[k]; -- } -- } -- return matrixC; -- } -- -- public static int[][] concurrentMultiplyCayman(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { -- final int matrixSize = matrixA.length; -- final int[][] matrixResult = new int[matrixSize][matrixSize]; -- final int threadCount = Runtime.getRuntime().availableProcessors(); -- final int maxIndex = matrixSize * matrixSize; -- final int cellsInThread = maxIndex / threadCount; -- final int[][] matrixBFinal = new int[matrixSize][matrixSize]; -- -- for (int i = 0; i < matrixSize; i++) { -- for (int j = 0; j < matrixSize; j++) { -- matrixBFinal[i][j] = matrixB[j][i]; -- } -- } -- -- Set> threads = new HashSet<>(); -- int fromIndex = 0; -- for (int i = 1; i <= threadCount; i++) { -- final int toIndex = i == threadCount ? maxIndex : fromIndex + cellsInThread; -- final int firstIndexFinal = fromIndex; -- threads.add(() -> { -- for (int j = firstIndexFinal; j < toIndex; j++) { -- final int row = j / matrixSize; -- final int col = j % matrixSize; -- -- int sum = 0; -- for (int k = 0; k < matrixSize; k++) { -- sum += matrixA[row][k] * matrixBFinal[col][k]; -- } -- matrixResult[row][col] = sum; -- } -- return true; -- }); -- fromIndex = toIndex; -- } -- executor.invokeAll(threads); -- return matrixResult; -- } -- -- public static int[][] concurrentMultiplyDarthVader(int[][] matrixA, int[][] matrixB, ExecutorService executor) -+ public static int[][] concurrentMultiplyStreams(int[][] matrixA, int[][] matrixB, int threadNumber) - throws InterruptedException, ExecutionException { - - final int matrixSize = matrixA.length; - final int[][] matrixC = new int[matrixSize][matrixSize]; - -- List> tasks = IntStream.range(0, matrixSize) -- .parallel() -- .mapToObj(i -> new Callable() { -- private final int[] tempColumn = new int[matrixSize]; -+ new ForkJoinPool(threadNumber).submit( -+ () -> IntStream.range(0, matrixSize) -+ .parallel() -+ .forEach(row -> { -+ final int[] rowA = matrixA[row]; -+ final int[] rowC = matrixC[row]; - -- @Override -- public Void call() throws Exception { -- for (int c = 0; c < matrixSize; c++) { -- tempColumn[c] = matrixB[c][i]; -- } -- for (int j = 0; j < matrixSize; j++) { -- int row[] = matrixA[j]; -- int sum = 0; -- for (int k = 0; k < matrixSize; k++) { -- sum += tempColumn[k] * row[k]; -+ for (int idx = 0; idx < matrixSize; idx++) { -+ final int elA = rowA[idx]; -+ final int[] rowB = matrixB[idx]; -+ for (int col = 0; col < matrixSize; col++) { -+ rowC[col] += elA * rowB[col]; -+ } - } -- matrixC[j][i] = sum; -- } -- return null; -- } -- }) -- .collect(Collectors.toList()); -+ })).get(); - -- executor.invokeAll(tasks); - return matrixC; - } - -- public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { -+ public static int[][] concurrentMultiply(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException, ExecutionException { - final int matrixSize = matrixA.length; - final int[][] matrixC = new int[matrixSize][]; - -@@ -161,7 +66,30 @@ - return matrixC; - } - -- // Optimized by https://habrahabr.ru/post/114797/ -+ public static int[][] concurrentMultiply2(int[][] matrixA, int[][] matrixB, ExecutorService executor) throws InterruptedException { -+ final int matrixSize = matrixA.length; -+ final int[][] matrixC = new int[matrixSize][matrixSize]; -+ final CountDownLatch latch = new CountDownLatch(matrixSize); -+ -+ for (int row = 0; row < matrixSize; row++) { -+ final int[] rowA = matrixA[row]; -+ final int[] rowC = matrixC[row]; -+ -+ executor.submit(() -> { -+ for (int idx = 0; idx < matrixSize; idx++) { -+ final int elA = rowA[idx]; -+ final int[] rowB = matrixB[idx]; -+ for (int col = 0; col < matrixSize; col++) { -+ rowC[col] += elA * rowB[col]; -+ } -+ } -+ latch.countDown(); -+ }); -+ } -+ latch.await(); -+ return matrixC; -+ } -+ - public static int[][] singleThreadMultiplyOpt(int[][] matrixA, int[][] matrixB) { - final int matrixSize = matrixA.length; - final int[][] matrixC = new int[matrixSize][matrixSize]; -@@ -183,6 +111,25 @@ - } - return matrixC; - } -+ -+ public static int[][] singleThreadMultiplyOpt2(int[][] matrixA, int[][] matrixB) { -+ final int matrixSize = matrixA.length; -+ final int[][] matrixC = new int[matrixSize][matrixSize]; -+ -+ for (int row = 0; row < matrixSize; row++) { -+ final int[] rowA = matrixA[row]; -+ final int[] rowC = matrixC[row]; -+ -+ for (int idx = 0; idx < matrixSize; idx++) { -+ final int elA = rowA[idx]; -+ final int[] rowB = matrixB[idx]; -+ for (int col = 0; col < matrixSize; col++) { -+ rowC[col] += elA * rowB[col]; -+ } -+ } -+ } -+ return matrixC; -+ } - - public static int[][] create(int size) { - int[][] matrix = new int[size][size]; -Index: src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java (date 1508268723000) -+++ src/main/java/ru/javaops/masterjava/matrix/MainMatrix.java (revision ) -@@ -4,10 +4,6 @@ - import java.util.concurrent.ExecutorService; - import java.util.concurrent.Executors; - --/** -- * gkislin -- * 03.07.2016 -- */ - public class MainMatrix { - private static final int MATRIX_SIZE = 1000; - private static final int THREAD_NUMBER = 10; -@@ -30,7 +26,7 @@ - singleThreadSum += duration; - - start = System.currentTimeMillis(); -- final int[][] concurrentMatrixC = MatrixUtil.concurrentMultiply(matrixA, matrixB, executor); -+ final int[][] concurrentMatrixC = MatrixUtil.concurrentMultiplyStreams(matrixA, matrixB, Runtime.getRuntime().availableProcessors() - 1); - duration = (System.currentTimeMillis() - start) / 1000.; - out("Concurrent thread time, sec: %.3f", duration); - concurrentThreadSum += duration; diff --git a/2_04_JMH_Benchmark.patch b/2_04_JMH_Benchmark.patch new file mode 100644 index 000000000..d5bd75f16 --- /dev/null +++ b/2_04_JMH_Benchmark.patch @@ -0,0 +1,113 @@ +Index: src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java (revision ) ++++ src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java (revision ) +@@ -0,0 +1,70 @@ ++package ru.javaops.masterjava.matrix; ++ ++import org.openjdk.jmh.annotations.*; ++ ++import java.util.concurrent.ExecutorService; ++import java.util.concurrent.Executors; ++import java.util.concurrent.TimeUnit; ++ ++@Warmup(iterations = 10) ++@Measurement(iterations = 10) ++@BenchmarkMode({Mode.SingleShotTime}) ++@OutputTimeUnit(TimeUnit.MILLISECONDS) ++@State(Scope.Benchmark) ++@Threads(1) ++@Fork(10) ++@Timeout(time = 5, timeUnit = TimeUnit.MINUTES) ++public class MatrixBenchmark { ++ ++ // Matrix size ++ private static final int MATRIX_SIZE = 1000; ++ ++ @Param({"3", "4", "10"}) ++ private int threadNumber; ++ ++ private static int[][] matrixA; ++ private static int[][] matrixB; ++ ++ @Setup ++ public void setUp() { ++ matrixA = MatrixUtil.create(MATRIX_SIZE); ++ matrixB = MatrixUtil.create(MATRIX_SIZE); ++ } ++ ++ private ExecutorService executor; ++ ++ // @Benchmark ++ public int[][] singleThreadMultiplyOpt() throws Exception { ++ return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB); ++ } ++ ++ // @Benchmark ++ public int[][] singleThreadMultiplyOpt2() throws Exception { ++ return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB); ++ } ++ ++ @Benchmark ++ public int[][] concurrentMultiplyStreams() throws Exception { ++ return MatrixUtil.concurrentMultiplyStreams(matrixA, matrixB, threadNumber); ++ } ++ ++ // @Benchmark ++ public int[][] concurrentMultiply() throws Exception { ++ return MatrixUtil.concurrentMultiply(matrixA, matrixB, executor); ++ } ++ ++ @Benchmark ++ public int[][] concurrentMultiply2() throws Exception { ++ return MatrixUtil.concurrentMultiply2(matrixA, matrixB, executor); ++ } ++ ++ @Setup ++ public void setup() { ++ executor = Executors.newFixedThreadPool(threadNumber); ++ } ++ ++ @TearDown ++ public void tearDown() { ++ executor.shutdown(); ++ } ++} +\ No newline at end of file +Index: pom.xml +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- pom.xml (date 1508790464000) ++++ pom.xml (revision ) +@@ -24,7 +24,7 @@ + + org.apache.maven.plugins + maven-compiler-plugin +- 3.1 ++ 3.7.0 + + ${java.version} + ${java.version} +@@ -34,6 +34,17 @@ + + + ++ ++ org.openjdk.jmh ++ jmh-core ++ RELEASE ++ ++ ++ org.openjdk.jmh ++ jmh-generator-annprocess ++ RELEASE ++ provided ++ + + + diff --git a/pom.xml b/pom.xml index 39e811e08..9f0832e4e 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ org.apache.maven.plugins maven-compiler-plugin - 3.1 + 3.7.0 ${java.version} ${java.version} @@ -34,6 +34,17 @@ + + org.openjdk.jmh + jmh-core + RELEASE + + + org.openjdk.jmh + jmh-generator-annprocess + RELEASE + provided + diff --git a/src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java b/src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java new file mode 100644 index 000000000..65fc52878 --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java @@ -0,0 +1,70 @@ +package ru.javaops.masterjava.matrix; + +import org.openjdk.jmh.annotations.*; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; + +@Warmup(iterations = 10) +@Measurement(iterations = 10) +@BenchmarkMode({Mode.SingleShotTime}) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@State(Scope.Benchmark) +@Threads(1) +@Fork(10) +@Timeout(time = 5, timeUnit = TimeUnit.MINUTES) +public class MatrixBenchmark { + + // Matrix size + private static final int MATRIX_SIZE = 1000; + + @Param({"3", "4", "10"}) + private int threadNumber; + + private static int[][] matrixA; + private static int[][] matrixB; + + @Setup + public void setUp() { + matrixA = MatrixUtil.create(MATRIX_SIZE); + matrixB = MatrixUtil.create(MATRIX_SIZE); + } + + private ExecutorService executor; + + // @Benchmark + public int[][] singleThreadMultiplyOpt() throws Exception { + return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB); + } + + // @Benchmark + public int[][] singleThreadMultiplyOpt2() throws Exception { + return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB); + } + + @Benchmark + public int[][] concurrentMultiplyStreams() throws Exception { + return MatrixUtil.concurrentMultiplyStreams(matrixA, matrixB, threadNumber); + } + + // @Benchmark + public int[][] concurrentMultiply() throws Exception { + return MatrixUtil.concurrentMultiply(matrixA, matrixB, executor); + } + + @Benchmark + public int[][] concurrentMultiply2() throws Exception { + return MatrixUtil.concurrentMultiply2(matrixA, matrixB, executor); + } + + @Setup + public void setup() { + executor = Executors.newFixedThreadPool(threadNumber); + } + + @TearDown + public void tearDown() { + executor.shutdown(); + } +} \ No newline at end of file From 2d8f085ebaa3d1d8c557e49761e0239453bbc046 Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Thu, 20 Sep 2018 10:08:41 +0500 Subject: [PATCH 12/20] 2_05 --- 2_04_JMH_Benchmark.patch | 113 ------------------ pom.xml | 35 ++++++ .../masterjava/matrix/MatrixBenchmark.java | 15 +++ 3 files changed, 50 insertions(+), 113 deletions(-) delete mode 100644 2_04_JMH_Benchmark.patch diff --git a/2_04_JMH_Benchmark.patch b/2_04_JMH_Benchmark.patch deleted file mode 100644 index d5bd75f16..000000000 --- a/2_04_JMH_Benchmark.patch +++ /dev/null @@ -1,113 +0,0 @@ -Index: src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java (revision ) -+++ src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java (revision ) -@@ -0,0 +1,70 @@ -+package ru.javaops.masterjava.matrix; -+ -+import org.openjdk.jmh.annotations.*; -+ -+import java.util.concurrent.ExecutorService; -+import java.util.concurrent.Executors; -+import java.util.concurrent.TimeUnit; -+ -+@Warmup(iterations = 10) -+@Measurement(iterations = 10) -+@BenchmarkMode({Mode.SingleShotTime}) -+@OutputTimeUnit(TimeUnit.MILLISECONDS) -+@State(Scope.Benchmark) -+@Threads(1) -+@Fork(10) -+@Timeout(time = 5, timeUnit = TimeUnit.MINUTES) -+public class MatrixBenchmark { -+ -+ // Matrix size -+ private static final int MATRIX_SIZE = 1000; -+ -+ @Param({"3", "4", "10"}) -+ private int threadNumber; -+ -+ private static int[][] matrixA; -+ private static int[][] matrixB; -+ -+ @Setup -+ public void setUp() { -+ matrixA = MatrixUtil.create(MATRIX_SIZE); -+ matrixB = MatrixUtil.create(MATRIX_SIZE); -+ } -+ -+ private ExecutorService executor; -+ -+ // @Benchmark -+ public int[][] singleThreadMultiplyOpt() throws Exception { -+ return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB); -+ } -+ -+ // @Benchmark -+ public int[][] singleThreadMultiplyOpt2() throws Exception { -+ return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB); -+ } -+ -+ @Benchmark -+ public int[][] concurrentMultiplyStreams() throws Exception { -+ return MatrixUtil.concurrentMultiplyStreams(matrixA, matrixB, threadNumber); -+ } -+ -+ // @Benchmark -+ public int[][] concurrentMultiply() throws Exception { -+ return MatrixUtil.concurrentMultiply(matrixA, matrixB, executor); -+ } -+ -+ @Benchmark -+ public int[][] concurrentMultiply2() throws Exception { -+ return MatrixUtil.concurrentMultiply2(matrixA, matrixB, executor); -+ } -+ -+ @Setup -+ public void setup() { -+ executor = Executors.newFixedThreadPool(threadNumber); -+ } -+ -+ @TearDown -+ public void tearDown() { -+ executor.shutdown(); -+ } -+} -\ No newline at end of file -Index: pom.xml -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- pom.xml (date 1508790464000) -+++ pom.xml (revision ) -@@ -24,7 +24,7 @@ - - org.apache.maven.plugins - maven-compiler-plugin -- 3.1 -+ 3.7.0 - - ${java.version} - ${java.version} -@@ -34,6 +34,17 @@ - - - -+ -+ org.openjdk.jmh -+ jmh-core -+ RELEASE -+ -+ -+ org.openjdk.jmh -+ jmh-generator-annprocess -+ RELEASE -+ provided -+ - - - diff --git a/pom.xml b/pom.xml index 9f0832e4e..4cadaf85e 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,41 @@ ${java.version} + + org.apache.maven.plugins + maven-shade-plugin + 3.1.0 + + + package + + shade + + + benchmarks + + + org.openjdk.jmh.Main + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + diff --git a/src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java b/src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java index 65fc52878..2df2fc674 100644 --- a/src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java +++ b/src/main/java/ru/javaops/masterjava/matrix/MatrixBenchmark.java @@ -1,6 +1,11 @@ package ru.javaops.masterjava.matrix; import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.runner.Runner; +import org.openjdk.jmh.runner.RunnerException; +import org.openjdk.jmh.runner.options.Options; +import org.openjdk.jmh.runner.options.OptionsBuilder; +import org.openjdk.jmh.runner.options.TimeValue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -33,6 +38,16 @@ public void setUp() { private ExecutorService executor; + public static void main(String[] args) throws RunnerException { + Options options = new OptionsBuilder() + .include(MatrixBenchmark.class.getSimpleName()) + .threads(1) + .forks(10) + .timeout(TimeValue.minutes(5)) + .build(); + new Runner(options).run(); + } + // @Benchmark public int[][] singleThreadMultiplyOpt() throws Exception { return MatrixUtil.singleThreadMultiplyOpt(matrixA, matrixB); From d4425914ea14aeae93e95dd6aed71915dedfc695 Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Thu, 20 Sep 2018 11:43:54 +0500 Subject: [PATCH 13/20] 2_06_xml_scheme.patch --- 2_06_xml_scheme.patch | 754 ++++++++++++++++++ .../masterjava/xml/schema/CityType.java | 94 +++ .../masterjava/xml/schema/FlagType.java | 54 ++ .../masterjava/xml/schema/ObjectFactory.java | 85 ++ .../masterjava/xml/schema/Payload.java | 233 ++++++ .../javaops/masterjava/xml/schema/User.java | 151 ++++ src/main/resources/payload.xsd | 56 ++ src/test/resources/payload.xml | 23 + 8 files changed, 1450 insertions(+) create mode 100644 2_06_xml_scheme.patch create mode 100644 src/main/java/ru/javaops/masterjava/xml/schema/CityType.java create mode 100644 src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java create mode 100644 src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java create mode 100644 src/main/java/ru/javaops/masterjava/xml/schema/Payload.java create mode 100644 src/main/java/ru/javaops/masterjava/xml/schema/User.java create mode 100644 src/main/resources/payload.xsd create mode 100644 src/test/resources/payload.xml diff --git a/2_06_xml_scheme.patch b/2_06_xml_scheme.patch new file mode 100644 index 000000000..9650668ee --- /dev/null +++ b/2_06_xml_scheme.patch @@ -0,0 +1,754 @@ +Index: src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java (revision ) +@@ -0,0 +1,85 @@ ++ ++package ru.javaops.masterjava.xml.schema; ++ ++import javax.xml.bind.JAXBElement; ++import javax.xml.bind.annotation.XmlElementDecl; ++import javax.xml.bind.annotation.XmlRegistry; ++import javax.xml.namespace.QName; ++ ++ ++/** ++ * This object contains factory methods for each ++ * Java content interface and Java element interface ++ * generated in the ru.javaops.masterjava.xml.schema package. ++ *

An ObjectFactory allows you to programatically ++ * construct new instances of the Java representation ++ * for XML content. The Java representation of XML ++ * content can consist of schema derived interfaces ++ * and classes representing the binding of schema ++ * type definitions, element declarations and model ++ * groups. Factory methods for each of these are ++ * provided in this class. ++ * ++ */ ++@XmlRegistry ++public class ObjectFactory { ++ ++ private final static QName _City_QNAME = new QName("http://javaops.ru", "City"); ++ ++ /** ++ * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: ru.javaops.masterjava.xml.schema ++ * ++ */ ++ public ObjectFactory() { ++ } ++ ++ /** ++ * Create an instance of {@link Payload } ++ * ++ */ ++ public Payload createPayload() { ++ return new Payload(); ++ } ++ ++ /** ++ * Create an instance of {@link User } ++ * ++ */ ++ public User createUser() { ++ return new User(); ++ } ++ ++ /** ++ * Create an instance of {@link Payload.Cities } ++ * ++ */ ++ public Payload.Cities createPayloadCities() { ++ return new Payload.Cities(); ++ } ++ ++ /** ++ * Create an instance of {@link Payload.Users } ++ * ++ */ ++ public Payload.Users createPayloadUsers() { ++ return new Payload.Users(); ++ } ++ ++ /** ++ * Create an instance of {@link CityType } ++ * ++ */ ++ public CityType createCityType() { ++ return new CityType(); ++ } ++ ++ /** ++ * Create an instance of {@link JAXBElement }{@code <}{@link CityType }{@code >}} ++ * ++ */ ++ @XmlElementDecl(namespace = "http://javaops.ru", name = "City") ++ public JAXBElement createCity(CityType value) { ++ return new JAXBElement(_City_QNAME, CityType.class, null, value); ++ } ++ ++} +Index: src/main/java/ru/javaops/masterjava/xml/schema/Payload.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/schema/Payload.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/schema/Payload.java (revision ) +@@ -0,0 +1,233 @@ ++ ++package ru.javaops.masterjava.xml.schema; ++ ++import java.util.ArrayList; ++import java.util.List; ++import javax.xml.bind.annotation.XmlAccessType; ++import javax.xml.bind.annotation.XmlAccessorType; ++import javax.xml.bind.annotation.XmlElement; ++import javax.xml.bind.annotation.XmlRootElement; ++import javax.xml.bind.annotation.XmlType; ++ ++ ++/** ++ *

Java class for anonymous complex type. ++ * ++ *

The following schema fragment specifies the expected content contained within this class. ++ * ++ *

++ * <complexType>
++ *   <complexContent>
++ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
++ *       <all>
++ *         <element name="Cities">
++ *           <complexType>
++ *             <complexContent>
++ *               <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
++ *                 <sequence maxOccurs="unbounded">
++ *                   <element ref="{http://javaops.ru}City"/>
++ *                 </sequence>
++ *               </restriction>
++ *             </complexContent>
++ *           </complexType>
++ *         </element>
++ *         <element name="Users">
++ *           <complexType>
++ *             <complexContent>
++ *               <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
++ *                 <sequence maxOccurs="unbounded" minOccurs="0">
++ *                   <element ref="{http://javaops.ru}User"/>
++ *                 </sequence>
++ *               </restriction>
++ *             </complexContent>
++ *           </complexType>
++ *         </element>
++ *       </all>
++ *     </restriction>
++ *   </complexContent>
++ * </complexType>
++ * 
++ * ++ * ++ */ ++@XmlAccessorType(XmlAccessType.FIELD) ++@XmlType(name = "", propOrder = { ++ ++}) ++@XmlRootElement(name = "Payload", namespace = "http://javaops.ru") ++public class Payload { ++ ++ @XmlElement(name = "Cities", namespace = "http://javaops.ru", required = true) ++ protected Payload.Cities cities; ++ @XmlElement(name = "Users", namespace = "http://javaops.ru", required = true) ++ protected Payload.Users users; ++ ++ /** ++ * Gets the value of the cities property. ++ * ++ * @return ++ * possible object is ++ * {@link Payload.Cities } ++ * ++ */ ++ public Payload.Cities getCities() { ++ return cities; ++ } ++ ++ /** ++ * Sets the value of the cities property. ++ * ++ * @param value ++ * allowed object is ++ * {@link Payload.Cities } ++ * ++ */ ++ public void setCities(Payload.Cities value) { ++ this.cities = value; ++ } ++ ++ /** ++ * Gets the value of the users property. ++ * ++ * @return ++ * possible object is ++ * {@link Payload.Users } ++ * ++ */ ++ public Payload.Users getUsers() { ++ return users; ++ } ++ ++ /** ++ * Sets the value of the users property. ++ * ++ * @param value ++ * allowed object is ++ * {@link Payload.Users } ++ * ++ */ ++ public void setUsers(Payload.Users value) { ++ this.users = value; ++ } ++ ++ ++ /** ++ *

Java class for anonymous complex type. ++ * ++ *

The following schema fragment specifies the expected content contained within this class. ++ * ++ *

++     * <complexType>
++     *   <complexContent>
++     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
++     *       <sequence maxOccurs="unbounded">
++     *         <element ref="{http://javaops.ru}City"/>
++     *       </sequence>
++     *     </restriction>
++     *   </complexContent>
++     * </complexType>
++     * 
++ * ++ * ++ */ ++ @XmlAccessorType(XmlAccessType.FIELD) ++ @XmlType(name = "", propOrder = { ++ "city" ++ }) ++ public static class Cities { ++ ++ @XmlElement(name = "City", namespace = "http://javaops.ru", required = true) ++ protected List city; ++ ++ /** ++ * Gets the value of the city property. ++ * ++ *

++ * This accessor method returns a reference to the live list, ++ * not a snapshot. Therefore any modification you make to the ++ * returned list will be present inside the JAXB object. ++ * This is why there is not a set method for the city property. ++ * ++ *

++ * For example, to add a new item, do as follows: ++ *

++         *    getCity().add(newItem);
++         * 
++ * ++ * ++ *

++ * Objects of the following type(s) are allowed in the list ++ * {@link CityType } ++ * ++ * ++ */ ++ public List getCity() { ++ if (city == null) { ++ city = new ArrayList(); ++ } ++ return this.city; ++ } ++ ++ } ++ ++ ++ /** ++ *

Java class for anonymous complex type. ++ * ++ *

The following schema fragment specifies the expected content contained within this class. ++ * ++ *

++     * <complexType>
++     *   <complexContent>
++     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
++     *       <sequence maxOccurs="unbounded" minOccurs="0">
++     *         <element ref="{http://javaops.ru}User"/>
++     *       </sequence>
++     *     </restriction>
++     *   </complexContent>
++     * </complexType>
++     * 
++ * ++ * ++ */ ++ @XmlAccessorType(XmlAccessType.FIELD) ++ @XmlType(name = "", propOrder = { ++ "user" ++ }) ++ public static class Users { ++ ++ @XmlElement(name = "User", namespace = "http://javaops.ru") ++ protected List user; ++ ++ /** ++ * Gets the value of the user property. ++ * ++ *

++ * This accessor method returns a reference to the live list, ++ * not a snapshot. Therefore any modification you make to the ++ * returned list will be present inside the JAXB object. ++ * This is why there is not a set method for the user property. ++ * ++ *

++ * For example, to add a new item, do as follows: ++ *

++         *    getUser().add(newItem);
++         * 
++ * ++ * ++ *

++ * Objects of the following type(s) are allowed in the list ++ * {@link User } ++ * ++ * ++ */ ++ public List getUser() { ++ if (user == null) { ++ user = new ArrayList(); ++ } ++ return this.user; ++ } ++ ++ } ++ ++} +Index: src/main/java/ru/javaops/masterjava/xml/schema/User.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/schema/User.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/schema/User.java (revision ) +@@ -0,0 +1,151 @@ ++ ++package ru.javaops.masterjava.xml.schema; ++ ++import javax.xml.bind.annotation.XmlAccessType; ++import javax.xml.bind.annotation.XmlAccessorType; ++import javax.xml.bind.annotation.XmlAttribute; ++import javax.xml.bind.annotation.XmlElement; ++import javax.xml.bind.annotation.XmlIDREF; ++import javax.xml.bind.annotation.XmlRootElement; ++import javax.xml.bind.annotation.XmlSchemaType; ++import javax.xml.bind.annotation.XmlType; ++ ++ ++/** ++ *

Java class for anonymous complex type. ++ * ++ *

The following schema fragment specifies the expected content contained within this class. ++ * ++ *

++ * <complexType>
++ *   <complexContent>
++ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
++ *       <sequence>
++ *         <element name="email" type="{http://www.w3.org/2001/XMLSchema}string"/>
++ *         <element name="fullName" type="{http://www.w3.org/2001/XMLSchema}string"/>
++ *       </sequence>
++ *       <attribute name="flag" use="required" type="{http://javaops.ru}flagType" />
++ *       <attribute name="city" use="required" type="{http://www.w3.org/2001/XMLSchema}IDREF" />
++ *     </restriction>
++ *   </complexContent>
++ * </complexType>
++ * 
++ * ++ * ++ */ ++@XmlAccessorType(XmlAccessType.FIELD) ++@XmlType(name = "", propOrder = { ++ "email", ++ "fullName" ++}) ++@XmlRootElement(name = "User", namespace = "http://javaops.ru") ++public class User { ++ ++ @XmlElement(namespace = "http://javaops.ru", required = true) ++ protected String email; ++ @XmlElement(namespace = "http://javaops.ru", required = true) ++ protected String fullName; ++ @XmlAttribute(name = "flag", required = true) ++ protected FlagType flag; ++ @XmlAttribute(name = "city", required = true) ++ @XmlIDREF ++ @XmlSchemaType(name = "IDREF") ++ protected Object city; ++ ++ /** ++ * Gets the value of the email property. ++ * ++ * @return ++ * possible object is ++ * {@link String } ++ * ++ */ ++ public String getEmail() { ++ return email; ++ } ++ ++ /** ++ * Sets the value of the email property. ++ * ++ * @param value ++ * allowed object is ++ * {@link String } ++ * ++ */ ++ public void setEmail(String value) { ++ this.email = value; ++ } ++ ++ /** ++ * Gets the value of the fullName property. ++ * ++ * @return ++ * possible object is ++ * {@link String } ++ * ++ */ ++ public String getFullName() { ++ return fullName; ++ } ++ ++ /** ++ * Sets the value of the fullName property. ++ * ++ * @param value ++ * allowed object is ++ * {@link String } ++ * ++ */ ++ public void setFullName(String value) { ++ this.fullName = value; ++ } ++ ++ /** ++ * Gets the value of the flag property. ++ * ++ * @return ++ * possible object is ++ * {@link FlagType } ++ * ++ */ ++ public FlagType getFlag() { ++ return flag; ++ } ++ ++ /** ++ * Sets the value of the flag property. ++ * ++ * @param value ++ * allowed object is ++ * {@link FlagType } ++ * ++ */ ++ public void setFlag(FlagType value) { ++ this.flag = value; ++ } ++ ++ /** ++ * Gets the value of the city property. ++ * ++ * @return ++ * possible object is ++ * {@link Object } ++ * ++ */ ++ public Object getCity() { ++ return city; ++ } ++ ++ /** ++ * Sets the value of the city property. ++ * ++ * @param value ++ * allowed object is ++ * {@link Object } ++ * ++ */ ++ public void setCity(Object value) { ++ this.city = value; ++ } ++ ++} +Index: src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java (revision ) +@@ -0,0 +1,54 @@ ++ ++package ru.javaops.masterjava.xml.schema; ++ ++import javax.xml.bind.annotation.XmlEnum; ++import javax.xml.bind.annotation.XmlEnumValue; ++import javax.xml.bind.annotation.XmlType; ++ ++ ++/** ++ *

Java class for flagType. ++ * ++ *

The following schema fragment specifies the expected content contained within this class. ++ *

++ *

++ * <simpleType name="flagType">
++ *   <restriction base="{http://www.w3.org/2001/XMLSchema}string">
++ *     <enumeration value="active"/>
++ *     <enumeration value="deleted"/>
++ *     <enumeration value="superuser"/>
++ *   </restriction>
++ * </simpleType>
++ * 
++ * ++ */ ++@XmlType(name = "flagType", namespace = "http://javaops.ru") ++@XmlEnum ++public enum FlagType { ++ ++ @XmlEnumValue("active") ++ ACTIVE("active"), ++ @XmlEnumValue("deleted") ++ DELETED("deleted"), ++ @XmlEnumValue("superuser") ++ SUPERUSER("superuser"); ++ private final String value; ++ ++ FlagType(String v) { ++ value = v; ++ } ++ ++ public String value() { ++ return value; ++ } ++ ++ public static FlagType fromValue(String v) { ++ for (FlagType c: FlagType.values()) { ++ if (c.value.equals(v)) { ++ return c; ++ } ++ } ++ throw new IllegalArgumentException(v); ++ } ++ ++} +Index: src/test/resources/payload.xml +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/test/resources/payload.xml (revision ) ++++ src/test/resources/payload.xml (revision ) +@@ -0,0 +1,23 @@ ++ ++ ++ ++ gmail@gmail.com ++ Full Name ++ ++ ++ admin@javaops.ru ++ Admin ++ ++ ++ mail@yandex.ru ++ Deleted ++ ++ ++ ++ Санкт-Петербург ++ Киев ++ Минск ++ ++ +\ No newline at end of file +Index: src/main/java/ru/javaops/masterjava/xml/schema/CityType.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/schema/CityType.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/schema/CityType.java (revision ) +@@ -0,0 +1,94 @@ ++ ++package ru.javaops.masterjava.xml.schema; ++ ++import javax.xml.bind.annotation.XmlAccessType; ++import javax.xml.bind.annotation.XmlAccessorType; ++import javax.xml.bind.annotation.XmlAttribute; ++import javax.xml.bind.annotation.XmlID; ++import javax.xml.bind.annotation.XmlSchemaType; ++import javax.xml.bind.annotation.XmlType; ++import javax.xml.bind.annotation.XmlValue; ++import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; ++import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; ++ ++ ++/** ++ *

Java class for cityType complex type. ++ * ++ *

The following schema fragment specifies the expected content contained within this class. ++ * ++ *

++ * <complexType name="cityType">
++ *   <simpleContent>
++ *     <extension base="<http://www.w3.org/2001/XMLSchema>string">
++ *       <attribute name="id" use="required" type="{http://www.w3.org/2001/XMLSchema}ID" />
++ *     </extension>
++ *   </simpleContent>
++ * </complexType>
++ * 
++ * ++ * ++ */ ++@XmlAccessorType(XmlAccessType.FIELD) ++@XmlType(name = "cityType", namespace = "http://javaops.ru", propOrder = { ++ "value" ++}) ++public class CityType { ++ ++ @XmlValue ++ protected String value; ++ @XmlAttribute(name = "id", required = true) ++ @XmlJavaTypeAdapter(CollapsedStringAdapter.class) ++ @XmlID ++ @XmlSchemaType(name = "ID") ++ protected String id; ++ ++ /** ++ * Gets the value of the value property. ++ * ++ * @return ++ * possible object is ++ * {@link String } ++ * ++ */ ++ public String getValue() { ++ return value; ++ } ++ ++ /** ++ * Sets the value of the value property. ++ * ++ * @param value ++ * allowed object is ++ * {@link String } ++ * ++ */ ++ public void setValue(String value) { ++ this.value = value; ++ } ++ ++ /** ++ * Gets the value of the id property. ++ * ++ * @return ++ * possible object is ++ * {@link String } ++ * ++ */ ++ public String getId() { ++ return id; ++ } ++ ++ /** ++ * Sets the value of the id property. ++ * ++ * @param value ++ * allowed object is ++ * {@link String } ++ * ++ */ ++ public void setId(String value) { ++ this.id = value; ++ } ++ ++} +Index: src/main/resources/payload.xsd +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/resources/payload.xsd (revision ) ++++ src/main/resources/payload.xsd (revision ) +@@ -0,0 +1,56 @@ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ ++ +\ No newline at end of file diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/CityType.java b/src/main/java/ru/javaops/masterjava/xml/schema/CityType.java new file mode 100644 index 000000000..029e352cb --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/schema/CityType.java @@ -0,0 +1,94 @@ + +package ru.javaops.masterjava.xml.schema; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlID; +import javax.xml.bind.annotation.XmlSchemaType; +import javax.xml.bind.annotation.XmlType; +import javax.xml.bind.annotation.XmlValue; +import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; + + +/** + *

Java class for cityType complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType name="cityType">
+ *   <simpleContent>
+ *     <extension base="<http://www.w3.org/2001/XMLSchema>string">
+ *       <attribute name="id" use="required" type="{http://www.w3.org/2001/XMLSchema}ID" />
+ *     </extension>
+ *   </simpleContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "cityType", namespace = "http://javaops.ru", propOrder = { + "value" +}) +public class CityType { + + @XmlValue + protected String value; + @XmlAttribute(name = "id", required = true) + @XmlJavaTypeAdapter(CollapsedStringAdapter.class) + @XmlID + @XmlSchemaType(name = "ID") + protected String id; + + /** + * Gets the value of the value property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getValue() { + return value; + } + + /** + * Sets the value of the value property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setValue(String value) { + this.value = value; + } + + /** + * Gets the value of the id property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getId() { + return id; + } + + /** + * Sets the value of the id property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setId(String value) { + this.id = value; + } + +} diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java b/src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java new file mode 100644 index 000000000..eda39fa9a --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java @@ -0,0 +1,54 @@ + +package ru.javaops.masterjava.xml.schema; + +import javax.xml.bind.annotation.XmlEnum; +import javax.xml.bind.annotation.XmlEnumValue; +import javax.xml.bind.annotation.XmlType; + + +/** + *

Java class for flagType. + * + *

The following schema fragment specifies the expected content contained within this class. + *

+ *

+ * <simpleType name="flagType">
+ *   <restriction base="{http://www.w3.org/2001/XMLSchema}string">
+ *     <enumeration value="active"/>
+ *     <enumeration value="deleted"/>
+ *     <enumeration value="superuser"/>
+ *   </restriction>
+ * </simpleType>
+ * 
+ * + */ +@XmlType(name = "flagType", namespace = "http://javaops.ru") +@XmlEnum +public enum FlagType { + + @XmlEnumValue("active") + ACTIVE("active"), + @XmlEnumValue("deleted") + DELETED("deleted"), + @XmlEnumValue("superuser") + SUPERUSER("superuser"); + private final String value; + + FlagType(String v) { + value = v; + } + + public String value() { + return value; + } + + public static FlagType fromValue(String v) { + for (FlagType c: FlagType.values()) { + if (c.value.equals(v)) { + return c; + } + } + throw new IllegalArgumentException(v); + } + +} diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java b/src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java new file mode 100644 index 000000000..e8f105e2a --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java @@ -0,0 +1,85 @@ + +package ru.javaops.masterjava.xml.schema; + +import javax.xml.bind.JAXBElement; +import javax.xml.bind.annotation.XmlElementDecl; +import javax.xml.bind.annotation.XmlRegistry; +import javax.xml.namespace.QName; + + +/** + * This object contains factory methods for each + * Java content interface and Java element interface + * generated in the ru.javaops.masterjava.xml.schema package. + *

An ObjectFactory allows you to programatically + * construct new instances of the Java representation + * for XML content. The Java representation of XML + * content can consist of schema derived interfaces + * and classes representing the binding of schema + * type definitions, element declarations and model + * groups. Factory methods for each of these are + * provided in this class. + * + */ +@XmlRegistry +public class ObjectFactory { + + private final static QName _City_QNAME = new QName("http://javaops.ru", "City"); + + /** + * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: ru.javaops.masterjava.xml.schema + * + */ + public ObjectFactory() { + } + + /** + * Create an instance of {@link Payload } + * + */ + public Payload createPayload() { + return new Payload(); + } + + /** + * Create an instance of {@link User } + * + */ + public User createUser() { + return new User(); + } + + /** + * Create an instance of {@link Payload.Cities } + * + */ + public Payload.Cities createPayloadCities() { + return new Payload.Cities(); + } + + /** + * Create an instance of {@link Payload.Users } + * + */ + public Payload.Users createPayloadUsers() { + return new Payload.Users(); + } + + /** + * Create an instance of {@link CityType } + * + */ + public CityType createCityType() { + return new CityType(); + } + + /** + * Create an instance of {@link JAXBElement }{@code <}{@link CityType }{@code >}} + * + */ + @XmlElementDecl(namespace = "http://javaops.ru", name = "City") + public JAXBElement createCity(CityType value) { + return new JAXBElement(_City_QNAME, CityType.class, null, value); + } + +} diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/Payload.java b/src/main/java/ru/javaops/masterjava/xml/schema/Payload.java new file mode 100644 index 000000000..2a6276490 --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/schema/Payload.java @@ -0,0 +1,233 @@ + +package ru.javaops.masterjava.xml.schema; + +import java.util.ArrayList; +import java.util.List; +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlType; + + +/** + *

Java class for anonymous complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType>
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <all>
+ *         <element name="Cities">
+ *           <complexType>
+ *             <complexContent>
+ *               <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *                 <sequence maxOccurs="unbounded">
+ *                   <element ref="{http://javaops.ru}City"/>
+ *                 </sequence>
+ *               </restriction>
+ *             </complexContent>
+ *           </complexType>
+ *         </element>
+ *         <element name="Users">
+ *           <complexType>
+ *             <complexContent>
+ *               <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *                 <sequence maxOccurs="unbounded" minOccurs="0">
+ *                   <element ref="{http://javaops.ru}User"/>
+ *                 </sequence>
+ *               </restriction>
+ *             </complexContent>
+ *           </complexType>
+ *         </element>
+ *       </all>
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "", propOrder = { + +}) +@XmlRootElement(name = "Payload", namespace = "http://javaops.ru") +public class Payload { + + @XmlElement(name = "Cities", namespace = "http://javaops.ru", required = true) + protected Payload.Cities cities; + @XmlElement(name = "Users", namespace = "http://javaops.ru", required = true) + protected Payload.Users users; + + /** + * Gets the value of the cities property. + * + * @return + * possible object is + * {@link Payload.Cities } + * + */ + public Payload.Cities getCities() { + return cities; + } + + /** + * Sets the value of the cities property. + * + * @param value + * allowed object is + * {@link Payload.Cities } + * + */ + public void setCities(Payload.Cities value) { + this.cities = value; + } + + /** + * Gets the value of the users property. + * + * @return + * possible object is + * {@link Payload.Users } + * + */ + public Payload.Users getUsers() { + return users; + } + + /** + * Sets the value of the users property. + * + * @param value + * allowed object is + * {@link Payload.Users } + * + */ + public void setUsers(Payload.Users value) { + this.users = value; + } + + + /** + *

Java class for anonymous complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+     * <complexType>
+     *   <complexContent>
+     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+     *       <sequence maxOccurs="unbounded">
+     *         <element ref="{http://javaops.ru}City"/>
+     *       </sequence>
+     *     </restriction>
+     *   </complexContent>
+     * </complexType>
+     * 
+ * + * + */ + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(name = "", propOrder = { + "city" + }) + public static class Cities { + + @XmlElement(name = "City", namespace = "http://javaops.ru", required = true) + protected List city; + + /** + * Gets the value of the city property. + * + *

+ * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a set method for the city property. + * + *

+ * For example, to add a new item, do as follows: + *

+         *    getCity().add(newItem);
+         * 
+ * + * + *

+ * Objects of the following type(s) are allowed in the list + * {@link CityType } + * + * + */ + public List getCity() { + if (city == null) { + city = new ArrayList(); + } + return this.city; + } + + } + + + /** + *

Java class for anonymous complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+     * <complexType>
+     *   <complexContent>
+     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+     *       <sequence maxOccurs="unbounded" minOccurs="0">
+     *         <element ref="{http://javaops.ru}User"/>
+     *       </sequence>
+     *     </restriction>
+     *   </complexContent>
+     * </complexType>
+     * 
+ * + * + */ + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(name = "", propOrder = { + "user" + }) + public static class Users { + + @XmlElement(name = "User", namespace = "http://javaops.ru") + protected List user; + + /** + * Gets the value of the user property. + * + *

+ * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a set method for the user property. + * + *

+ * For example, to add a new item, do as follows: + *

+         *    getUser().add(newItem);
+         * 
+ * + * + *

+ * Objects of the following type(s) are allowed in the list + * {@link User } + * + * + */ + public List getUser() { + if (user == null) { + user = new ArrayList(); + } + return this.user; + } + + } + +} diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/User.java b/src/main/java/ru/javaops/masterjava/xml/schema/User.java new file mode 100644 index 000000000..b3430ce71 --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/schema/User.java @@ -0,0 +1,151 @@ + +package ru.javaops.masterjava.xml.schema; + +import javax.xml.bind.annotation.XmlAccessType; +import javax.xml.bind.annotation.XmlAccessorType; +import javax.xml.bind.annotation.XmlAttribute; +import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlIDREF; +import javax.xml.bind.annotation.XmlRootElement; +import javax.xml.bind.annotation.XmlSchemaType; +import javax.xml.bind.annotation.XmlType; + + +/** + *

Java class for anonymous complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType>
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="email" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *         <element name="fullName" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *       </sequence>
+ *       <attribute name="flag" use="required" type="{http://javaops.ru}flagType" />
+ *       <attribute name="city" use="required" type="{http://www.w3.org/2001/XMLSchema}IDREF" />
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "", propOrder = { + "email", + "fullName" +}) +@XmlRootElement(name = "User", namespace = "http://javaops.ru") +public class User { + + @XmlElement(namespace = "http://javaops.ru", required = true) + protected String email; + @XmlElement(namespace = "http://javaops.ru", required = true) + protected String fullName; + @XmlAttribute(name = "flag", required = true) + protected FlagType flag; + @XmlAttribute(name = "city", required = true) + @XmlIDREF + @XmlSchemaType(name = "IDREF") + protected Object city; + + /** + * Gets the value of the email property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getEmail() { + return email; + } + + /** + * Sets the value of the email property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setEmail(String value) { + this.email = value; + } + + /** + * Gets the value of the fullName property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getFullName() { + return fullName; + } + + /** + * Sets the value of the fullName property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setFullName(String value) { + this.fullName = value; + } + + /** + * Gets the value of the flag property. + * + * @return + * possible object is + * {@link FlagType } + * + */ + public FlagType getFlag() { + return flag; + } + + /** + * Sets the value of the flag property. + * + * @param value + * allowed object is + * {@link FlagType } + * + */ + public void setFlag(FlagType value) { + this.flag = value; + } + + /** + * Gets the value of the city property. + * + * @return + * possible object is + * {@link Object } + * + */ + public Object getCity() { + return city; + } + + /** + * Sets the value of the city property. + * + * @param value + * allowed object is + * {@link Object } + * + */ + public void setCity(Object value) { + this.city = value; + } + +} diff --git a/src/main/resources/payload.xsd b/src/main/resources/payload.xsd new file mode 100644 index 000000000..9ef1e46eb --- /dev/null +++ b/src/main/resources/payload.xsd @@ -0,0 +1,56 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/src/test/resources/payload.xml b/src/test/resources/payload.xml new file mode 100644 index 000000000..796e99cb3 --- /dev/null +++ b/src/test/resources/payload.xml @@ -0,0 +1,23 @@ + + + + gmail@gmail.com + Full Name + + + admin@javaops.ru + Admin + + + mail@yandex.ru + Deleted + + + + Санкт-Петербург + Киев + Минск + + \ No newline at end of file From ca219363addea480d37331954b172cb815df3e0e Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Thu, 20 Sep 2018 14:06:53 +0500 Subject: [PATCH 14/20] 2_07_JAXB.patch --- 2_07_JAXB.patch | 342 ++++++++++++++++++ pom.xml | 19 + .../masterjava/xml/util/JaxbMarshaller.java | 39 ++ .../masterjava/xml/util/JaxbParser.java | 88 +++++ .../masterjava/xml/util/JaxbUnmarshaller.java | 33 ++ .../javaops/masterjava/xml/util/Schemas.java | 48 +++ .../masterjava/xml/util/JaxbParserTest.java | 40 ++ src/test/resources/city.xml | 4 + 8 files changed, 613 insertions(+) create mode 100644 2_07_JAXB.patch create mode 100644 src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java create mode 100644 src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java create mode 100644 src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java create mode 100644 src/main/java/ru/javaops/masterjava/xml/util/Schemas.java create mode 100644 src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java create mode 100644 src/test/resources/city.xml diff --git a/2_07_JAXB.patch b/2_07_JAXB.patch new file mode 100644 index 000000000..5dfafecc9 --- /dev/null +++ b/2_07_JAXB.patch @@ -0,0 +1,342 @@ +Index: src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java (revision ) +@@ -0,0 +1,39 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import javax.xml.bind.JAXBContext; ++import javax.xml.bind.JAXBException; ++import javax.xml.bind.Marshaller; ++import javax.xml.bind.PropertyException; ++import javax.xml.validation.Schema; ++import java.io.StringWriter; ++import java.io.Writer; ++ ++public class JaxbMarshaller { ++ private Marshaller marshaller; ++ ++ public JaxbMarshaller(JAXBContext ctx) throws JAXBException { ++ marshaller = ctx.createMarshaller(); ++ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); ++ marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); ++ marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); ++ } ++ ++ public void setProperty(String prop, Object value) throws PropertyException { ++ marshaller.setProperty(prop, value); ++ } ++ ++ public synchronized void setSchema(Schema schema) { ++ marshaller.setSchema(schema); ++ } ++ ++ public String marshal(Object instance) throws JAXBException { ++ StringWriter sw = new StringWriter(); ++ marshal(instance, sw); ++ return sw.toString(); ++ } ++ ++ public synchronized void marshal(Object instance, Writer writer) throws JAXBException { ++ marshaller.marshal(instance, writer); ++ } ++ ++} +Index: src/main/java/ru/javaops/masterjava/xml/util/Schemas.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/util/Schemas.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/util/Schemas.java (revision ) +@@ -0,0 +1,48 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import com.google.common.io.Resources; ++import org.xml.sax.SAXException; ++ ++import javax.xml.XMLConstants; ++import javax.xml.transform.stream.StreamSource; ++import javax.xml.validation.Schema; ++import javax.xml.validation.SchemaFactory; ++import java.io.File; ++import java.io.StringReader; ++import java.net.URL; ++ ++ ++public class Schemas { ++ ++ // SchemaFactory is not thread-safe ++ private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); ++ ++ public static synchronized Schema ofString(String xsd) { ++ try { ++ return SCHEMA_FACTORY.newSchema(new StreamSource(new StringReader(xsd))); ++ } catch (SAXException e) { ++ throw new IllegalArgumentException(e); ++ } ++ } ++ ++ public static synchronized Schema ofClasspath(String resource) { ++ // http://digitalsanctum.com/2012/11/30/how-to-read-file-contents-in-java-the-easy-way-with-guava/ ++ return ofURL(Resources.getResource(resource)); ++ } ++ ++ public static synchronized Schema ofURL(URL url) { ++ try { ++ return SCHEMA_FACTORY.newSchema(url); ++ } catch (SAXException e) { ++ throw new IllegalArgumentException(e); ++ } ++ } ++ ++ public static synchronized Schema ofFile(File file) { ++ try { ++ return SCHEMA_FACTORY.newSchema(file); ++ } catch (SAXException e) { ++ throw new IllegalArgumentException(e); ++ } ++ } ++} +Index: src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java (revision ) ++++ src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java (revision ) +@@ -0,0 +1,40 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import com.google.common.io.Resources; ++import org.junit.Test; ++import ru.javaops.masterjava.xml.schema.CityType; ++import ru.javaops.masterjava.xml.schema.ObjectFactory; ++import ru.javaops.masterjava.xml.schema.Payload; ++ ++import javax.xml.bind.JAXBElement; ++import javax.xml.namespace.QName; ++ ++public class JaxbParserTest { ++ private static final JaxbParser JAXB_PARSER = new JaxbParser(ObjectFactory.class); ++ ++ static { ++ JAXB_PARSER.setSchema(Schemas.ofClasspath("payload.xsd")); ++ } ++ ++ @Test ++ public void testPayload() throws Exception { ++// JaxbParserTest.class.getResourceAsStream("/city.xml") ++ Payload payload = JAXB_PARSER.unmarshal( ++ Resources.getResource("payload.xml").openStream()); ++ String strPayload = JAXB_PARSER.marshal(payload); ++ JAXB_PARSER.validate(strPayload); ++ System.out.println(strPayload); ++ } ++ ++ @Test ++ public void testCity() throws Exception { ++ JAXBElement cityElement = JAXB_PARSER.unmarshal( ++ Resources.getResource("city.xml").openStream()); ++ CityType city = cityElement.getValue(); ++ JAXBElement cityElement2 = ++ new JAXBElement<>(new QName("http://javaops.ru", "City"), CityType.class, city); ++ String strCity = JAXB_PARSER.marshal(cityElement2); ++ JAXB_PARSER.validate(strCity); ++ System.out.println(strCity); ++ } ++} +\ No newline at end of file +Index: src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java (revision ) +@@ -0,0 +1,88 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import org.xml.sax.SAXException; ++ ++import javax.xml.bind.JAXBContext; ++import javax.xml.bind.JAXBException; ++import javax.xml.bind.PropertyException; ++import javax.xml.transform.stream.StreamSource; ++import javax.xml.validation.Schema; ++import java.io.*; ++ ++ ++/** ++ * Marshalling/Unmarshalling JAXB helper ++ * XML Facade ++ */ ++public class JaxbParser { ++ ++ protected JaxbMarshaller jaxbMarshaller; ++ protected JaxbUnmarshaller jaxbUnmarshaller; ++ protected Schema schema; ++ ++ public JaxbParser(Class... classesToBeBound) { ++ try { ++ init(JAXBContext.newInstance(classesToBeBound)); ++ } catch (JAXBException e) { ++ throw new IllegalArgumentException(e); ++ } ++ } ++ ++ // http://stackoverflow.com/questions/30643802/what-is-jaxbcontext-newinstancestring-contextpath ++ public JaxbParser(String context) { ++ try { ++ init(JAXBContext.newInstance(context)); ++ } catch (JAXBException e) { ++ throw new IllegalArgumentException(e); ++ } ++ } ++ ++ private void init(JAXBContext ctx) throws JAXBException { ++ jaxbMarshaller = new JaxbMarshaller(ctx); ++ jaxbUnmarshaller = new JaxbUnmarshaller(ctx); ++ } ++ ++ // Unmarshaller ++ public T unmarshal(InputStream is) throws JAXBException { ++ return (T) jaxbUnmarshaller.unmarshal(is); ++ } ++ ++ public T unmarshal(Reader reader) throws JAXBException { ++ return (T) jaxbUnmarshaller.unmarshal(reader); ++ } ++ ++ public T unmarshal(String str) throws JAXBException { ++ return (T) jaxbUnmarshaller.unmarshal(str); ++ } ++ ++ // Marshaller ++ public void setMarshallerProperty(String prop, Object value) { ++ try { ++ jaxbMarshaller.setProperty(prop, value); ++ } catch (PropertyException e) { ++ throw new IllegalArgumentException(e); ++ } ++ } ++ ++ public String marshal(Object instance) throws JAXBException { ++ return jaxbMarshaller.marshal(instance); ++ } ++ ++ public void marshal(Object instance, Writer writer) throws JAXBException { ++ jaxbMarshaller.marshal(instance, writer); ++ } ++ ++ public void setSchema(Schema schema) { ++ this.schema = schema; ++ jaxbUnmarshaller.setSchema(schema); ++ jaxbMarshaller.setSchema(schema); ++ } ++ ++ public void validate(String str) throws IOException, SAXException { ++ validate(new StringReader(str)); ++ } ++ ++ public void validate(Reader reader) throws IOException, SAXException { ++ schema.newValidator().validate(new StreamSource(reader)); ++ } ++} +Index: src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java (revision ) +@@ -0,0 +1,33 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import javax.xml.bind.JAXBContext; ++import javax.xml.bind.JAXBException; ++import javax.xml.bind.Unmarshaller; ++import javax.xml.validation.Schema; ++import java.io.InputStream; ++import java.io.Reader; ++import java.io.StringReader; ++ ++public class JaxbUnmarshaller { ++ private Unmarshaller unmarshaller; ++ ++ public JaxbUnmarshaller(JAXBContext ctx) throws JAXBException { ++ unmarshaller = ctx.createUnmarshaller(); ++ } ++ ++ public synchronized void setSchema(Schema schema) { ++ unmarshaller.setSchema(schema); ++ } ++ ++ public synchronized Object unmarshal(InputStream is) throws JAXBException { ++ return unmarshaller.unmarshal(is); ++ } ++ ++ public synchronized Object unmarshal(Reader reader) throws JAXBException { ++ return unmarshaller.unmarshal(reader); ++ } ++ ++ public Object unmarshal(String str) throws JAXBException { ++ return unmarshal(new StringReader(str)); ++ } ++} +Index: src/test/resources/city.xml +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/test/resources/city.xml (revision ) ++++ src/test/resources/city.xml (revision ) +@@ -0,0 +1,4 @@ ++Санкт-Петербург ++ +\ No newline at end of file +Index: pom.xml +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- pom.xml (date 1508793189000) ++++ pom.xml (revision ) +@@ -32,6 +32,14 @@ +
+ + org.apache.maven.plugins ++ maven-surefire-plugin ++ 2.20.1 ++ ++ -Dfile.encoding=UTF-8 ++ ++ ++ ++ org.apache.maven.plugins + maven-shade-plugin + 3.1.0 + +@@ -79,6 +87,17 @@ + jmh-generator-annprocess + RELEASE + provided ++ ++ ++ com.google.guava ++ guava ++ 21.0 ++ ++ ++ junit ++ junit ++ 4.12 ++ test + + + diff --git a/pom.xml b/pom.xml index 4cadaf85e..884be4839 100644 --- a/pom.xml +++ b/pom.xml @@ -30,6 +30,14 @@ ${java.version}
+ + org.apache.maven.plugins + maven-surefire-plugin + 2.20.1 + + -Dfile.encoding=UTF-8 + + org.apache.maven.plugins maven-shade-plugin @@ -80,6 +88,17 @@ RELEASE provided + + com.google.guava + guava + 21.0 + + + junit + junit + 4.12 + test + diff --git a/src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java b/src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java new file mode 100644 index 000000000..d6006800f --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java @@ -0,0 +1,39 @@ +package ru.javaops.masterjava.xml.util; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Marshaller; +import javax.xml.bind.PropertyException; +import javax.xml.validation.Schema; +import java.io.StringWriter; +import java.io.Writer; + +public class JaxbMarshaller { + private Marshaller marshaller; + + public JaxbMarshaller(JAXBContext ctx) throws JAXBException { + marshaller = ctx.createMarshaller(); + marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); + marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); + marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); + } + + public void setProperty(String prop, Object value) throws PropertyException { + marshaller.setProperty(prop, value); + } + + public synchronized void setSchema(Schema schema) { + marshaller.setSchema(schema); + } + + public String marshal(Object instance) throws JAXBException { + StringWriter sw = new StringWriter(); + marshal(instance, sw); + return sw.toString(); + } + + public synchronized void marshal(Object instance, Writer writer) throws JAXBException { + marshaller.marshal(instance, writer); + } + +} diff --git a/src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java b/src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java new file mode 100644 index 000000000..b3a45f66c --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java @@ -0,0 +1,88 @@ +package ru.javaops.masterjava.xml.util; + +import org.xml.sax.SAXException; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.PropertyException; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import java.io.*; + + +/** + * Marshalling/Unmarshalling JAXB helper + * XML Facade + */ +public class JaxbParser { + + protected JaxbMarshaller jaxbMarshaller; + protected JaxbUnmarshaller jaxbUnmarshaller; + protected Schema schema; + + public JaxbParser(Class... classesToBeBound) { + try { + init(JAXBContext.newInstance(classesToBeBound)); + } catch (JAXBException e) { + throw new IllegalArgumentException(e); + } + } + + // http://stackoverflow.com/questions/30643802/what-is-jaxbcontext-newinstancestring-contextpath + public JaxbParser(String context) { + try { + init(JAXBContext.newInstance(context)); + } catch (JAXBException e) { + throw new IllegalArgumentException(e); + } + } + + private void init(JAXBContext ctx) throws JAXBException { + jaxbMarshaller = new JaxbMarshaller(ctx); + jaxbUnmarshaller = new JaxbUnmarshaller(ctx); + } + + // Unmarshaller + public T unmarshal(InputStream is) throws JAXBException { + return (T) jaxbUnmarshaller.unmarshal(is); + } + + public T unmarshal(Reader reader) throws JAXBException { + return (T) jaxbUnmarshaller.unmarshal(reader); + } + + public T unmarshal(String str) throws JAXBException { + return (T) jaxbUnmarshaller.unmarshal(str); + } + + // Marshaller + public void setMarshallerProperty(String prop, Object value) { + try { + jaxbMarshaller.setProperty(prop, value); + } catch (PropertyException e) { + throw new IllegalArgumentException(e); + } + } + + public String marshal(Object instance) throws JAXBException { + return jaxbMarshaller.marshal(instance); + } + + public void marshal(Object instance, Writer writer) throws JAXBException { + jaxbMarshaller.marshal(instance, writer); + } + + public void setSchema(Schema schema) { + this.schema = schema; + jaxbUnmarshaller.setSchema(schema); + jaxbMarshaller.setSchema(schema); + } + + public void validate(String str) throws IOException, SAXException { + validate(new StringReader(str)); + } + + public void validate(Reader reader) throws IOException, SAXException { + schema.newValidator().validate(new StreamSource(reader)); + } +} diff --git a/src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java b/src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java new file mode 100644 index 000000000..7a3e13461 --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java @@ -0,0 +1,33 @@ +package ru.javaops.masterjava.xml.util; + +import javax.xml.bind.JAXBContext; +import javax.xml.bind.JAXBException; +import javax.xml.bind.Unmarshaller; +import javax.xml.validation.Schema; +import java.io.InputStream; +import java.io.Reader; +import java.io.StringReader; + +public class JaxbUnmarshaller { + private Unmarshaller unmarshaller; + + public JaxbUnmarshaller(JAXBContext ctx) throws JAXBException { + unmarshaller = ctx.createUnmarshaller(); + } + + public synchronized void setSchema(Schema schema) { + unmarshaller.setSchema(schema); + } + + public synchronized Object unmarshal(InputStream is) throws JAXBException { + return unmarshaller.unmarshal(is); + } + + public synchronized Object unmarshal(Reader reader) throws JAXBException { + return unmarshaller.unmarshal(reader); + } + + public Object unmarshal(String str) throws JAXBException { + return unmarshal(new StringReader(str)); + } +} diff --git a/src/main/java/ru/javaops/masterjava/xml/util/Schemas.java b/src/main/java/ru/javaops/masterjava/xml/util/Schemas.java new file mode 100644 index 000000000..42f41df80 --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/util/Schemas.java @@ -0,0 +1,48 @@ +package ru.javaops.masterjava.xml.util; + +import com.google.common.io.Resources; +import org.xml.sax.SAXException; + +import javax.xml.XMLConstants; +import javax.xml.transform.stream.StreamSource; +import javax.xml.validation.Schema; +import javax.xml.validation.SchemaFactory; +import java.io.File; +import java.io.StringReader; +import java.net.URL; + + +public class Schemas { + + // SchemaFactory is not thread-safe + private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); + + public static synchronized Schema ofString(String xsd) { + try { + return SCHEMA_FACTORY.newSchema(new StreamSource(new StringReader(xsd))); + } catch (SAXException e) { + throw new IllegalArgumentException(e); + } + } + + public static synchronized Schema ofClasspath(String resource) { + // http://digitalsanctum.com/2012/11/30/how-to-read-file-contents-in-java-the-easy-way-with-guava/ + return ofURL(Resources.getResource(resource)); + } + + public static synchronized Schema ofURL(URL url) { + try { + return SCHEMA_FACTORY.newSchema(url); + } catch (SAXException e) { + throw new IllegalArgumentException(e); + } + } + + public static synchronized Schema ofFile(File file) { + try { + return SCHEMA_FACTORY.newSchema(file); + } catch (SAXException e) { + throw new IllegalArgumentException(e); + } + } +} diff --git a/src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java b/src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java new file mode 100644 index 000000000..623265428 --- /dev/null +++ b/src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java @@ -0,0 +1,40 @@ +package ru.javaops.masterjava.xml.util; + +import com.google.common.io.Resources; +import org.junit.Test; +import ru.javaops.masterjava.xml.schema.CityType; +import ru.javaops.masterjava.xml.schema.ObjectFactory; +import ru.javaops.masterjava.xml.schema.Payload; + +import javax.xml.bind.JAXBElement; +import javax.xml.namespace.QName; + +public class JaxbParserTest { + private static final JaxbParser JAXB_PARSER = new JaxbParser(ObjectFactory.class); + + static { + JAXB_PARSER.setSchema(Schemas.ofClasspath("payload.xsd")); + } + + @Test + public void testPayload() throws Exception { +// JaxbParserTest.class.getResourceAsStream("/city.xml") + Payload payload = JAXB_PARSER.unmarshal( + Resources.getResource("payload.xml").openStream()); + String strPayload = JAXB_PARSER.marshal(payload); + JAXB_PARSER.validate(strPayload); + System.out.println(strPayload); + } + + @Test + public void testCity() throws Exception { + JAXBElement cityElement = JAXB_PARSER.unmarshal( + Resources.getResource("city.xml").openStream()); + CityType city = cityElement.getValue(); + JAXBElement cityElement2 = + new JAXBElement<>(new QName("http://javaops.ru", "City"), CityType.class, city); + String strCity = JAXB_PARSER.marshal(cityElement2); + JAXB_PARSER.validate(strCity); + System.out.println(strCity); + } +} \ No newline at end of file diff --git a/src/test/resources/city.xml b/src/test/resources/city.xml new file mode 100644 index 000000000..8b0abcf8a --- /dev/null +++ b/src/test/resources/city.xml @@ -0,0 +1,4 @@ +Санкт-Петербург + \ No newline at end of file From 98b48f0922e131e90488b1af94b9ca59eed3eb3a Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Thu, 20 Sep 2018 16:09:04 +0500 Subject: [PATCH 15/20] 2_08_StAX.patch --- 2_06_xml_scheme.patch | 754 ------------------ 2_07_JAXB.patch | 342 -------- 2_08_StAX.patch | 109 +++ .../xml/util/StaxStreamProcessor.java | 56 ++ .../xml/util/StaxStreamProcessorTest.java | 36 + 5 files changed, 201 insertions(+), 1096 deletions(-) delete mode 100644 2_06_xml_scheme.patch delete mode 100644 2_07_JAXB.patch create mode 100644 2_08_StAX.patch create mode 100644 src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java create mode 100644 src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java diff --git a/2_06_xml_scheme.patch b/2_06_xml_scheme.patch deleted file mode 100644 index 9650668ee..000000000 --- a/2_06_xml_scheme.patch +++ /dev/null @@ -1,754 +0,0 @@ -Index: src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java (revision ) -@@ -0,0 +1,85 @@ -+ -+package ru.javaops.masterjava.xml.schema; -+ -+import javax.xml.bind.JAXBElement; -+import javax.xml.bind.annotation.XmlElementDecl; -+import javax.xml.bind.annotation.XmlRegistry; -+import javax.xml.namespace.QName; -+ -+ -+/** -+ * This object contains factory methods for each -+ * Java content interface and Java element interface -+ * generated in the ru.javaops.masterjava.xml.schema package. -+ *

An ObjectFactory allows you to programatically -+ * construct new instances of the Java representation -+ * for XML content. The Java representation of XML -+ * content can consist of schema derived interfaces -+ * and classes representing the binding of schema -+ * type definitions, element declarations and model -+ * groups. Factory methods for each of these are -+ * provided in this class. -+ * -+ */ -+@XmlRegistry -+public class ObjectFactory { -+ -+ private final static QName _City_QNAME = new QName("http://javaops.ru", "City"); -+ -+ /** -+ * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: ru.javaops.masterjava.xml.schema -+ * -+ */ -+ public ObjectFactory() { -+ } -+ -+ /** -+ * Create an instance of {@link Payload } -+ * -+ */ -+ public Payload createPayload() { -+ return new Payload(); -+ } -+ -+ /** -+ * Create an instance of {@link User } -+ * -+ */ -+ public User createUser() { -+ return new User(); -+ } -+ -+ /** -+ * Create an instance of {@link Payload.Cities } -+ * -+ */ -+ public Payload.Cities createPayloadCities() { -+ return new Payload.Cities(); -+ } -+ -+ /** -+ * Create an instance of {@link Payload.Users } -+ * -+ */ -+ public Payload.Users createPayloadUsers() { -+ return new Payload.Users(); -+ } -+ -+ /** -+ * Create an instance of {@link CityType } -+ * -+ */ -+ public CityType createCityType() { -+ return new CityType(); -+ } -+ -+ /** -+ * Create an instance of {@link JAXBElement }{@code <}{@link CityType }{@code >}} -+ * -+ */ -+ @XmlElementDecl(namespace = "http://javaops.ru", name = "City") -+ public JAXBElement createCity(CityType value) { -+ return new JAXBElement(_City_QNAME, CityType.class, null, value); -+ } -+ -+} -Index: src/main/java/ru/javaops/masterjava/xml/schema/Payload.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/schema/Payload.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/schema/Payload.java (revision ) -@@ -0,0 +1,233 @@ -+ -+package ru.javaops.masterjava.xml.schema; -+ -+import java.util.ArrayList; -+import java.util.List; -+import javax.xml.bind.annotation.XmlAccessType; -+import javax.xml.bind.annotation.XmlAccessorType; -+import javax.xml.bind.annotation.XmlElement; -+import javax.xml.bind.annotation.XmlRootElement; -+import javax.xml.bind.annotation.XmlType; -+ -+ -+/** -+ *

Java class for anonymous complex type. -+ * -+ *

The following schema fragment specifies the expected content contained within this class. -+ * -+ *

-+ * <complexType>
-+ *   <complexContent>
-+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
-+ *       <all>
-+ *         <element name="Cities">
-+ *           <complexType>
-+ *             <complexContent>
-+ *               <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
-+ *                 <sequence maxOccurs="unbounded">
-+ *                   <element ref="{http://javaops.ru}City"/>
-+ *                 </sequence>
-+ *               </restriction>
-+ *             </complexContent>
-+ *           </complexType>
-+ *         </element>
-+ *         <element name="Users">
-+ *           <complexType>
-+ *             <complexContent>
-+ *               <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
-+ *                 <sequence maxOccurs="unbounded" minOccurs="0">
-+ *                   <element ref="{http://javaops.ru}User"/>
-+ *                 </sequence>
-+ *               </restriction>
-+ *             </complexContent>
-+ *           </complexType>
-+ *         </element>
-+ *       </all>
-+ *     </restriction>
-+ *   </complexContent>
-+ * </complexType>
-+ * 
-+ * -+ * -+ */ -+@XmlAccessorType(XmlAccessType.FIELD) -+@XmlType(name = "", propOrder = { -+ -+}) -+@XmlRootElement(name = "Payload", namespace = "http://javaops.ru") -+public class Payload { -+ -+ @XmlElement(name = "Cities", namespace = "http://javaops.ru", required = true) -+ protected Payload.Cities cities; -+ @XmlElement(name = "Users", namespace = "http://javaops.ru", required = true) -+ protected Payload.Users users; -+ -+ /** -+ * Gets the value of the cities property. -+ * -+ * @return -+ * possible object is -+ * {@link Payload.Cities } -+ * -+ */ -+ public Payload.Cities getCities() { -+ return cities; -+ } -+ -+ /** -+ * Sets the value of the cities property. -+ * -+ * @param value -+ * allowed object is -+ * {@link Payload.Cities } -+ * -+ */ -+ public void setCities(Payload.Cities value) { -+ this.cities = value; -+ } -+ -+ /** -+ * Gets the value of the users property. -+ * -+ * @return -+ * possible object is -+ * {@link Payload.Users } -+ * -+ */ -+ public Payload.Users getUsers() { -+ return users; -+ } -+ -+ /** -+ * Sets the value of the users property. -+ * -+ * @param value -+ * allowed object is -+ * {@link Payload.Users } -+ * -+ */ -+ public void setUsers(Payload.Users value) { -+ this.users = value; -+ } -+ -+ -+ /** -+ *

Java class for anonymous complex type. -+ * -+ *

The following schema fragment specifies the expected content contained within this class. -+ * -+ *

-+     * <complexType>
-+     *   <complexContent>
-+     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
-+     *       <sequence maxOccurs="unbounded">
-+     *         <element ref="{http://javaops.ru}City"/>
-+     *       </sequence>
-+     *     </restriction>
-+     *   </complexContent>
-+     * </complexType>
-+     * 
-+ * -+ * -+ */ -+ @XmlAccessorType(XmlAccessType.FIELD) -+ @XmlType(name = "", propOrder = { -+ "city" -+ }) -+ public static class Cities { -+ -+ @XmlElement(name = "City", namespace = "http://javaops.ru", required = true) -+ protected List city; -+ -+ /** -+ * Gets the value of the city property. -+ * -+ *

-+ * This accessor method returns a reference to the live list, -+ * not a snapshot. Therefore any modification you make to the -+ * returned list will be present inside the JAXB object. -+ * This is why there is not a set method for the city property. -+ * -+ *

-+ * For example, to add a new item, do as follows: -+ *

-+         *    getCity().add(newItem);
-+         * 
-+ * -+ * -+ *

-+ * Objects of the following type(s) are allowed in the list -+ * {@link CityType } -+ * -+ * -+ */ -+ public List getCity() { -+ if (city == null) { -+ city = new ArrayList(); -+ } -+ return this.city; -+ } -+ -+ } -+ -+ -+ /** -+ *

Java class for anonymous complex type. -+ * -+ *

The following schema fragment specifies the expected content contained within this class. -+ * -+ *

-+     * <complexType>
-+     *   <complexContent>
-+     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
-+     *       <sequence maxOccurs="unbounded" minOccurs="0">
-+     *         <element ref="{http://javaops.ru}User"/>
-+     *       </sequence>
-+     *     </restriction>
-+     *   </complexContent>
-+     * </complexType>
-+     * 
-+ * -+ * -+ */ -+ @XmlAccessorType(XmlAccessType.FIELD) -+ @XmlType(name = "", propOrder = { -+ "user" -+ }) -+ public static class Users { -+ -+ @XmlElement(name = "User", namespace = "http://javaops.ru") -+ protected List user; -+ -+ /** -+ * Gets the value of the user property. -+ * -+ *

-+ * This accessor method returns a reference to the live list, -+ * not a snapshot. Therefore any modification you make to the -+ * returned list will be present inside the JAXB object. -+ * This is why there is not a set method for the user property. -+ * -+ *

-+ * For example, to add a new item, do as follows: -+ *

-+         *    getUser().add(newItem);
-+         * 
-+ * -+ * -+ *

-+ * Objects of the following type(s) are allowed in the list -+ * {@link User } -+ * -+ * -+ */ -+ public List getUser() { -+ if (user == null) { -+ user = new ArrayList(); -+ } -+ return this.user; -+ } -+ -+ } -+ -+} -Index: src/main/java/ru/javaops/masterjava/xml/schema/User.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/schema/User.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/schema/User.java (revision ) -@@ -0,0 +1,151 @@ -+ -+package ru.javaops.masterjava.xml.schema; -+ -+import javax.xml.bind.annotation.XmlAccessType; -+import javax.xml.bind.annotation.XmlAccessorType; -+import javax.xml.bind.annotation.XmlAttribute; -+import javax.xml.bind.annotation.XmlElement; -+import javax.xml.bind.annotation.XmlIDREF; -+import javax.xml.bind.annotation.XmlRootElement; -+import javax.xml.bind.annotation.XmlSchemaType; -+import javax.xml.bind.annotation.XmlType; -+ -+ -+/** -+ *

Java class for anonymous complex type. -+ * -+ *

The following schema fragment specifies the expected content contained within this class. -+ * -+ *

-+ * <complexType>
-+ *   <complexContent>
-+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
-+ *       <sequence>
-+ *         <element name="email" type="{http://www.w3.org/2001/XMLSchema}string"/>
-+ *         <element name="fullName" type="{http://www.w3.org/2001/XMLSchema}string"/>
-+ *       </sequence>
-+ *       <attribute name="flag" use="required" type="{http://javaops.ru}flagType" />
-+ *       <attribute name="city" use="required" type="{http://www.w3.org/2001/XMLSchema}IDREF" />
-+ *     </restriction>
-+ *   </complexContent>
-+ * </complexType>
-+ * 
-+ * -+ * -+ */ -+@XmlAccessorType(XmlAccessType.FIELD) -+@XmlType(name = "", propOrder = { -+ "email", -+ "fullName" -+}) -+@XmlRootElement(name = "User", namespace = "http://javaops.ru") -+public class User { -+ -+ @XmlElement(namespace = "http://javaops.ru", required = true) -+ protected String email; -+ @XmlElement(namespace = "http://javaops.ru", required = true) -+ protected String fullName; -+ @XmlAttribute(name = "flag", required = true) -+ protected FlagType flag; -+ @XmlAttribute(name = "city", required = true) -+ @XmlIDREF -+ @XmlSchemaType(name = "IDREF") -+ protected Object city; -+ -+ /** -+ * Gets the value of the email property. -+ * -+ * @return -+ * possible object is -+ * {@link String } -+ * -+ */ -+ public String getEmail() { -+ return email; -+ } -+ -+ /** -+ * Sets the value of the email property. -+ * -+ * @param value -+ * allowed object is -+ * {@link String } -+ * -+ */ -+ public void setEmail(String value) { -+ this.email = value; -+ } -+ -+ /** -+ * Gets the value of the fullName property. -+ * -+ * @return -+ * possible object is -+ * {@link String } -+ * -+ */ -+ public String getFullName() { -+ return fullName; -+ } -+ -+ /** -+ * Sets the value of the fullName property. -+ * -+ * @param value -+ * allowed object is -+ * {@link String } -+ * -+ */ -+ public void setFullName(String value) { -+ this.fullName = value; -+ } -+ -+ /** -+ * Gets the value of the flag property. -+ * -+ * @return -+ * possible object is -+ * {@link FlagType } -+ * -+ */ -+ public FlagType getFlag() { -+ return flag; -+ } -+ -+ /** -+ * Sets the value of the flag property. -+ * -+ * @param value -+ * allowed object is -+ * {@link FlagType } -+ * -+ */ -+ public void setFlag(FlagType value) { -+ this.flag = value; -+ } -+ -+ /** -+ * Gets the value of the city property. -+ * -+ * @return -+ * possible object is -+ * {@link Object } -+ * -+ */ -+ public Object getCity() { -+ return city; -+ } -+ -+ /** -+ * Sets the value of the city property. -+ * -+ * @param value -+ * allowed object is -+ * {@link Object } -+ * -+ */ -+ public void setCity(Object value) { -+ this.city = value; -+ } -+ -+} -Index: src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/schema/FlagType.java (revision ) -@@ -0,0 +1,54 @@ -+ -+package ru.javaops.masterjava.xml.schema; -+ -+import javax.xml.bind.annotation.XmlEnum; -+import javax.xml.bind.annotation.XmlEnumValue; -+import javax.xml.bind.annotation.XmlType; -+ -+ -+/** -+ *

Java class for flagType. -+ * -+ *

The following schema fragment specifies the expected content contained within this class. -+ *

-+ *

-+ * <simpleType name="flagType">
-+ *   <restriction base="{http://www.w3.org/2001/XMLSchema}string">
-+ *     <enumeration value="active"/>
-+ *     <enumeration value="deleted"/>
-+ *     <enumeration value="superuser"/>
-+ *   </restriction>
-+ * </simpleType>
-+ * 
-+ * -+ */ -+@XmlType(name = "flagType", namespace = "http://javaops.ru") -+@XmlEnum -+public enum FlagType { -+ -+ @XmlEnumValue("active") -+ ACTIVE("active"), -+ @XmlEnumValue("deleted") -+ DELETED("deleted"), -+ @XmlEnumValue("superuser") -+ SUPERUSER("superuser"); -+ private final String value; -+ -+ FlagType(String v) { -+ value = v; -+ } -+ -+ public String value() { -+ return value; -+ } -+ -+ public static FlagType fromValue(String v) { -+ for (FlagType c: FlagType.values()) { -+ if (c.value.equals(v)) { -+ return c; -+ } -+ } -+ throw new IllegalArgumentException(v); -+ } -+ -+} -Index: src/test/resources/payload.xml -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/test/resources/payload.xml (revision ) -+++ src/test/resources/payload.xml (revision ) -@@ -0,0 +1,23 @@ -+ -+ -+ -+ gmail@gmail.com -+ Full Name -+ -+ -+ admin@javaops.ru -+ Admin -+ -+ -+ mail@yandex.ru -+ Deleted -+ -+ -+ -+ Санкт-Петербург -+ Киев -+ Минск -+ -+ -\ No newline at end of file -Index: src/main/java/ru/javaops/masterjava/xml/schema/CityType.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/schema/CityType.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/schema/CityType.java (revision ) -@@ -0,0 +1,94 @@ -+ -+package ru.javaops.masterjava.xml.schema; -+ -+import javax.xml.bind.annotation.XmlAccessType; -+import javax.xml.bind.annotation.XmlAccessorType; -+import javax.xml.bind.annotation.XmlAttribute; -+import javax.xml.bind.annotation.XmlID; -+import javax.xml.bind.annotation.XmlSchemaType; -+import javax.xml.bind.annotation.XmlType; -+import javax.xml.bind.annotation.XmlValue; -+import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; -+import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; -+ -+ -+/** -+ *

Java class for cityType complex type. -+ * -+ *

The following schema fragment specifies the expected content contained within this class. -+ * -+ *

-+ * <complexType name="cityType">
-+ *   <simpleContent>
-+ *     <extension base="<http://www.w3.org/2001/XMLSchema>string">
-+ *       <attribute name="id" use="required" type="{http://www.w3.org/2001/XMLSchema}ID" />
-+ *     </extension>
-+ *   </simpleContent>
-+ * </complexType>
-+ * 
-+ * -+ * -+ */ -+@XmlAccessorType(XmlAccessType.FIELD) -+@XmlType(name = "cityType", namespace = "http://javaops.ru", propOrder = { -+ "value" -+}) -+public class CityType { -+ -+ @XmlValue -+ protected String value; -+ @XmlAttribute(name = "id", required = true) -+ @XmlJavaTypeAdapter(CollapsedStringAdapter.class) -+ @XmlID -+ @XmlSchemaType(name = "ID") -+ protected String id; -+ -+ /** -+ * Gets the value of the value property. -+ * -+ * @return -+ * possible object is -+ * {@link String } -+ * -+ */ -+ public String getValue() { -+ return value; -+ } -+ -+ /** -+ * Sets the value of the value property. -+ * -+ * @param value -+ * allowed object is -+ * {@link String } -+ * -+ */ -+ public void setValue(String value) { -+ this.value = value; -+ } -+ -+ /** -+ * Gets the value of the id property. -+ * -+ * @return -+ * possible object is -+ * {@link String } -+ * -+ */ -+ public String getId() { -+ return id; -+ } -+ -+ /** -+ * Sets the value of the id property. -+ * -+ * @param value -+ * allowed object is -+ * {@link String } -+ * -+ */ -+ public void setId(String value) { -+ this.id = value; -+ } -+ -+} -Index: src/main/resources/payload.xsd -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/resources/payload.xsd (revision ) -+++ src/main/resources/payload.xsd (revision ) -@@ -0,0 +1,56 @@ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -+ -\ No newline at end of file diff --git a/2_07_JAXB.patch b/2_07_JAXB.patch deleted file mode 100644 index 5dfafecc9..000000000 --- a/2_07_JAXB.patch +++ /dev/null @@ -1,342 +0,0 @@ -Index: src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/util/JaxbMarshaller.java (revision ) -@@ -0,0 +1,39 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import javax.xml.bind.JAXBContext; -+import javax.xml.bind.JAXBException; -+import javax.xml.bind.Marshaller; -+import javax.xml.bind.PropertyException; -+import javax.xml.validation.Schema; -+import java.io.StringWriter; -+import java.io.Writer; -+ -+public class JaxbMarshaller { -+ private Marshaller marshaller; -+ -+ public JaxbMarshaller(JAXBContext ctx) throws JAXBException { -+ marshaller = ctx.createMarshaller(); -+ marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); -+ marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8"); -+ marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); -+ } -+ -+ public void setProperty(String prop, Object value) throws PropertyException { -+ marshaller.setProperty(prop, value); -+ } -+ -+ public synchronized void setSchema(Schema schema) { -+ marshaller.setSchema(schema); -+ } -+ -+ public String marshal(Object instance) throws JAXBException { -+ StringWriter sw = new StringWriter(); -+ marshal(instance, sw); -+ return sw.toString(); -+ } -+ -+ public synchronized void marshal(Object instance, Writer writer) throws JAXBException { -+ marshaller.marshal(instance, writer); -+ } -+ -+} -Index: src/main/java/ru/javaops/masterjava/xml/util/Schemas.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/util/Schemas.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/util/Schemas.java (revision ) -@@ -0,0 +1,48 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import com.google.common.io.Resources; -+import org.xml.sax.SAXException; -+ -+import javax.xml.XMLConstants; -+import javax.xml.transform.stream.StreamSource; -+import javax.xml.validation.Schema; -+import javax.xml.validation.SchemaFactory; -+import java.io.File; -+import java.io.StringReader; -+import java.net.URL; -+ -+ -+public class Schemas { -+ -+ // SchemaFactory is not thread-safe -+ private static final SchemaFactory SCHEMA_FACTORY = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); -+ -+ public static synchronized Schema ofString(String xsd) { -+ try { -+ return SCHEMA_FACTORY.newSchema(new StreamSource(new StringReader(xsd))); -+ } catch (SAXException e) { -+ throw new IllegalArgumentException(e); -+ } -+ } -+ -+ public static synchronized Schema ofClasspath(String resource) { -+ // http://digitalsanctum.com/2012/11/30/how-to-read-file-contents-in-java-the-easy-way-with-guava/ -+ return ofURL(Resources.getResource(resource)); -+ } -+ -+ public static synchronized Schema ofURL(URL url) { -+ try { -+ return SCHEMA_FACTORY.newSchema(url); -+ } catch (SAXException e) { -+ throw new IllegalArgumentException(e); -+ } -+ } -+ -+ public static synchronized Schema ofFile(File file) { -+ try { -+ return SCHEMA_FACTORY.newSchema(file); -+ } catch (SAXException e) { -+ throw new IllegalArgumentException(e); -+ } -+ } -+} -Index: src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java (revision ) -+++ src/test/java/ru/javaops/masterjava/xml/util/JaxbParserTest.java (revision ) -@@ -0,0 +1,40 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import com.google.common.io.Resources; -+import org.junit.Test; -+import ru.javaops.masterjava.xml.schema.CityType; -+import ru.javaops.masterjava.xml.schema.ObjectFactory; -+import ru.javaops.masterjava.xml.schema.Payload; -+ -+import javax.xml.bind.JAXBElement; -+import javax.xml.namespace.QName; -+ -+public class JaxbParserTest { -+ private static final JaxbParser JAXB_PARSER = new JaxbParser(ObjectFactory.class); -+ -+ static { -+ JAXB_PARSER.setSchema(Schemas.ofClasspath("payload.xsd")); -+ } -+ -+ @Test -+ public void testPayload() throws Exception { -+// JaxbParserTest.class.getResourceAsStream("/city.xml") -+ Payload payload = JAXB_PARSER.unmarshal( -+ Resources.getResource("payload.xml").openStream()); -+ String strPayload = JAXB_PARSER.marshal(payload); -+ JAXB_PARSER.validate(strPayload); -+ System.out.println(strPayload); -+ } -+ -+ @Test -+ public void testCity() throws Exception { -+ JAXBElement cityElement = JAXB_PARSER.unmarshal( -+ Resources.getResource("city.xml").openStream()); -+ CityType city = cityElement.getValue(); -+ JAXBElement cityElement2 = -+ new JAXBElement<>(new QName("http://javaops.ru", "City"), CityType.class, city); -+ String strCity = JAXB_PARSER.marshal(cityElement2); -+ JAXB_PARSER.validate(strCity); -+ System.out.println(strCity); -+ } -+} -\ No newline at end of file -Index: src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/util/JaxbParser.java (revision ) -@@ -0,0 +1,88 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import org.xml.sax.SAXException; -+ -+import javax.xml.bind.JAXBContext; -+import javax.xml.bind.JAXBException; -+import javax.xml.bind.PropertyException; -+import javax.xml.transform.stream.StreamSource; -+import javax.xml.validation.Schema; -+import java.io.*; -+ -+ -+/** -+ * Marshalling/Unmarshalling JAXB helper -+ * XML Facade -+ */ -+public class JaxbParser { -+ -+ protected JaxbMarshaller jaxbMarshaller; -+ protected JaxbUnmarshaller jaxbUnmarshaller; -+ protected Schema schema; -+ -+ public JaxbParser(Class... classesToBeBound) { -+ try { -+ init(JAXBContext.newInstance(classesToBeBound)); -+ } catch (JAXBException e) { -+ throw new IllegalArgumentException(e); -+ } -+ } -+ -+ // http://stackoverflow.com/questions/30643802/what-is-jaxbcontext-newinstancestring-contextpath -+ public JaxbParser(String context) { -+ try { -+ init(JAXBContext.newInstance(context)); -+ } catch (JAXBException e) { -+ throw new IllegalArgumentException(e); -+ } -+ } -+ -+ private void init(JAXBContext ctx) throws JAXBException { -+ jaxbMarshaller = new JaxbMarshaller(ctx); -+ jaxbUnmarshaller = new JaxbUnmarshaller(ctx); -+ } -+ -+ // Unmarshaller -+ public T unmarshal(InputStream is) throws JAXBException { -+ return (T) jaxbUnmarshaller.unmarshal(is); -+ } -+ -+ public T unmarshal(Reader reader) throws JAXBException { -+ return (T) jaxbUnmarshaller.unmarshal(reader); -+ } -+ -+ public T unmarshal(String str) throws JAXBException { -+ return (T) jaxbUnmarshaller.unmarshal(str); -+ } -+ -+ // Marshaller -+ public void setMarshallerProperty(String prop, Object value) { -+ try { -+ jaxbMarshaller.setProperty(prop, value); -+ } catch (PropertyException e) { -+ throw new IllegalArgumentException(e); -+ } -+ } -+ -+ public String marshal(Object instance) throws JAXBException { -+ return jaxbMarshaller.marshal(instance); -+ } -+ -+ public void marshal(Object instance, Writer writer) throws JAXBException { -+ jaxbMarshaller.marshal(instance, writer); -+ } -+ -+ public void setSchema(Schema schema) { -+ this.schema = schema; -+ jaxbUnmarshaller.setSchema(schema); -+ jaxbMarshaller.setSchema(schema); -+ } -+ -+ public void validate(String str) throws IOException, SAXException { -+ validate(new StringReader(str)); -+ } -+ -+ public void validate(Reader reader) throws IOException, SAXException { -+ schema.newValidator().validate(new StreamSource(reader)); -+ } -+} -Index: src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/util/JaxbUnmarshaller.java (revision ) -@@ -0,0 +1,33 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import javax.xml.bind.JAXBContext; -+import javax.xml.bind.JAXBException; -+import javax.xml.bind.Unmarshaller; -+import javax.xml.validation.Schema; -+import java.io.InputStream; -+import java.io.Reader; -+import java.io.StringReader; -+ -+public class JaxbUnmarshaller { -+ private Unmarshaller unmarshaller; -+ -+ public JaxbUnmarshaller(JAXBContext ctx) throws JAXBException { -+ unmarshaller = ctx.createUnmarshaller(); -+ } -+ -+ public synchronized void setSchema(Schema schema) { -+ unmarshaller.setSchema(schema); -+ } -+ -+ public synchronized Object unmarshal(InputStream is) throws JAXBException { -+ return unmarshaller.unmarshal(is); -+ } -+ -+ public synchronized Object unmarshal(Reader reader) throws JAXBException { -+ return unmarshaller.unmarshal(reader); -+ } -+ -+ public Object unmarshal(String str) throws JAXBException { -+ return unmarshal(new StringReader(str)); -+ } -+} -Index: src/test/resources/city.xml -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/test/resources/city.xml (revision ) -+++ src/test/resources/city.xml (revision ) -@@ -0,0 +1,4 @@ -+Санкт-Петербург -+ -\ No newline at end of file -Index: pom.xml -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- pom.xml (date 1508793189000) -+++ pom.xml (revision ) -@@ -32,6 +32,14 @@ -
- - org.apache.maven.plugins -+ maven-surefire-plugin -+ 2.20.1 -+ -+ -Dfile.encoding=UTF-8 -+ -+ -+ -+ org.apache.maven.plugins - maven-shade-plugin - 3.1.0 - -@@ -79,6 +87,17 @@ - jmh-generator-annprocess - RELEASE - provided -+ -+ -+ com.google.guava -+ guava -+ 21.0 -+ -+ -+ junit -+ junit -+ 4.12 -+ test - - - diff --git a/2_08_StAX.patch b/2_08_StAX.patch new file mode 100644 index 000000000..228cca7b1 --- /dev/null +++ b/2_08_StAX.patch @@ -0,0 +1,109 @@ +Index: src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java (revision ) +@@ -0,0 +1,56 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import javax.xml.stream.XMLInputFactory; ++import javax.xml.stream.XMLStreamException; ++import javax.xml.stream.XMLStreamReader; ++import javax.xml.stream.events.XMLEvent; ++import java.io.InputStream; ++ ++public class StaxStreamProcessor implements AutoCloseable { ++ private static final XMLInputFactory FACTORY = XMLInputFactory.newInstance(); ++ ++ private final XMLStreamReader reader; ++ ++ public StaxStreamProcessor(InputStream is) throws XMLStreamException { ++ reader = FACTORY.createXMLStreamReader(is); ++ } ++ ++ public XMLStreamReader getReader() { ++ return reader; ++ } ++ ++ public boolean doUntil(int stopEvent, String value) throws XMLStreamException { ++ while (reader.hasNext()) { ++ int event = reader.next(); ++ if (event == stopEvent) { ++ if (value.equals(getValue(event))) { ++ return true; ++ } ++ } ++ } ++ return false; ++ } ++ ++ public String getValue(int event) throws XMLStreamException { ++ return (event == XMLEvent.CHARACTERS) ? reader.getText() : reader.getLocalName(); ++ } ++ ++ public String getElementValue(String element) throws XMLStreamException { ++ return doUntil(XMLEvent.START_ELEMENT, element) ? reader.getElementText() : null; ++ } ++ ++ public String getText() throws XMLStreamException { ++ return reader.getElementText(); ++ } ++ ++ @Override ++ public void close() { ++ if (reader != null) { ++ try { ++ reader.close(); ++ } catch (XMLStreamException e) { ++ // empty ++ } ++ } ++ } ++} +Index: src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java (revision ) ++++ src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java (revision ) +@@ -0,0 +1,36 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import com.google.common.io.Resources; ++import org.junit.Test; ++ ++import javax.xml.stream.XMLStreamReader; ++import javax.xml.stream.events.XMLEvent; ++ ++public class StaxStreamProcessorTest { ++ @Test ++ public void readCities() throws Exception { ++ try (StaxStreamProcessor processor = ++ new StaxStreamProcessor(Resources.getResource("payload.xml").openStream())) { ++ XMLStreamReader reader = processor.getReader(); ++ while (reader.hasNext()) { ++ int event = reader.next(); ++ if (event == XMLEvent.START_ELEMENT) { ++ if ("City".equals(reader.getLocalName())) { ++ System.out.println(reader.getElementText()); ++ } ++ } ++ } ++ } ++ } ++ ++ @Test ++ public void readCities2() throws Exception { ++ try (StaxStreamProcessor processor = ++ new StaxStreamProcessor(Resources.getResource("payload.xml").openStream())) { ++ String city; ++ while ((city = processor.getElementValue("City")) != null) { ++ System.out.println(city); ++ } ++ } ++ } ++} +\ No newline at end of file diff --git a/src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java b/src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java new file mode 100644 index 000000000..921ca6aff --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java @@ -0,0 +1,56 @@ +package ru.javaops.masterjava.xml.util; + +import javax.xml.stream.XMLInputFactory; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.events.XMLEvent; +import java.io.InputStream; + +public class StaxStreamProcessor implements AutoCloseable { + private static final XMLInputFactory FACTORY = XMLInputFactory.newInstance(); + + private final XMLStreamReader reader; + + public StaxStreamProcessor(InputStream is) throws XMLStreamException { + reader = FACTORY.createXMLStreamReader(is); + } + + public XMLStreamReader getReader() { + return reader; + } + + public boolean doUntil(int stopEvent, String value) throws XMLStreamException { + while (reader.hasNext()) { + int event = reader.next(); + if (event == stopEvent) { + if (value.equals(getValue(event))) { + return true; + } + } + } + return false; + } + + public String getValue(int event) throws XMLStreamException { + return (event == XMLEvent.CHARACTERS) ? reader.getText() : reader.getLocalName(); + } + + public String getElementValue(String element) throws XMLStreamException { + return doUntil(XMLEvent.START_ELEMENT, element) ? reader.getElementText() : null; + } + + public String getText() throws XMLStreamException { + return reader.getElementText(); + } + + @Override + public void close() { + if (reader != null) { + try { + reader.close(); + } catch (XMLStreamException e) { + // empty + } + } + } +} diff --git a/src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java b/src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java new file mode 100644 index 000000000..fd55963dd --- /dev/null +++ b/src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java @@ -0,0 +1,36 @@ +package ru.javaops.masterjava.xml.util; + +import com.google.common.io.Resources; +import org.junit.Test; + +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.events.XMLEvent; + +public class StaxStreamProcessorTest { + @Test + public void readCities() throws Exception { + try (StaxStreamProcessor processor = + new StaxStreamProcessor(Resources.getResource("payload.xml").openStream())) { + XMLStreamReader reader = processor.getReader(); + while (reader.hasNext()) { + int event = reader.next(); + if (event == XMLEvent.START_ELEMENT) { + if ("City".equals(reader.getLocalName())) { + System.out.println(reader.getElementText()); + } + } + } + } + } + + @Test + public void readCities2() throws Exception { + try (StaxStreamProcessor processor = + new StaxStreamProcessor(Resources.getResource("payload.xml").openStream())) { + String city; + while ((city = processor.getElementValue("City")) != null) { + System.out.println(city); + } + } + } +} \ No newline at end of file From b3ce5504ce55b104cae551752becfed44d3b18cf Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Thu, 20 Sep 2018 16:42:21 +0500 Subject: [PATCH 16/20] 2_09_XPath.patch --- 2_09_XPath.patch | 101 ++++++++++++++++++ .../masterjava/xml/util/XPathProcessor.java | 58 ++++++++++ .../xml/util/XPathProcessorTest.java | 26 +++++ 3 files changed, 185 insertions(+) create mode 100644 2_09_XPath.patch create mode 100644 src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java create mode 100644 src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java diff --git a/2_09_XPath.patch b/2_09_XPath.patch new file mode 100644 index 000000000..f662f3b82 --- /dev/null +++ b/2_09_XPath.patch @@ -0,0 +1,101 @@ +Index: src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java (revision ) +@@ -0,0 +1,58 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import org.w3c.dom.Document; ++import org.xml.sax.SAXException; ++ ++import javax.xml.namespace.QName; ++import javax.xml.parsers.DocumentBuilder; ++import javax.xml.parsers.DocumentBuilderFactory; ++import javax.xml.parsers.ParserConfigurationException; ++import javax.xml.xpath.XPath; ++import javax.xml.xpath.XPathExpression; ++import javax.xml.xpath.XPathExpressionException; ++import javax.xml.xpath.XPathFactory; ++import java.io.IOException; ++import java.io.InputStream; ++ ++public class XPathProcessor { ++ private static final DocumentBuilderFactory DOCUMENT_FACTORY = DocumentBuilderFactory.newInstance(); ++ private static final DocumentBuilder DOCUMENT_BUILDER; ++ ++ private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance(); ++ private static final XPath XPATH = XPATH_FACTORY.newXPath(); ++ ++ static { ++ DOCUMENT_FACTORY.setNamespaceAware(true); ++ try { ++ DOCUMENT_BUILDER = DOCUMENT_FACTORY.newDocumentBuilder(); ++ } catch (ParserConfigurationException e) { ++ throw new IllegalStateException(e); ++ } ++ } ++ ++ private final Document doc; ++ ++ public XPathProcessor(InputStream is) { ++ try { ++ doc = DOCUMENT_BUILDER.parse(is); ++ } catch (SAXException | IOException e) { ++ throw new IllegalArgumentException(e); ++ } ++ } ++ ++ public static synchronized XPathExpression getExpression(String exp) { ++ try { ++ return XPATH.compile(exp); ++ } catch (XPathExpressionException e) { ++ throw new IllegalArgumentException(e); ++ } ++ } ++ ++ public T evaluate(XPathExpression expression, QName type) { ++ try { ++ return (T) expression.evaluate(doc, type); ++ } catch (XPathExpressionException e) { ++ throw new IllegalArgumentException(e); ++ } ++ } ++} +Index: src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java (revision ) ++++ src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java (revision ) +@@ -0,0 +1,26 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import com.google.common.io.Resources; ++import org.junit.Test; ++import org.w3c.dom.NodeList; ++ ++import javax.xml.xpath.XPathConstants; ++import javax.xml.xpath.XPathExpression; ++import java.io.InputStream; ++import java.util.stream.IntStream; ++ ++public class XPathProcessorTest { ++ @Test ++ public void getCities() throws Exception { ++ try (InputStream is = ++ Resources.getResource("payload.xml").openStream()) { ++ XPathProcessor processor = new XPathProcessor(is); ++ XPathExpression expression = ++ XPathProcessor.getExpression("/*[name()='Payload']/*[name()='Cities']/*[name()='City']/text()"); ++ NodeList nodes = processor.evaluate(expression, XPathConstants.NODESET); ++ IntStream.range(0, nodes.getLength()).forEach( ++ i -> System.out.println(nodes.item(i).getNodeValue()) ++ ); ++ } ++ } ++} +\ No newline at end of file diff --git a/src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java b/src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java new file mode 100644 index 000000000..63baae5d1 --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java @@ -0,0 +1,58 @@ +package ru.javaops.masterjava.xml.util; + +import org.w3c.dom.Document; +import org.xml.sax.SAXException; + +import javax.xml.namespace.QName; +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.xpath.XPath; +import javax.xml.xpath.XPathExpression; +import javax.xml.xpath.XPathExpressionException; +import javax.xml.xpath.XPathFactory; +import java.io.IOException; +import java.io.InputStream; + +public class XPathProcessor { + private static final DocumentBuilderFactory DOCUMENT_FACTORY = DocumentBuilderFactory.newInstance(); + private static final DocumentBuilder DOCUMENT_BUILDER; + + private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance(); + private static final XPath XPATH = XPATH_FACTORY.newXPath(); + + static { + DOCUMENT_FACTORY.setNamespaceAware(true); + try { + DOCUMENT_BUILDER = DOCUMENT_FACTORY.newDocumentBuilder(); + } catch (ParserConfigurationException e) { + throw new IllegalStateException(e); + } + } + + private final Document doc; + + public XPathProcessor(InputStream is) { + try { + doc = DOCUMENT_BUILDER.parse(is); + } catch (SAXException | IOException e) { + throw new IllegalArgumentException(e); + } + } + + public static synchronized XPathExpression getExpression(String exp) { + try { + return XPATH.compile(exp); + } catch (XPathExpressionException e) { + throw new IllegalArgumentException(e); + } + } + + public T evaluate(XPathExpression expression, QName type) { + try { + return (T) expression.evaluate(doc, type); + } catch (XPathExpressionException e) { + throw new IllegalArgumentException(e); + } + } +} diff --git a/src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java b/src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java new file mode 100644 index 000000000..199f676a1 --- /dev/null +++ b/src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java @@ -0,0 +1,26 @@ +package ru.javaops.masterjava.xml.util; + +import com.google.common.io.Resources; +import org.junit.Test; +import org.w3c.dom.NodeList; + +import javax.xml.xpath.XPathConstants; +import javax.xml.xpath.XPathExpression; +import java.io.InputStream; +import java.util.stream.IntStream; + +public class XPathProcessorTest { + @Test + public void getCities() throws Exception { + try (InputStream is = + Resources.getResource("payload.xml").openStream()) { + XPathProcessor processor = new XPathProcessor(is); + XPathExpression expression = + XPathProcessor.getExpression("/*[name()='Payload']/*[name()='Cities']/*[name()='City']/text()"); + NodeList nodes = processor.evaluate(expression, XPathConstants.NODESET); + IntStream.range(0, nodes.getLength()).forEach( + i -> System.out.println(nodes.item(i).getNodeValue()) + ); + } + } +} \ No newline at end of file From 9b6db16a82ad4cbf281fbf5fbdaa9b9e124b80cf Mon Sep 17 00:00:00 2001 From: Alexey Korban Date: Thu, 20 Sep 2018 16:59:36 +0500 Subject: [PATCH 17/20] 2_10_Xslt.patch --- 2_08_StAX.patch | 109 ------------------ 2_09_XPath.patch | 101 ---------------- 2_10_Xslt.patch | 95 +++++++++++++++ .../masterjava/xml/util/XsltProcessor.java | 43 +++++++ src/main/resources/cities.xsl | 9 ++ .../xml/util/XsltProcessorTest.java | 18 +++ 6 files changed, 165 insertions(+), 210 deletions(-) delete mode 100644 2_08_StAX.patch delete mode 100644 2_09_XPath.patch create mode 100644 2_10_Xslt.patch create mode 100644 src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java create mode 100644 src/main/resources/cities.xsl create mode 100644 src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java diff --git a/2_08_StAX.patch b/2_08_StAX.patch deleted file mode 100644 index 228cca7b1..000000000 --- a/2_08_StAX.patch +++ /dev/null @@ -1,109 +0,0 @@ -Index: src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/util/StaxStreamProcessor.java (revision ) -@@ -0,0 +1,56 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import javax.xml.stream.XMLInputFactory; -+import javax.xml.stream.XMLStreamException; -+import javax.xml.stream.XMLStreamReader; -+import javax.xml.stream.events.XMLEvent; -+import java.io.InputStream; -+ -+public class StaxStreamProcessor implements AutoCloseable { -+ private static final XMLInputFactory FACTORY = XMLInputFactory.newInstance(); -+ -+ private final XMLStreamReader reader; -+ -+ public StaxStreamProcessor(InputStream is) throws XMLStreamException { -+ reader = FACTORY.createXMLStreamReader(is); -+ } -+ -+ public XMLStreamReader getReader() { -+ return reader; -+ } -+ -+ public boolean doUntil(int stopEvent, String value) throws XMLStreamException { -+ while (reader.hasNext()) { -+ int event = reader.next(); -+ if (event == stopEvent) { -+ if (value.equals(getValue(event))) { -+ return true; -+ } -+ } -+ } -+ return false; -+ } -+ -+ public String getValue(int event) throws XMLStreamException { -+ return (event == XMLEvent.CHARACTERS) ? reader.getText() : reader.getLocalName(); -+ } -+ -+ public String getElementValue(String element) throws XMLStreamException { -+ return doUntil(XMLEvent.START_ELEMENT, element) ? reader.getElementText() : null; -+ } -+ -+ public String getText() throws XMLStreamException { -+ return reader.getElementText(); -+ } -+ -+ @Override -+ public void close() { -+ if (reader != null) { -+ try { -+ reader.close(); -+ } catch (XMLStreamException e) { -+ // empty -+ } -+ } -+ } -+} -Index: src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java (revision ) -+++ src/test/java/ru/javaops/masterjava/xml/util/StaxStreamProcessorTest.java (revision ) -@@ -0,0 +1,36 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import com.google.common.io.Resources; -+import org.junit.Test; -+ -+import javax.xml.stream.XMLStreamReader; -+import javax.xml.stream.events.XMLEvent; -+ -+public class StaxStreamProcessorTest { -+ @Test -+ public void readCities() throws Exception { -+ try (StaxStreamProcessor processor = -+ new StaxStreamProcessor(Resources.getResource("payload.xml").openStream())) { -+ XMLStreamReader reader = processor.getReader(); -+ while (reader.hasNext()) { -+ int event = reader.next(); -+ if (event == XMLEvent.START_ELEMENT) { -+ if ("City".equals(reader.getLocalName())) { -+ System.out.println(reader.getElementText()); -+ } -+ } -+ } -+ } -+ } -+ -+ @Test -+ public void readCities2() throws Exception { -+ try (StaxStreamProcessor processor = -+ new StaxStreamProcessor(Resources.getResource("payload.xml").openStream())) { -+ String city; -+ while ((city = processor.getElementValue("City")) != null) { -+ System.out.println(city); -+ } -+ } -+ } -+} -\ No newline at end of file diff --git a/2_09_XPath.patch b/2_09_XPath.patch deleted file mode 100644 index f662f3b82..000000000 --- a/2_09_XPath.patch +++ /dev/null @@ -1,101 +0,0 @@ -Index: src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/util/XPathProcessor.java (revision ) -@@ -0,0 +1,58 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import org.w3c.dom.Document; -+import org.xml.sax.SAXException; -+ -+import javax.xml.namespace.QName; -+import javax.xml.parsers.DocumentBuilder; -+import javax.xml.parsers.DocumentBuilderFactory; -+import javax.xml.parsers.ParserConfigurationException; -+import javax.xml.xpath.XPath; -+import javax.xml.xpath.XPathExpression; -+import javax.xml.xpath.XPathExpressionException; -+import javax.xml.xpath.XPathFactory; -+import java.io.IOException; -+import java.io.InputStream; -+ -+public class XPathProcessor { -+ private static final DocumentBuilderFactory DOCUMENT_FACTORY = DocumentBuilderFactory.newInstance(); -+ private static final DocumentBuilder DOCUMENT_BUILDER; -+ -+ private static final XPathFactory XPATH_FACTORY = XPathFactory.newInstance(); -+ private static final XPath XPATH = XPATH_FACTORY.newXPath(); -+ -+ static { -+ DOCUMENT_FACTORY.setNamespaceAware(true); -+ try { -+ DOCUMENT_BUILDER = DOCUMENT_FACTORY.newDocumentBuilder(); -+ } catch (ParserConfigurationException e) { -+ throw new IllegalStateException(e); -+ } -+ } -+ -+ private final Document doc; -+ -+ public XPathProcessor(InputStream is) { -+ try { -+ doc = DOCUMENT_BUILDER.parse(is); -+ } catch (SAXException | IOException e) { -+ throw new IllegalArgumentException(e); -+ } -+ } -+ -+ public static synchronized XPathExpression getExpression(String exp) { -+ try { -+ return XPATH.compile(exp); -+ } catch (XPathExpressionException e) { -+ throw new IllegalArgumentException(e); -+ } -+ } -+ -+ public T evaluate(XPathExpression expression, QName type) { -+ try { -+ return (T) expression.evaluate(doc, type); -+ } catch (XPathExpressionException e) { -+ throw new IllegalArgumentException(e); -+ } -+ } -+} -Index: src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java (revision ) -+++ src/test/java/ru/javaops/masterjava/xml/util/XPathProcessorTest.java (revision ) -@@ -0,0 +1,26 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import com.google.common.io.Resources; -+import org.junit.Test; -+import org.w3c.dom.NodeList; -+ -+import javax.xml.xpath.XPathConstants; -+import javax.xml.xpath.XPathExpression; -+import java.io.InputStream; -+import java.util.stream.IntStream; -+ -+public class XPathProcessorTest { -+ @Test -+ public void getCities() throws Exception { -+ try (InputStream is = -+ Resources.getResource("payload.xml").openStream()) { -+ XPathProcessor processor = new XPathProcessor(is); -+ XPathExpression expression = -+ XPathProcessor.getExpression("/*[name()='Payload']/*[name()='Cities']/*[name()='City']/text()"); -+ NodeList nodes = processor.evaluate(expression, XPathConstants.NODESET); -+ IntStream.range(0, nodes.getLength()).forEach( -+ i -> System.out.println(nodes.item(i).getNodeValue()) -+ ); -+ } -+ } -+} -\ No newline at end of file diff --git a/2_10_Xslt.patch b/2_10_Xslt.patch new file mode 100644 index 000000000..fceab9e55 --- /dev/null +++ b/2_10_Xslt.patch @@ -0,0 +1,95 @@ +Index: src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java (revision ) ++++ src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java (revision ) +@@ -0,0 +1,43 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import javax.xml.transform.*; ++import javax.xml.transform.stream.StreamResult; ++import javax.xml.transform.stream.StreamSource; ++import java.io.*; ++import java.nio.charset.StandardCharsets; ++ ++public class XsltProcessor { ++ private static TransformerFactory FACTORY = TransformerFactory.newInstance(); ++ private final Transformer xformer; ++ ++ public XsltProcessor(InputStream xslInputStream) { ++ this(new BufferedReader(new InputStreamReader(xslInputStream, StandardCharsets.UTF_8))); ++ } ++ ++ public XsltProcessor(Reader xslReader) { ++ try { ++ Templates template = FACTORY.newTemplates(new StreamSource(xslReader)); ++ xformer = template.newTransformer(); ++ } catch (TransformerConfigurationException e) { ++ throw new IllegalStateException("XSLT transformer creation failed: " + e.toString(), e); ++ } ++ } ++ ++ public String transform(InputStream xmlInputStream) throws TransformerException { ++ StringWriter out = new StringWriter(); ++ transform(xmlInputStream, out); ++ return out.getBuffer().toString(); ++ } ++ ++ public void transform(InputStream xmlInputStream, Writer result) throws TransformerException { ++ transform(new BufferedReader(new InputStreamReader(xmlInputStream, StandardCharsets.UTF_8)), result); ++ } ++ ++ public void transform(Reader sourceReader, Writer result) throws TransformerException { ++ xformer.transform(new StreamSource(sourceReader), new StreamResult(result)); ++ } ++ ++ public static String getXsltHeader(String xslt) { ++ return "\n"; ++ } ++} +Index: src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java (revision ) ++++ src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java (revision ) +@@ -0,0 +1,18 @@ ++package ru.javaops.masterjava.xml.util; ++ ++import com.google.common.io.Resources; ++import org.junit.Test; ++ ++import java.io.InputStream; ++ ++public class XsltProcessorTest { ++ @Test ++ public void transform() throws Exception { ++ try (InputStream xslInputStream = Resources.getResource("cities.xsl").openStream(); ++ InputStream xmlInputStream = Resources.getResource("payload.xml").openStream()) { ++ ++ XsltProcessor processor = new XsltProcessor(xslInputStream); ++ System.out.println(processor.transform(xmlInputStream)); ++ } ++ } ++} +Index: src/main/resources/cities.xsl +IDEA additional info: +Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP +<+>UTF-8 +=================================================================== +--- src/main/resources/cities.xsl (revision ) ++++ src/main/resources/cities.xsl (revision ) +@@ -0,0 +1,9 @@ ++ ++ ++ ++ ++ ++ ++ ++ ++ +\ No newline at end of file diff --git a/src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java b/src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java new file mode 100644 index 000000000..019eeed3d --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java @@ -0,0 +1,43 @@ +package ru.javaops.masterjava.xml.util; + +import javax.xml.transform.*; +import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; +import java.io.*; +import java.nio.charset.StandardCharsets; + +public class XsltProcessor { + private static TransformerFactory FACTORY = TransformerFactory.newInstance(); + private final Transformer xformer; + + public XsltProcessor(InputStream xslInputStream) { + this(new BufferedReader(new InputStreamReader(xslInputStream, StandardCharsets.UTF_8))); + } + + public XsltProcessor(Reader xslReader) { + try { + Templates template = FACTORY.newTemplates(new StreamSource(xslReader)); + xformer = template.newTransformer(); + } catch (TransformerConfigurationException e) { + throw new IllegalStateException("XSLT transformer creation failed: " + e.toString(), e); + } + } + + public String transform(InputStream xmlInputStream) throws TransformerException { + StringWriter out = new StringWriter(); + transform(xmlInputStream, out); + return out.getBuffer().toString(); + } + + public void transform(InputStream xmlInputStream, Writer result) throws TransformerException { + transform(new BufferedReader(new InputStreamReader(xmlInputStream, StandardCharsets.UTF_8)), result); + } + + public void transform(Reader sourceReader, Writer result) throws TransformerException { + xformer.transform(new StreamSource(sourceReader), new StreamResult(result)); + } + + public static String getXsltHeader(String xslt) { + return "\n"; + } +} diff --git a/src/main/resources/cities.xsl b/src/main/resources/cities.xsl new file mode 100644 index 000000000..1c509124b --- /dev/null +++ b/src/main/resources/cities.xsl @@ -0,0 +1,9 @@ + + + + + + + + + \ No newline at end of file diff --git a/src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java b/src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java new file mode 100644 index 000000000..d7f42a699 --- /dev/null +++ b/src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java @@ -0,0 +1,18 @@ +package ru.javaops.masterjava.xml.util; + +import com.google.common.io.Resources; +import org.junit.Test; + +import java.io.InputStream; + +public class XsltProcessorTest { + @Test + public void transform() throws Exception { + try (InputStream xslInputStream = Resources.getResource("cities.xsl").openStream(); + InputStream xmlInputStream = Resources.getResource("payload.xml").openStream()) { + + XsltProcessor processor = new XsltProcessor(xslInputStream); + System.out.println(processor.transform(xmlInputStream)); + } + } +} From 2db45b67c4c143ed85e891c4c0af7dd57f71a4b9 Mon Sep 17 00:00:00 2001 From: AlexeyKorban Date: Thu, 20 Sep 2018 19:55:40 +0500 Subject: [PATCH 18/20] =?UTF-8?q?=D0=94=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20=D0=BF=D1=80=D0=BE=D0=B5=D0=BA=D1=82=D1=8B=20=D0=B2=20?= =?UTF-8?q?xsd?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/main/resources/payload2.xsd | 64 +++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 src/main/resources/payload2.xsd diff --git a/src/main/resources/payload2.xsd b/src/main/resources/payload2.xsd new file mode 100644 index 000000000..5273497c5 --- /dev/null +++ b/src/main/resources/payload2.xsd @@ -0,0 +1,64 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From bb0f6f931a1c8a46077c224f6743d92283e2d138 Mon Sep 17 00:00:00 2001 From: AlexeyKorban Date: Thu, 20 Sep 2018 21:32:45 +0500 Subject: [PATCH 19/20] 3_1 --- .../masterjava/xml/schema/GroupType.java | 40 ++++ .../masterjava/xml/schema/ObjectFactory.java | 24 ++ .../masterjava/xml/schema/Payload.java | 111 ++++++++- .../masterjava/xml/schema/Project.java | 216 ++++++++++++++++++ .../javaops/masterjava/xml/schema/User.java | 95 +++++--- src/main/resources/payload.xsd | 57 ++++- src/test/resources/payload.xml | 36 +-- 7 files changed, 516 insertions(+), 63 deletions(-) create mode 100644 src/main/java/ru/javaops/masterjava/xml/schema/GroupType.java create mode 100644 src/main/java/ru/javaops/masterjava/xml/schema/Project.java diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/GroupType.java b/src/main/java/ru/javaops/masterjava/xml/schema/GroupType.java new file mode 100644 index 000000000..d5041640b --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/schema/GroupType.java @@ -0,0 +1,40 @@ + +package ru.javaops.masterjava.xml.schema; + +import javax.xml.bind.annotation.XmlEnum; +import javax.xml.bind.annotation.XmlType; + + +/** + *

Java class for groupType. + * + *

The following schema fragment specifies the expected content contained within this class. + *

+ *

+ * <simpleType name="groupType">
+ *   <restriction base="{http://www.w3.org/2001/XMLSchema}string">
+ *     <enumeration value="REGISTERING"/>
+ *     <enumeration value="CURRENT"/>
+ *     <enumeration value="FINISHED"/>
+ *   </restriction>
+ * </simpleType>
+ * 
+ * + */ +@XmlType(name = "groupType", namespace = "http://javaops.ru") +@XmlEnum +public enum GroupType { + + REGISTERING, + CURRENT, + FINISHED; + + public String value() { + return name(); + } + + public static GroupType fromValue(String v) { + return valueOf(v); + } + +} diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java b/src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java index e8f105e2a..bfb393299 100644 --- a/src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java +++ b/src/main/java/ru/javaops/masterjava/xml/schema/ObjectFactory.java @@ -33,6 +33,14 @@ public class ObjectFactory { public ObjectFactory() { } + /** + * Create an instance of {@link Project } + * + */ + public Project createProject() { + return new Project(); + } + /** * Create an instance of {@link Payload } * @@ -41,6 +49,14 @@ public Payload createPayload() { return new Payload(); } + /** + * Create an instance of {@link Project.Group } + * + */ + public Project.Group createProjectGroup() { + return new Project.Group(); + } + /** * Create an instance of {@link User } * @@ -49,6 +65,14 @@ public User createUser() { return new User(); } + /** + * Create an instance of {@link Payload.Projects } + * + */ + public Payload.Projects createPayloadProjects() { + return new Payload.Projects(); + } + /** * Create an instance of {@link Payload.Cities } * diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/Payload.java b/src/main/java/ru/javaops/masterjava/xml/schema/Payload.java index 2a6276490..9d4cc3046 100644 --- a/src/main/java/ru/javaops/masterjava/xml/schema/Payload.java +++ b/src/main/java/ru/javaops/masterjava/xml/schema/Payload.java @@ -1,13 +1,9 @@ package ru.javaops.masterjava.xml.schema; +import javax.xml.bind.annotation.*; import java.util.ArrayList; import java.util.List; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlType; /** @@ -19,7 +15,18 @@ * <complexType> * <complexContent> * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> - * <all> + * <sequence> + * <element name="Projects"> + * <complexType> + * <complexContent> + * <restriction base="{http://www.w3.org/2001/XMLSchema}anyType"> + * <sequence maxOccurs="unbounded"> + * <element ref="{http://javaops.ru}Project"/> + * </sequence> + * </restriction> + * </complexContent> + * </complexType> + * </element> * <element name="Cities"> * <complexType> * <complexContent> @@ -42,7 +49,7 @@ * </complexContent> * </complexType> * </element> - * </all> + * </sequence> * </restriction> * </complexContent> * </complexType> @@ -52,16 +59,44 @@ */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { - + "projects", + "cities", + "users" }) @XmlRootElement(name = "Payload", namespace = "http://javaops.ru") public class Payload { + @XmlElement(name = "Projects", namespace = "http://javaops.ru", required = true) + protected Payload.Projects projects; @XmlElement(name = "Cities", namespace = "http://javaops.ru", required = true) protected Payload.Cities cities; @XmlElement(name = "Users", namespace = "http://javaops.ru", required = true) protected Payload.Users users; + /** + * Gets the value of the projects property. + * + * @return + * possible object is + * {@link Payload.Projects } + * + */ + public Payload.Projects getProjects() { + return projects; + } + + /** + * Sets the value of the projects property. + * + * @param value + * allowed object is + * {@link Payload.Projects } + * + */ + public void setProjects(Payload.Projects value) { + this.projects = value; + } + /** * Gets the value of the cities property. * @@ -171,6 +206,66 @@ public List getCity() { } + /** + *

Java class for anonymous complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+     * <complexType>
+     *   <complexContent>
+     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+     *       <sequence maxOccurs="unbounded">
+     *         <element ref="{http://javaops.ru}Project"/>
+     *       </sequence>
+     *     </restriction>
+     *   </complexContent>
+     * </complexType>
+     * 
+ * + * + */ + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(name = "", propOrder = { + "project" + }) + public static class Projects { + + @XmlElement(name = "Project", namespace = "http://javaops.ru", required = true) + protected List project; + + /** + * Gets the value of the project property. + * + *

+ * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a set method for the project property. + * + *

+ * For example, to add a new item, do as follows: + *

+         *    getProject().add(newItem);
+         * 
+ * + * + *

+ * Objects of the following type(s) are allowed in the list + * {@link Project } + * + * + */ + public List getProject() { + if (project == null) { + project = new ArrayList(); + } + return this.project; + } + + } + + /** *

Java class for anonymous complex type. * diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/Project.java b/src/main/java/ru/javaops/masterjava/xml/schema/Project.java new file mode 100644 index 000000000..180a997e9 --- /dev/null +++ b/src/main/java/ru/javaops/masterjava/xml/schema/Project.java @@ -0,0 +1,216 @@ + +package ru.javaops.masterjava.xml.schema; + +import javax.xml.bind.annotation.*; +import javax.xml.bind.annotation.adapters.CollapsedStringAdapter; +import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter; +import java.util.ArrayList; +import java.util.List; + + +/** + *

Java class for anonymous complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+ * <complexType>
+ *   <complexContent>
+ *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *       <sequence>
+ *         <element name="description" type="{http://www.w3.org/2001/XMLSchema}string"/>
+ *         <sequence maxOccurs="unbounded">
+ *           <element name="Group">
+ *             <complexType>
+ *               <complexContent>
+ *                 <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+ *                   <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}ID" />
+ *                   <attribute name="type" use="required" type="{http://javaops.ru}groupType" />
+ *                 </restriction>
+ *               </complexContent>
+ *             </complexType>
+ *           </element>
+ *         </sequence>
+ *       </sequence>
+ *       <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}string" />
+ *     </restriction>
+ *   </complexContent>
+ * </complexType>
+ * 
+ * + * + */ +@XmlAccessorType(XmlAccessType.FIELD) +@XmlType(name = "", propOrder = { + "description", + "group" +}) +@XmlRootElement(name = "Project", namespace = "http://javaops.ru") +public class Project { + + @XmlElement(namespace = "http://javaops.ru", required = true) + protected String description; + @XmlElement(name = "Group", namespace = "http://javaops.ru", required = true) + protected List group; + @XmlAttribute(name = "name", required = true) + protected String name; + + /** + * Gets the value of the description property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getDescription() { + return description; + } + + /** + * Sets the value of the description property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setDescription(String value) { + this.description = value; + } + + /** + * Gets the value of the group property. + * + *

+ * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a set method for the group property. + * + *

+ * For example, to add a new item, do as follows: + *

+     *    getGroup().add(newItem);
+     * 
+ * + * + *

+ * Objects of the following type(s) are allowed in the list + * {@link Project.Group } + * + * + */ + public List getGroup() { + if (group == null) { + group = new ArrayList(); + } + return this.group; + } + + /** + * Gets the value of the name property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getName() { + return name; + } + + /** + * Sets the value of the name property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setName(String value) { + this.name = value; + } + + + /** + *

Java class for anonymous complex type. + * + *

The following schema fragment specifies the expected content contained within this class. + * + *

+     * <complexType>
+     *   <complexContent>
+     *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
+     *       <attribute name="name" use="required" type="{http://www.w3.org/2001/XMLSchema}ID" />
+     *       <attribute name="type" use="required" type="{http://javaops.ru}groupType" />
+     *     </restriction>
+     *   </complexContent>
+     * </complexType>
+     * 
+ * + * + */ + @XmlAccessorType(XmlAccessType.FIELD) + @XmlType(name = "") + public static class Group { + + @XmlAttribute(name = "name", required = true) + @XmlJavaTypeAdapter(CollapsedStringAdapter.class) + @XmlID + @XmlSchemaType(name = "ID") + protected String name; + @XmlAttribute(name = "type", required = true) + protected GroupType type; + + /** + * Gets the value of the name property. + * + * @return + * possible object is + * {@link String } + * + */ + public String getName() { + return name; + } + + /** + * Sets the value of the name property. + * + * @param value + * allowed object is + * {@link String } + * + */ + public void setName(String value) { + this.name = value; + } + + /** + * Gets the value of the type property. + * + * @return + * possible object is + * {@link GroupType } + * + */ + public GroupType getType() { + return type; + } + + /** + * Sets the value of the type property. + * + * @param value + * allowed object is + * {@link GroupType } + * + */ + public void setType(GroupType value) { + this.type = value; + } + + } + +} diff --git a/src/main/java/ru/javaops/masterjava/xml/schema/User.java b/src/main/java/ru/javaops/masterjava/xml/schema/User.java index b3430ce71..b2cc1c449 100644 --- a/src/main/java/ru/javaops/masterjava/xml/schema/User.java +++ b/src/main/java/ru/javaops/masterjava/xml/schema/User.java @@ -1,14 +1,9 @@ package ru.javaops.masterjava.xml.schema; -import javax.xml.bind.annotation.XmlAccessType; -import javax.xml.bind.annotation.XmlAccessorType; -import javax.xml.bind.annotation.XmlAttribute; -import javax.xml.bind.annotation.XmlElement; -import javax.xml.bind.annotation.XmlIDREF; -import javax.xml.bind.annotation.XmlRootElement; -import javax.xml.bind.annotation.XmlSchemaType; -import javax.xml.bind.annotation.XmlType; +import javax.xml.bind.annotation.*; +import java.util.ArrayList; +import java.util.List; /** @@ -18,16 +13,14 @@ * *
  * <complexType>
- *   <complexContent>
- *     <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
- *       <sequence>
- *         <element name="email" type="{http://www.w3.org/2001/XMLSchema}string"/>
- *         <element name="fullName" type="{http://www.w3.org/2001/XMLSchema}string"/>
- *       </sequence>
+ *   <simpleContent>
+ *     <extension base="<http://www.w3.org/2001/XMLSchema>string">
+ *       <attribute name="email" type="{http://javaops.ru}emailAddressType" />
  *       <attribute name="flag" use="required" type="{http://javaops.ru}flagType" />
  *       <attribute name="city" use="required" type="{http://www.w3.org/2001/XMLSchema}IDREF" />
- *     </restriction>
- *   </complexContent>
+ *       <attribute name="groupRefs" type="{http://www.w3.org/2001/XMLSchema}IDREFS" />
+ *     </extension>
+ *   </simpleContent>
  * </complexType>
  * 
* @@ -35,69 +28,72 @@ */ @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { - "email", - "fullName" + "value" }) @XmlRootElement(name = "User", namespace = "http://javaops.ru") public class User { - @XmlElement(namespace = "http://javaops.ru", required = true) + @XmlValue + protected String value; + @XmlAttribute(name = "email") protected String email; - @XmlElement(namespace = "http://javaops.ru", required = true) - protected String fullName; @XmlAttribute(name = "flag", required = true) protected FlagType flag; @XmlAttribute(name = "city", required = true) @XmlIDREF @XmlSchemaType(name = "IDREF") protected Object city; + @XmlAttribute(name = "groupRefs") + @XmlIDREF + @XmlSchemaType(name = "IDREFS") + protected List groupRefs; /** - * Gets the value of the email property. + * Gets the value of the value property. * * @return * possible object is * {@link String } * */ - public String getEmail() { - return email; + public String getValue() { + return value; } /** - * Sets the value of the email property. + * Sets the value of the value property. * * @param value * allowed object is * {@link String } * */ - public void setEmail(String value) { - this.email = value; + public void setValue(String value) { + this.value = value; } /** - * Gets the value of the fullName property. + * Gets the value of the email property. * * @return * possible object is * {@link String } * */ - public String getFullName() { - return fullName; + public String getEmail() { + return email; } /** - * Sets the value of the fullName property. + * Sets the value of the email property. * * @param value * allowed object is * {@link String } * */ - public void setFullName(String value) { - this.fullName = value; + public void setEmail(String value) { + this.email = value; } /** @@ -148,4 +144,37 @@ public void setCity(Object value) { this.city = value; } + /** + * Gets the value of the groupRefs property. + * + *

+ * This accessor method returns a reference to the live list, + * not a snapshot. Therefore any modification you make to the + * returned list will be present inside the JAXB object. + * This is why there is not a set method for the groupRefs property. + * + *

+ * For example, to add a new item, do as follows: + *

+     *    getGroupRefs().add(newItem);
+     * 
+ * + * + *

+ * Objects of the following type(s) are allowed in the list + * {@link Object } + * + * + */ + public List getGroupRefs() { + if (groupRefs == null) { + groupRefs = new ArrayList(); + } + return this.groupRefs; + } + + @Override + public String toString() { + return value + '(' + email + ')'; + } } diff --git a/src/main/resources/payload.xsd b/src/main/resources/payload.xsd index 9ef1e46eb..3d545ec54 100644 --- a/src/main/resources/payload.xsd +++ b/src/main/resources/payload.xsd @@ -6,7 +6,14 @@ - + + + + + + + + @@ -21,18 +28,39 @@ - + - + - - + + + + + + + + + - - + + + + + + + + + + + + + + + + @@ -44,7 +72,13 @@ - + + + + + + + @@ -53,4 +87,11 @@ + + + + + + + \ No newline at end of file diff --git a/src/test/resources/payload.xml b/src/test/resources/payload.xml index 796e99cb3..fadf8c64a 100644 --- a/src/test/resources/payload.xml +++ b/src/test/resources/payload.xml @@ -1,23 +1,31 @@ - - - gmail@gmail.com - Full Name - - - admin@javaops.ru - Admin - - - mail@yandex.ru - Deleted - - + + + + Topjava + + + + + + Masterjava + + + Санкт-Петербург + Москва Киев Минск + + Full Name + Admin + Deleted + User1 + User2 + User3 + \ No newline at end of file From 986e68b5d9460f07763298d4692e713f75763f4f Mon Sep 17 00:00:00 2001 From: AlexeyKorban Date: Thu, 20 Sep 2018 21:33:42 +0500 Subject: [PATCH 20/20] =?UTF-8?q?3=5F1=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B8=D0=B5=20=D0=BE=D1=88=D0=B8=D0=B1?= =?UTF-8?q?=D0=BE=D0=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 2_10_Xslt.patch | 95 --------------------------------- src/main/resources/payload2.xsd | 64 ---------------------- 2 files changed, 159 deletions(-) delete mode 100644 2_10_Xslt.patch delete mode 100644 src/main/resources/payload2.xsd diff --git a/2_10_Xslt.patch b/2_10_Xslt.patch deleted file mode 100644 index fceab9e55..000000000 --- a/2_10_Xslt.patch +++ /dev/null @@ -1,95 +0,0 @@ -Index: src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java (revision ) -+++ src/main/java/ru/javaops/masterjava/xml/util/XsltProcessor.java (revision ) -@@ -0,0 +1,43 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import javax.xml.transform.*; -+import javax.xml.transform.stream.StreamResult; -+import javax.xml.transform.stream.StreamSource; -+import java.io.*; -+import java.nio.charset.StandardCharsets; -+ -+public class XsltProcessor { -+ private static TransformerFactory FACTORY = TransformerFactory.newInstance(); -+ private final Transformer xformer; -+ -+ public XsltProcessor(InputStream xslInputStream) { -+ this(new BufferedReader(new InputStreamReader(xslInputStream, StandardCharsets.UTF_8))); -+ } -+ -+ public XsltProcessor(Reader xslReader) { -+ try { -+ Templates template = FACTORY.newTemplates(new StreamSource(xslReader)); -+ xformer = template.newTransformer(); -+ } catch (TransformerConfigurationException e) { -+ throw new IllegalStateException("XSLT transformer creation failed: " + e.toString(), e); -+ } -+ } -+ -+ public String transform(InputStream xmlInputStream) throws TransformerException { -+ StringWriter out = new StringWriter(); -+ transform(xmlInputStream, out); -+ return out.getBuffer().toString(); -+ } -+ -+ public void transform(InputStream xmlInputStream, Writer result) throws TransformerException { -+ transform(new BufferedReader(new InputStreamReader(xmlInputStream, StandardCharsets.UTF_8)), result); -+ } -+ -+ public void transform(Reader sourceReader, Writer result) throws TransformerException { -+ xformer.transform(new StreamSource(sourceReader), new StreamResult(result)); -+ } -+ -+ public static String getXsltHeader(String xslt) { -+ return "\n"; -+ } -+} -Index: src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java (revision ) -+++ src/test/java/ru/javaops/masterjava/xml/util/XsltProcessorTest.java (revision ) -@@ -0,0 +1,18 @@ -+package ru.javaops.masterjava.xml.util; -+ -+import com.google.common.io.Resources; -+import org.junit.Test; -+ -+import java.io.InputStream; -+ -+public class XsltProcessorTest { -+ @Test -+ public void transform() throws Exception { -+ try (InputStream xslInputStream = Resources.getResource("cities.xsl").openStream(); -+ InputStream xmlInputStream = Resources.getResource("payload.xml").openStream()) { -+ -+ XsltProcessor processor = new XsltProcessor(xslInputStream); -+ System.out.println(processor.transform(xmlInputStream)); -+ } -+ } -+} -Index: src/main/resources/cities.xsl -IDEA additional info: -Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP -<+>UTF-8 -=================================================================== ---- src/main/resources/cities.xsl (revision ) -+++ src/main/resources/cities.xsl (revision ) -@@ -0,0 +1,9 @@ -+ -+ -+ -+ -+ -+ -+ -+ -+ -\ No newline at end of file diff --git a/src/main/resources/payload2.xsd b/src/main/resources/payload2.xsd deleted file mode 100644 index 5273497c5..000000000 --- a/src/main/resources/payload2.xsd +++ /dev/null @@ -1,64 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file