rzo1 commented on code in PR #197:
URL: https://github.com/apache/opennlp-sandbox/pull/197#discussion_r1887406916


##########
opennlp-grpc/opennlp-grpc-service/src/main/java/opennlp/service/classpath/DirectoryModelFinder.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 opennlp.service.classpath;
+
+import java.io.IOException;
+import java.net.JarURLConnection;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Locale;
+import java.util.Objects;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+import org.slf4j.LoggerFactory;
+
+import opennlp.tools.models.AbstractClassPathModelFinder;
+import opennlp.tools.models.ClassPathModelFinder;
+
+/**
+ * The {@code DirectoryModelFinder} class is responsible for finding model 
files in a given directory
+ * on the classpath.
+ *
+ * <p>This class allows searching for models based on wildcard patterns, 
either in plain directory structures
+ * or within JAR files. The search can be performed recursively depending on 
the specified configuration.
+ *
+ * <p><b>Usage:</b>
+ * <ul>
+ *   <li>Provide the prefix for models to be found in JAR files using the 
{@code jarModelPrefix} parameter.</li>
+ *   <li>Specify the directory to search and whether to enable recursive 
scanning.</li>
+ *   <li>The class supports resolving both direct file matches and entries 
within JAR archives.</li>
+ * </ul>
+ *
+ * @see AbstractClassPathModelFinder
+ * @see ClassPathModelFinder
+ */
+public class DirectoryModelFinder extends AbstractClassPathModelFinder 
implements ClassPathModelFinder {
+
+  private static final org.slf4j.Logger logger = 
LoggerFactory.getLogger(DirectoryModelFinder.class);
+
+  private final Path directory;
+  private final boolean recursive;
+
+  /**
+   * Constructs a new {@code DirectoryModelFinder} instance.
+   *
+   * @param jarModelPrefix the prefix for identifying model files in JAR 
archives; may be {@code null}.
+   *                       If it is {@code null}, {@link 
ClassPathModelFinder#OPENNLP_MODEL_JAR_PREFIX} is used.
+   * @param directory      the root directory to search for model files; must 
not be {@code null}.
+   * @param recursive      {@code true} if the search should include 
subdirectories, {@code false} otherwise.
+   * @throws NullPointerException if {@code directory} is {@code null}.
+   */
+  public DirectoryModelFinder(String jarModelPrefix, Path directory, boolean 
recursive) {
+    super(jarModelPrefix == null ? OPENNLP_MODEL_JAR_PREFIX : jarModelPrefix);
+    Objects.requireNonNull(directory, "Given directory must not be NULL");
+    this.directory = directory;
+    this.recursive = recursive;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  protected Object getContext() {
+    return null;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  protected List<URI> getMatchingURIs(String wildcardPattern, Object context) {
+    if (wildcardPattern == null) {
+      return Collections.emptyList();
+    }
+
+    final boolean isWindows = isWindows();
+    final List<URL> cp = getDirectoryContent();
+    final List<URI> cpu = new ArrayList<>();
+    final Pattern jarPattern = Pattern.compile(asRegex("*" + 
getJarModelPrefix()));

Review Comment:
   Cannot be a constant here but we can avoid creating it on each method call.



##########
opennlp-grpc/opennlp-grpc-service/src/main/java/opennlp/service/classpath/DirectoryModelFinder.java:
##########
@@ -0,0 +1,182 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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 opennlp.service.classpath;
+
+import java.io.IOException;
+import java.net.JarURLConnection;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Locale;
+import java.util.Objects;
+import java.util.jar.JarEntry;
+import java.util.jar.JarFile;
+import java.util.regex.Pattern;
+import java.util.stream.Stream;
+
+import org.slf4j.LoggerFactory;
+
+import opennlp.tools.models.AbstractClassPathModelFinder;
+import opennlp.tools.models.ClassPathModelFinder;
+
+/**
+ * The {@code DirectoryModelFinder} class is responsible for finding model 
files in a given directory
+ * on the classpath.
+ *
+ * <p>This class allows searching for models based on wildcard patterns, 
either in plain directory structures
+ * or within JAR files. The search can be performed recursively depending on 
the specified configuration.
+ *
+ * <p><b>Usage:</b>
+ * <ul>
+ *   <li>Provide the prefix for models to be found in JAR files using the 
{@code jarModelPrefix} parameter.</li>
+ *   <li>Specify the directory to search and whether to enable recursive 
scanning.</li>
+ *   <li>The class supports resolving both direct file matches and entries 
within JAR archives.</li>
+ * </ul>
+ *
+ * @see AbstractClassPathModelFinder
+ * @see ClassPathModelFinder
+ */
+public class DirectoryModelFinder extends AbstractClassPathModelFinder 
implements ClassPathModelFinder {
+
+  private static final org.slf4j.Logger logger = 
LoggerFactory.getLogger(DirectoryModelFinder.class);
+
+  private final Path directory;
+  private final boolean recursive;
+
+  /**
+   * Constructs a new {@code DirectoryModelFinder} instance.
+   *
+   * @param jarModelPrefix the prefix for identifying model files in JAR 
archives; may be {@code null}.
+   *                       If it is {@code null}, {@link 
ClassPathModelFinder#OPENNLP_MODEL_JAR_PREFIX} is used.
+   * @param directory      the root directory to search for model files; must 
not be {@code null}.
+   * @param recursive      {@code true} if the search should include 
subdirectories, {@code false} otherwise.
+   * @throws NullPointerException if {@code directory} is {@code null}.
+   */
+  public DirectoryModelFinder(String jarModelPrefix, Path directory, boolean 
recursive) {
+    super(jarModelPrefix == null ? OPENNLP_MODEL_JAR_PREFIX : jarModelPrefix);
+    Objects.requireNonNull(directory, "Given directory must not be NULL");
+    this.directory = directory;
+    this.recursive = recursive;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  protected Object getContext() {
+    return null;
+  }
+
+  /**
+   * {@inheritDoc}
+   */
+  @Override
+  protected List<URI> getMatchingURIs(String wildcardPattern, Object context) {
+    if (wildcardPattern == null) {
+      return Collections.emptyList();
+    }
+
+    final boolean isWindows = isWindows();
+    final List<URL> cp = getDirectoryContent();
+    final List<URI> cpu = new ArrayList<>();
+    final Pattern jarPattern = Pattern.compile(asRegex("*" + 
getJarModelPrefix()));
+    final Pattern filePattern = Pattern.compile(asRegex("*" + 
wildcardPattern));

Review Comment:
   Cannot be a constant here but we can avoid creating it on each method call.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscr...@opennlp.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to