Author: fanningpj
Date: Mon Jan  1 20:50:31 2024
New Revision: 1915048

URL: http://svn.apache.org/viewvc?rev=1915048&view=rev
Log:
[XMLBEANS-617] add recurseSourceSubdirs support to maven plugin

Added:
    xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/FileUtil.java
    xmlbeans/trunk/src/test/java/org/apache/xmlbeans/impl/tool/FileUtilTest.java
Modified:
    xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java
    xmlbeans/trunk/src/main/java9/module-info.class
    xmlbeans/trunk/src/main/maven/plugin.xml

Added: xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/FileUtil.java
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/FileUtil.java?rev=1915048&view=auto
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/FileUtil.java 
(added)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/FileUtil.java 
Mon Jan  1 20:50:31 2024
@@ -0,0 +1,48 @@
+/*   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.xmlbeans.impl.tool;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+final class FileUtil {
+
+    private FileUtil() {}
+
+    /**
+     * @param base Directory where to look for files
+     * @param pattern Regex pattern the files must match to be included
+     * @param searchSubdirectories If true, search all subdirectories. 
Otherwise, only search base
+     * @return Collection of files in base and possibly subdirectories that 
match pattern
+     */
+    static Collection<File> find(File base, Pattern pattern, boolean 
searchSubdirectories) {
+        try (Stream<Path> pathStream = Files.find(base.toPath(), 
searchSubdirectories ? Integer.MAX_VALUE : 1, (path, atts) -> {
+            String name = path.getFileName().toString();
+            return !name.endsWith(".xsdconfig") && 
pattern.matcher(name).matches();
+        })) {
+            return pathStream.map(Path::toFile).collect(Collectors.toList());
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+}

Modified: 
xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java?rev=1915048&r1=1915047&r2=1915048&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java 
(original)
+++ xmlbeans/trunk/src/main/java/org/apache/xmlbeans/impl/tool/MavenPlugin.java 
Mon Jan  1 20:50:31 2024
@@ -59,6 +59,10 @@ public class MavenPlugin extends Abstrac
     @Parameter( defaultValue = "*.xsd,*.wsdl,*.java" )
     private String sourceSchemas;
 
+    /** recurseSourceSubdirs determines if subdirectories of sourceDir are 
checked for schemas */
+    @Parameter( defaultValue = "false" )
+    private boolean recurseSourceSubdirs;
+
     /** xmlConfigs points to your xmlconfig.xml file */
     @Parameter( defaultValue = "${project.basedir}/src/schema/xmlconfig.xml" )
     private String xmlConfigs;
@@ -211,14 +215,12 @@ public class MavenPlugin extends Abstrac
         // if sourceSchemas is not specified use all found schemas
         // otherwise convert comma-separated string to regex including glob 
parameter
         Pattern pat = Pattern.compile(sourceSchemas == null ? ".*" :
-            "(" + sourceSchemas
-                .replace(",","|")
-                .replace(".", "\\.")
-                .replace("*",".*") +
-            ")");
-
-        File[] schemaFiles = Objects.requireNonNull(base.listFiles((dir, name) 
->
-            !name.endsWith(".xsdconfig") && pat.matcher(name).matches()));
+                "(" + sourceSchemas
+                        .replace(",","|")
+                        .replace(".", "\\.")
+                        .replace("*",".*") +
+                        ")");
+        final Collection<File> schemaFiles = FileUtil.find(base, pat, 
recurseSourceSubdirs);
         for (File sf : schemaFiles) {
             String name = sf.getName();
             switch (name.replaceAll(".*\\.", "")) {
@@ -239,9 +241,9 @@ public class MavenPlugin extends Abstrac
 
         if (buildSchemas) {
             List<File> configs = (xmlConfigs == null || xmlConfigs.isEmpty()) 
? Collections.emptyList()
-                : Stream.of(xmlConfigs.split(",")).flatMap(s ->
+                    : Stream.of(xmlConfigs.split(",")).flatMap(s ->
                     Stream.of(new File(s), new File(base, 
s)).filter(File::exists)
-                ).collect(Collectors.toList());
+            ).collect(Collectors.toList());
 
             List<File> classPathList = new ArrayList<>();
             List<URL> urls = new ArrayList<>();
@@ -304,7 +306,7 @@ public class MavenPlugin extends Abstrac
 
             if (!result) {
                 throw new MojoFailureException("Schema compilation failed!\n"+
-                    
errorList.stream().map(XmlError::toString).collect(Collectors.joining("\n"))
+                        
errorList.stream().map(XmlError::toString).collect(Collectors.joining("\n"))
                 );
             }
 

Modified: xmlbeans/trunk/src/main/java9/module-info.class
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/java9/module-info.class?rev=1915048&r1=1915047&r2=1915048&view=diff
==============================================================================
Binary files - no diff available.

Modified: xmlbeans/trunk/src/main/maven/plugin.xml
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/main/maven/plugin.xml?rev=1915048&r1=1915047&r2=1915048&view=diff
==============================================================================
--- xmlbeans/trunk/src/main/maven/plugin.xml (original)
+++ xmlbeans/trunk/src/main/maven/plugin.xml Mon Jan  1 20:50:31 2024
@@ -168,6 +168,13 @@
                     <description>sourceSchemas is a comma-delimited list of 
all the schemas you want to compile</description>
                 </parameter>
                 <parameter>
+                    <name>recurseSourceSubdirs</name>
+                    <type>boolean</type>
+                    <required>false</required>
+                    <editable>true</editable>
+                    <description>recurseSourceSubdirs determines if 
subdirectories of sourceDir are checked for schemas</description>
+                </parameter>
+                <parameter>
                     <name>xmlConfigs</name>
                     <type>java.lang.String</type>
                     <required>false</required>
@@ -363,6 +370,7 @@
                 <buildSchemas implementation="boolean" default-value="true"/>
                 <sourceDir implementation="java.lang.String" 
default-value="${project.basedir}/src/main/schema"/>
                 <sourceSchemas implementation="java.lang.String" 
default-value="*.xsd,*.wsdl,*.java"/>
+                <recurseSourceSubdirs implementation="boolean" 
default-value="false"/>
                 <xmlConfigs implementation="java.lang.String" 
default-value="${project.basedir}/src/schema/xmlconfig.xml"/>
                 <javaTargetDir implementation="java.lang.String" 
default-value="${project.basedir}/target/generated-sources"/>
                 <classTargetDir implementation="java.lang.String" 
default-value="${project.basedir}/target/generated-resources"/>
@@ -422,4 +430,4 @@
             <version>3.25.5</version>
         </dependency>
     </dependencies>
-</plugin>
\ No newline at end of file
+</plugin>

Added: 
xmlbeans/trunk/src/test/java/org/apache/xmlbeans/impl/tool/FileUtilTest.java
URL: 
http://svn.apache.org/viewvc/xmlbeans/trunk/src/test/java/org/apache/xmlbeans/impl/tool/FileUtilTest.java?rev=1915048&view=auto
==============================================================================
--- 
xmlbeans/trunk/src/test/java/org/apache/xmlbeans/impl/tool/FileUtilTest.java 
(added)
+++ 
xmlbeans/trunk/src/test/java/org/apache/xmlbeans/impl/tool/FileUtilTest.java 
Mon Jan  1 20:50:31 2024
@@ -0,0 +1,87 @@
+/*   Copyright 2004 The Apache Software Foundation
+ *
+ *   Licensed under the Apache License, Version 2.0 (the "License");
+ *   you may not use this file except in compliance with the License.
+ *   You may obtain a copy of the License at
+ *
+ *       http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *   Unless required by applicable law or agreed to in writing, software
+ *   distributed under the License is distributed on an "AS IS" BASIS,
+ *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *   See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+
+package org.apache.xmlbeans.impl.tool;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.HashSet;
+import java.util.regex.Pattern;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+public class FileUtilTest {
+
+    private Path root;
+
+    @BeforeEach
+    public void setup() throws IOException {
+        root = Files.createTempDirectory("test");
+    }
+
+    @AfterEach
+    public void cleanup() throws IOException {
+        try (Stream<Path> walk = Files.walk(root)) {
+            walk.sorted(Comparator.reverseOrder())
+                    .map(Path::toFile)
+                    .forEach(File::delete);
+        }
+    }
+
+    @Test
+    public void testFindNoSubdirs() throws IOException {
+        Files.createFile(root.resolve("test1.xsdconfig"));
+        Files.createFile(root.resolve("test1.xsd"));
+        Files.createFile(root.resolve("test1.txt"));
+        Path dir = Files.createDirectory(root.resolve("test"));
+        Files.createFile(dir.resolve("test2.xsdconfig"));
+        Files.createFile(dir.resolve("test2.xsd"));
+        Files.createFile(dir.resolve("test2.txt"));
+
+        Collection<File> files = FileUtil.find(root.toFile(), 
Pattern.compile(".*\\.xsd"), false);
+
+        assertEquals(1, files.size());
+        assertEquals("test1.xsd", files.iterator().next().getName());
+    }
+
+    @Test
+    public void testFindSubdirs() throws IOException {
+        Files.createFile(root.resolve("test1.xsdconfig"));
+        Files.createFile(root.resolve("test1.xsd"));
+        Files.createFile(root.resolve("test1.txt"));
+        Path dir = Files.createDirectory(root.resolve("test"));
+        Files.createFile(dir.resolve("test2.xsdconfig"));
+        Files.createFile(dir.resolve("test2.xsd"));
+        Files.createFile(dir.resolve("test2.txt"));
+
+        Collection<File> files = FileUtil.find(root.toFile(), 
Pattern.compile(".*\\.xsd"), true);
+
+        assertEquals(2, files.size());
+        HashSet<String> expectedSet = new HashSet<>();
+        expectedSet.add("test1.xsd");
+        expectedSet.add("test2.xsd");
+        assertEquals(expectedSet, 
files.stream().map(File::getName).collect(Collectors.toSet()));
+    }
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to