@SuppressWarnings("MessageMissingOnJUnitAssertion") @Test public void testConcurrentExtractFromXml() throws Exception { File tempDir = null; try { tempDir = FileUtil.createTemporaryDirectory("test-xml"); File testFile = new File(tempDir, "test.xml"); FileUtil.writeFile(testFile, getBytes("test.xml")); int concurrency = 4 * Runtime.getRuntime().availableProcessors(); List<Thread> threads = new ArrayList<>(); for (int i = 0; i < concurrency; i++) { threads.add(new Thread(() -> { for (int i1 = 0; i1 < 100; i1++) { try { Assert.assertEquals("false", XmlUtil.extractFromXml(testFile, "/a/c/@boolAttr", String.class)); } catch (IOException e) { throw new RuntimeException(e); } } })); } threads.forEach(Thread::start); for (Thread thread : threads) { thread.join(); } } finally { FileUtil.deleteTotallyAsync(tempDir); } }
public static <T> T extractFromXml(@Nonnull File xmlFile, String xPath, Class<T> clazz) throws IOException { return Objects.requireNonNull(FileUtil.executeIoOperation(new ThreadUtil.Operation<T>() { @Nonnull @Override public T run() throws IOException { try { return internalExtractFromXml(new FileInputStream(xmlFile), xPath, clazz); } catch (FileNotFoundException e) { throw new IOException(String.format( "Can't find file '%s' while evaluating XPath '%s'.", xmlFile.getCanonicalPath(), xPath ), e); } } })); }
XmlUtil { public static <T> T extractFromXml(@Nonnull File xmlFile, String xPath, Class<T> clazz) throws IOException { return Objects.requireNonNull(FileUtil.executeIoOperation(new ThreadUtil.Operation<T>() { @Nonnull @Override public T run() throws IOException { try { return internalExtractFromXml(new FileInputStream(xmlFile), xPath, clazz); } catch (FileNotFoundException e) { throw new IOException(String.format( "Can't find file '%s' while evaluating XPath '%s'.", xmlFile.getCanonicalPath(), xPath ), e); } } })); } }
XmlUtil { public static <T> T extractFromXml(@Nonnull File xmlFile, String xPath, Class<T> clazz) throws IOException { return Objects.requireNonNull(FileUtil.executeIoOperation(new ThreadUtil.Operation<T>() { @Nonnull @Override public T run() throws IOException { try { return internalExtractFromXml(new FileInputStream(xmlFile), xPath, clazz); } catch (FileNotFoundException e) { throw new IOException(String.format( "Can't find file '%s' while evaluating XPath '%s'.", xmlFile.getCanonicalPath(), xPath ), e); } } })); } private  XmlUtil(); }
XmlUtil { public static <T> T extractFromXml(@Nonnull File xmlFile, String xPath, Class<T> clazz) throws IOException { return Objects.requireNonNull(FileUtil.executeIoOperation(new ThreadUtil.Operation<T>() { @Nonnull @Override public T run() throws IOException { try { return internalExtractFromXml(new FileInputStream(xmlFile), xPath, clazz); } catch (FileNotFoundException e) { throw new IOException(String.format( "Can't find file '%s' while evaluating XPath '%s'.", xmlFile.getCanonicalPath(), xPath ), e); } } })); } private  XmlUtil(); static T extractFromXml(@Nonnull File xmlFile, String xPath, Class<T> clazz); @Nonnull static T extractFromXml(InputStream xmlInputStream, String xPath, Class<T> clazz); static void writeXml(@Nonnull File xmlFile, @Nonnull Document document); static void writeXml(OutputStream xmlOutputStream, @Nonnull Document document); static void updateXml(@Nonnull File xmlFile, String xPath, String value); static void updateXml(
            InputStream xmlInputStream, OutputStream xmlOutputStream, String xPath, String value); static void updateText(@Nonnull File xmlFile, String xPath, @Nullable String value); static void updateText(
            InputStream xmlInputStream, OutputStream xmlOutputStream, String xPath,
            @Nullable String value); static void ensureXmlElementExists(
            @Nonnull File xmlFile, @Nonnull String parentElementXPath, @Nonnull String elementName,
            @Nonnull Map<String, String> filterAttributes, @Nullable Map<String, String> newAttributes,
            @Nullable Set<String> obsoleteAttributes); static void ensureXmlElementExists(
            @Nonnull InputStream xmlInputStream, @Nonnull OutputStream xmlOutputStream,
            @Nonnull String parentElementXPath, @Nonnull String elementName,
            @Nonnull Map<String, String> filterAttributes, @Nullable Map<String, String> newAttributes,
            @Nullable Set<String> obsoleteAttributes); static void removeElementsIfExists(@Nonnull File xmlFile, @Nonnull String elementXPath); static void removeElementsIfExists(
            @Nonnull InputStream xmlInputStream, @Nonnull OutputStream xmlOutputStream,
            @Nonnull String elementXPath); @Contract("null -> null; !null -> !null") @Nullable static String formatEnumValueForXml(@Nullable Enum enumValue); @Contract("null, _ -> null") @Nullable static T extractEnumValueFromXml(@Nullable String enumFormat, Class<T> enumClass); static boolean hasOnlyTextSiblings(@Nonnull Node node); static boolean hasOnlyTextChildren(@Nonnull Node node); @Nonnull static Document newDomDocument(); static XPathFactory newXPathFactory(); static TransformerFactory newTransformerFactory(); static DocumentBuilderFactory newDocumentBuilderFactory(); static Object evaluateXPath(InputStream xmlInputStream, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(Document document, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(Element element, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(InputStream xmlInputStream, @Nonnull XPathExpression xPath); static Object evaluateXPath(Document document, @Nonnull XPathExpression xPath); static Object evaluateXPath(Element element, @Nonnull XPathExpression xPath); static Map<String, String> toMap(String xml); }
XmlUtil { public static <T> T extractFromXml(@Nonnull File xmlFile, String xPath, Class<T> clazz) throws IOException { return Objects.requireNonNull(FileUtil.executeIoOperation(new ThreadUtil.Operation<T>() { @Nonnull @Override public T run() throws IOException { try { return internalExtractFromXml(new FileInputStream(xmlFile), xPath, clazz); } catch (FileNotFoundException e) { throw new IOException(String.format( "Can't find file '%s' while evaluating XPath '%s'.", xmlFile.getCanonicalPath(), xPath ), e); } } })); } private  XmlUtil(); static T extractFromXml(@Nonnull File xmlFile, String xPath, Class<T> clazz); @Nonnull static T extractFromXml(InputStream xmlInputStream, String xPath, Class<T> clazz); static void writeXml(@Nonnull File xmlFile, @Nonnull Document document); static void writeXml(OutputStream xmlOutputStream, @Nonnull Document document); static void updateXml(@Nonnull File xmlFile, String xPath, String value); static void updateXml(
            InputStream xmlInputStream, OutputStream xmlOutputStream, String xPath, String value); static void updateText(@Nonnull File xmlFile, String xPath, @Nullable String value); static void updateText(
            InputStream xmlInputStream, OutputStream xmlOutputStream, String xPath,
            @Nullable String value); static void ensureXmlElementExists(
            @Nonnull File xmlFile, @Nonnull String parentElementXPath, @Nonnull String elementName,
            @Nonnull Map<String, String> filterAttributes, @Nullable Map<String, String> newAttributes,
            @Nullable Set<String> obsoleteAttributes); static void ensureXmlElementExists(
            @Nonnull InputStream xmlInputStream, @Nonnull OutputStream xmlOutputStream,
            @Nonnull String parentElementXPath, @Nonnull String elementName,
            @Nonnull Map<String, String> filterAttributes, @Nullable Map<String, String> newAttributes,
            @Nullable Set<String> obsoleteAttributes); static void removeElementsIfExists(@Nonnull File xmlFile, @Nonnull String elementXPath); static void removeElementsIfExists(
            @Nonnull InputStream xmlInputStream, @Nonnull OutputStream xmlOutputStream,
            @Nonnull String elementXPath); @Contract("null -> null; !null -> !null") @Nullable static String formatEnumValueForXml(@Nullable Enum enumValue); @Contract("null, _ -> null") @Nullable static T extractEnumValueFromXml(@Nullable String enumFormat, Class<T> enumClass); static boolean hasOnlyTextSiblings(@Nonnull Node node); static boolean hasOnlyTextChildren(@Nonnull Node node); @Nonnull static Document newDomDocument(); static XPathFactory newXPathFactory(); static TransformerFactory newTransformerFactory(); static DocumentBuilderFactory newDocumentBuilderFactory(); static Object evaluateXPath(InputStream xmlInputStream, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(Document document, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(Element element, @Nonnull XPathExpression xPath, QName returnType); static Object evaluateXPath(InputStream xmlInputStream, @Nonnull XPathExpression xPath); static Object evaluateXPath(Document document, @Nonnull XPathExpression xPath); static Object evaluateXPath(Element element, @Nonnull XPathExpression xPath); static Map<String, String> toMap(String xml);  }