@SuppressWarnings("unchecked") private List<String> yamlToList(Object yamlObject) throws InvalidParameterException { if (yamlObject == null) { return Collections.emptyList(); } else if (yamlObject instanceof List) { return (List<String>) yamlObject; } else if (yamlObject instanceof String) { return Lists.newArrayList((String) yamlObject); } else { throw new InvalidParameterException("Unable to cast object to List<String>"); } }
private static Number parseNumber(String s) throws InvalidParameterException { try { return Integer.parseInt(s); } catch (NumberFormatException e) { // Ignore } try { return Double.parseDouble(s); } catch (NumberFormatException e) { // Ignore } throw new InvalidParameterException("Couldn't parse number"); }
@SuppressWarnings("unchecked") private List<String> yamlToList(Object yamlObject) throws InvalidParameterException { if (yamlObject == null) { return Collections.emptyList(); } else if (yamlObject instanceof List) { return (List<String>) yamlObject; } else if (yamlObject instanceof String) { return Lists.newArrayList((String) yamlObject); } else { throw new InvalidParameterException("Unable to cast object to List<String>"); } }
private static Number parseNumber(String s) throws InvalidParameterException { try { return Integer.parseInt(s); } catch (NumberFormatException e) { // Ignore } try { return Double.parseDouble(s); } catch (NumberFormatException e) { // Ignore } throw new InvalidParameterException("Couldn't parse number"); }
@Override protected void doInitialize(UimaContext context) throws ResourceInitializationException { if (file == null || !file.canRead() || !file.isFile()) { throw new ResourceInitializationException( new InvalidParameterException("Specified parameter '" + PARAM_FILE + "' was not valid")); } try { br = new BufferedReader( new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); } catch (IOException ioe) { throw new ResourceInitializationException(ioe); } }
@Override public void init(Resource connection, Map<String, Object> config) throws BaleenException { try { country = (SharedCountryResource) connection; } catch (ClassCastException cce) { throw new InvalidParameterException( "Unable to cast connection parameter to SharedCountryResource", cce); } super.init(connection, config); }
@Override public void init(Resource connection, Map<String, Object> config) throws BaleenException { try { country = (SharedCountryResource) connection; } catch (ClassCastException cce) { throw new InvalidParameterException( "Unable to cast connection parameter to SharedCountryResource", cce); } super.init(connection, config); }
/** * Takes a string of the class name and return a Class. First tries looking in the default * packages, and then if not found it will assume the class is fully qualified and try to use the * name as it is provided * * @param className The name of the class * @param type The type that the class should extend * @param defaultPackage The package to look in if the className isn't a fully qualified name * @return The class specified */ public static <S extends T, T> Class<S> getClassFromString( String className, String... defaultPackage) throws InvalidParameterException { for (String pkg : defaultPackage) { try { return (Class<S>) Class.forName(pkg + "." + className); } catch (Exception e) { LOGGER.debug("Couldn't find class {} in package {}", className, pkg, e); } } try { return (Class<S>) Class.forName(className); } catch (Exception e) { throw new InvalidParameterException("Could not find or instantiate class " + className, e); } }
/** * Take the user specified location and test to see whether it exists and whether we can access it * or not. * * @param location The location of the configuration file * @return An optional containing the file if found, or empty otherwise * @throws InvalidParameterException If the configuration file exists but can't be accessed */ private Optional<File> getConfigurationFile(String location) throws InvalidParameterException { if (location != null && !location.isEmpty()) { File file = new File(location); if (!file.exists() || !file.isFile()) { throw new InvalidParameterException( "Unable to access the configuration file at " + file.getAbsolutePath()); } else { return Optional.of(file); } } return Optional.empty(); } }
/** * Takes a string of the class name and return a Class. First tries looking in the default * packages, and then if not found it will assume the class is fully qualified and try to use the * name as it is provided * * @param className The name of the class * @param type The type that the class should extend * @param defaultPackage The package to look in if the className isn't a fully qualified name * @return The class specified */ public static <S extends T, T> Class<S> getClassFromString( String className, String... defaultPackage) throws InvalidParameterException { for (String pkg : defaultPackage) { try { return (Class<S>) Class.forName(pkg + "." + className); } catch (Exception e) { LOGGER.debug("Couldn't find class {} in package {}", className, pkg, e); } } try { return (Class<S>) Class.forName(className); } catch (Exception e) { throw new InvalidParameterException("Could not find or instantiate class " + className, e); } }
/** Takes an object of unknown type and attempts to parse it to a Double. */ public static Double parseToDouble(Object obj) throws InvalidParameterException { Double ret; if (obj == null) { ret = null; } else if (Double.class.isAssignableFrom(obj.getClass())) { ret = (Double) obj; } else if (Long.class.isAssignableFrom(obj.getClass())) { Long l = (Long) obj; ret = l.doubleValue(); } else if (Integer.class.isAssignableFrom(obj.getClass())) { Integer i = (Integer) obj; ret = i.doubleValue(); } else if (String.class.isAssignableFrom(obj.getClass())) { try { ret = Double.parseDouble((String) obj); } catch (NumberFormatException nfe) { throw new InvalidParameterException("String is not numeric", nfe); } } else { throw new InvalidParameterException("Object is not numeric"); } return ret; }
/** * Takes a string of the class name and return a Class. First tries looking in the default * packages, and then if not found it will assume the class is fully qualified and try to use the * name as it is provided * * @param className The name of the class * @param type The type that the class should extend * @param defaultPackage The package to look in if the className isn't a fully qualified name * @return The class specified */ @SuppressWarnings("unchecked") protected <S extends T, T> Class<S> getClassFromString(String className, String... defaultPackage) throws InvalidParameterException { for (String pkg : defaultPackage) { try { return (Class<S>) Class.forName(pkg + "." + className); } catch (Exception e) { LoggerFactory.getLogger(clazz) .debug("Couldn't find class {} in package {}", className, pkg, e); } } try { return (Class<S>) Class.forName(className); } catch (Exception e) { throw new InvalidParameterException( "Could not find or instantiate analysis engine " + className, e); } }
/** * Takes a string of the class name and return a Class. First tries looking in the default * packages, and then if not found it will assume the class is fully qualified and try to use the * name as it is provided * * @param className The name of the class * @param type The type that the class should extend * @param defaultPackage The package to look in if the className isn't a fully qualified name * @return The class specified */ @SuppressWarnings("unchecked") protected <S extends T, T> Class<S> getClassFromString(String className, String... defaultPackage) throws InvalidParameterException { for (String pkg : defaultPackage) { try { return (Class<S>) Class.forName(pkg + "." + className); } catch (Exception e) { LoggerFactory.getLogger(clazz) .debug("Couldn't find class {} in package {}", className, pkg, e); } } try { return (Class<S>) Class.forName(className); } catch (Exception e) { throw new InvalidParameterException( "Could not find or instantiate analysis engine " + className, e); } }
/** Takes an object of unknown type and attempts to parse it to a Double. */ public static Double parseToDouble(Object obj) throws InvalidParameterException { Double ret; if (obj == null) { ret = null; } else if (Double.class.isAssignableFrom(obj.getClass())) { ret = (Double) obj; } else if (Long.class.isAssignableFrom(obj.getClass())) { Long l = (Long) obj; ret = l.doubleValue(); } else if (Integer.class.isAssignableFrom(obj.getClass())) { Integer i = (Integer) obj; ret = i.doubleValue(); } else if (String.class.isAssignableFrom(obj.getClass())) { try { ret = Double.parseDouble((String) obj); } catch (NumberFormatException nfe) { throw new InvalidParameterException("String is not numeric", nfe); } } else { throw new InvalidParameterException("Object is not numeric"); } return ret; }
/** * Takes a string of the class name and return a Class * * @param className The name of the class, which must implement IContentExtractor * @return The class specified */ public static IContentExtractor getContentExtractor(String className) throws InvalidParameterException { try { return (IContentExtractor) BuilderUtils.getClassFromString( className, BaleenDefaults.DEFAULT_CONTENT_EXTRACTOR_PACKAGE) .newInstance(); } catch (Exception e1) { throw new InvalidParameterException( "Could not find or instantiate content extractor " + className, e1); } }
private void control(BaleenPipeline pipeline, String command) throws InvalidParameterException { switch (command.toLowerCase()) { case "pause": pipeline.pause(); break; case "unpause": pipeline.unpause(); break; default: throw new InvalidParameterException("Unexpected command '" + command + "'"); } }
/** * Configure a new instance of FileGazetteer. The following config parameters are * expected/allowed: * * <ul> * <li><b>fileName</b> - What file are we using for the gazetteer; defaults to gazetteer.txt * <li><b>termSeparator</b> - The string that separates aliases of the same entity on a single * line in the gazetteer. Defaults to "," * </ul> * * @param connection A SharedFileResource object to read the file with * @param config A map of additional configuration options */ @Override public void init(Resource connection, Map<String, Object> config) throws BaleenException { if (config.containsKey(CONFIG_FILE)) { file = new File(config.get(CONFIG_FILE).toString()); } else { file = new File("gazetteer.txt"); } if (config.containsKey(CONFIG_TERM_SEPARATOR)) { termSeparator = config.get(CONFIG_TERM_SEPARATOR).toString(); } if (!file.exists() || !file.canRead()) { throw new InvalidParameterException("Unable to read file " + file.getPath()); } super.init(connection, config); }
@Override protected void doInitialize(UimaContext context) throws ResourceInitializationException { if (file == null || !file.canRead() || !file.isFile()) { throw new ResourceInitializationException( new InvalidParameterException("Specified parameter '" + PARAM_FILE + "' was not valid")); } try { br = new BufferedReader( new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)); } catch (IOException ioe) { throw new ResourceInitializationException(ioe); } try { extractor = getContentExtractor(contentExtractor); } catch (InvalidParameterException ipe) { throw new ResourceInitializationException(ipe); } extractor.initialize(context, getConfigParameters(context)); }
private void control(BaleenPipeline pipeline, String command) throws InvalidParameterException { switch (command.toLowerCase()) { case "pause": pipeline.pause(); break; case "unpause": pipeline.unpause(); break; default: throw new InvalidParameterException("Unexpected command '" + command + "'"); } }
/** * Configure a new instance of FileGazetteer. The following config parameters are * expected/allowed: * * <ul> * <li><b>fileName</b> - What file are we using for the gazetteer; defaults to gazetteer.txt * <li><b>termSeparator</b> - The string that separates aliases of the same entity on a single * line in the gazetteer. Defaults to "," * </ul> * * @param connection A SharedFileResource object to read the file with * @param config A map of additional configuration options */ @Override public void init(Resource connection, Map<String, Object> config) throws BaleenException { if (config.containsKey(CONFIG_FILE)) { file = new File(config.get(CONFIG_FILE).toString()); } else { file = new File("gazetteer.txt"); } if (config.containsKey(CONFIG_TERM_SEPARATOR)) { termSeparator = config.get(CONFIG_TERM_SEPARATOR).toString(); } if (!file.exists() || !file.canRead()) { throw new InvalidParameterException("Unable to read file " + file.getPath()); } super.init(connection, config); }