示例#1
0
    /**
     * Parse the parameters of a connection into a CoreNLP properties file that can be passed into
     * {@link StanfordCoreNLP}, and used in the I/O stages.
     *
     * @param httpExchange The http exchange; effectively, the request information.
     * @return A {@link Properties} object corresponding to a combination of default and passed
     *     properties.
     * @throws UnsupportedEncodingException Thrown if we could not decode the key/value pairs with
     *     UTF-8.
     */
    private Properties getProperties(HttpExchange httpExchange)
        throws UnsupportedEncodingException {
      // Load the default properties
      Properties props = new Properties();
      defaultProps
          .entrySet()
          .stream()
          .forEach(
              entry -> props.setProperty(entry.getKey().toString(), entry.getValue().toString()));

      // Try to get more properties from query string.
      Map<String, String> urlParams = getURLParams(httpExchange.getRequestURI());
      if (urlParams.containsKey("properties")) {
        StringUtils.decodeMap(URLDecoder.decode(urlParams.get("properties"), "UTF-8"))
            .entrySet()
            .forEach(entry -> props.setProperty(entry.getKey(), entry.getValue()));
      } else if (urlParams.containsKey("props")) {
        StringUtils.decodeMap(URLDecoder.decode(urlParams.get("properties"), "UTF-8"))
            .entrySet()
            .forEach(entry -> props.setProperty(entry.getKey(), entry.getValue()));
      }

      // Make sure the properties compile
      props.setProperty(
          "annotators",
          StanfordCoreNLP.ensurePrerequisiteAnnotators(
              props.getProperty("annotators").split("[, \t]+")));

      return props;
    }
  private List<String> getTopicTweets(HttpServletRequest request) throws ServletException {

    final String topicFile = request.getParameter("topicFile");
    final String fullFilePath = topicFile;
    List<String> listFiles = new ArrayList<String>();

    Properties properties = new Properties();

    try {
      properties.load(new FileInputStream(fullFilePath));
    } catch (FileNotFoundException e) {
      LOGGER.error("missing file for in topic tweets: '" + fullFilePath + "'" + e.getMessage());
      e.printStackTrace();
    } catch (IOException e) {
      LOGGER.error("can't read file for in topic tweets: '" + fullFilePath + "'" + e.getMessage());
      e.printStackTrace();
    }

    for (Entry<Object, Object> propertyEntry : properties.entrySet()) {
      String key = (String) propertyEntry.getKey();
      if (key.startsWith("inTopic")) {
        listFiles.add((String) propertyEntry.getValue());
      }
    }

    List<String> tweets = new ArrayList<String>();
    for (String filtFile : listFiles) {
      tweets.addAll(parseTweetsFromFile(filtFile));
    }
    return tweets;
  }
  private List<String> getOutOfTopicTweets(HttpServletRequest request) throws ServletException {

    String topicFile = request.getParameter("topicFile");

    List<String> listFiles = new ArrayList<String>();
    Properties properties = new Properties();
    try {
      properties.load(new FileInputStream(topicFile));
    } catch (FileNotFoundException e) {
      final String emsg =
          "missing file for out of topic tweets: '" + topicFile + "'" + e.getMessage();
      LOGGER.error(emsg);
      throw new ServletException(emsg, e);
    } catch (IOException e) {
      final String emsg =
          "can't read file for out of topic tweets: '" + topicFile + "'" + e.getMessage();
      LOGGER.error(emsg);
      throw new ServletException(emsg);
    }

    for (Entry<Object, Object> propertyEntry : properties.entrySet()) {
      String key = (String) propertyEntry.getKey();

      if (key.startsWith("outOfTopic")) {
        listFiles.add((String) propertyEntry.getValue());
      }
    }

    List<String> tweets = new ArrayList<String>();
    for (String filtFile : listFiles) {
      tweets.addAll(parseTweetsFromFile(filtFile));
    }
    return tweets;
  }
  public void loadFile(String filename) throws IOException {
    final Properties settings = new Properties();
    settings.load(new FileInputStream(filename));

    for (Iterator it = settings.entrySet().iterator(); it.hasNext(); ) {

      Map.Entry setting = (Map.Entry) it.next();
      putAny((String) setting.getKey(), (String) setting.getValue());
    }
  }
示例#5
0
  public void buildCalc(InputStreamReader readerCmdList, CStackCalc calc) throws IOException {
    Properties cmdMap = new Properties(); // Перечень комманд калькулятора для создания.

    cmdMap.load(readerCmdList);
    Hashtable<FieldCmdKind, Object> fieldsValue =
        InitValuesFieldForCmd(); // Перечень пар (тип поля, значение для него) (инициализация полей
                                 // команды).

    for (Map.Entry<Object, Object> icmdList : cmdMap.entrySet()) {
      addCmdInCalc(calc, icmdList.getKey().toString(), icmdList.getValue().toString(), fieldsValue);
    }
  }
 /**
  * When launching a secondary runtime workbench, all projects already in dev mode must continue in
  * dev mode such that their class files are found.
  *
  * @param properties dev.properties
  */
 public static void weaveDevProperties(Properties properties) {
   if (fgIsDev) {
     Properties devProperties = getDevProperties();
     if (devProperties != null) {
       Set<?> entries = devProperties.entrySet();
       Iterator<?> iterator = entries.iterator();
       while (iterator.hasNext()) {
         Entry<?, ?> entry = (Entry<?, ?>) iterator.next();
         properties.setProperty((String) entry.getKey(), (String) entry.getValue());
       }
     }
   }
 }
示例#7
0
 protected Properties filterResponseProps(Properties props) {
   Properties res = new Properties();
   for (Map.Entry ent : props.entrySet()) {
     String key = (String) ent.getKey();
     if (StringUtil.startsWithIgnoreCase(key, "x-lockss")
         || StringUtil.startsWithIgnoreCase(key, "x_lockss")
         || key.equalsIgnoreCase("org.lockss.version.number")) {
       continue;
     }
     // We've lost the original case - capitalize them the way most people
     // expect
     res.put(StringUtil.titleCase(key, '-'), (String) ent.getValue());
   }
   return res;
 }
示例#8
0
  public static void main(final String[] args) throws IOException {
    Properties properties = new Properties();
    int n = args.length - 1;
    for (int i = 0; i < n - 1; ++i) {
      properties.load(new FileInputStream(args[i]));
    }

    for (Map.Entry<Object, Object> entry : properties.entrySet()) {
      MAPPING.put((String) entry.getKey(), (String) entry.getValue());
    }

    final Set<String> unused = new HashSet<String>(MAPPING.keySet());

    File f = new File(args[n - 1]);
    File d = new File(args[n]);

    optimize(
        f,
        d,
        new SimpleRemapper(MAPPING) {
          @Override
          public String map(String key) {
            String s = super.map(key);
            if (s != null) {
              unused.remove(key);
            }
            return s;
          }
        });

    Iterator<String> i = unused.iterator();
    while (i.hasNext()) {
      String s = i.next();
      if (!s.endsWith("/remove")) {
        System.out.println("INFO: unused mapping " + s);
      }
    }
  }
示例#9
0
  /**
   * Add a TdbAu to a TdbTitle and TdbPubisher, and add links to the TdbTitle specified by the
   * properties.
   *
   * @param props the properties
   * @param au the TdbAu to add
   * @throws TdbException if the AU already exists in this Tdb
   */
  private void addTdbAu(Properties props, TdbAu au) throws TdbException {
    // add au for plugin assuming it is not a duplicate
    if (!addTdbAuForPlugin(au)) {
      // au already registered -- report existing au
      TdbAu existingAu = findExistingTdbAu(au);
      String titleName = getTdbTitleName(props, au);
      if (!titleName.equals(existingAu.getTdbTitle().getName())) {
        throw new TdbException(
            "Cannot add duplicate au entry: \""
                + au.getName()
                + "\" for title \""
                + titleName
                + "\" with same definition as existing au entry: \""
                + existingAu.getName()
                + "\" for title \""
                + existingAu.getTdbTitle().getName()
                + "\" to title database");
      } else if (!existingAu.getName().equals(au.getName())) {
        // error because it could lead to a missing AU -- one probably has a typo
        throw new TdbException(
            "Cannot add duplicate au entry: \""
                + au.getName()
                + "\" with the same definition as \""
                + existingAu.getName()
                + "\" for title \""
                + titleName
                + "\" to title database");
      } else {
        throw new TdbException(
            "Cannot add duplicate au entry: \""
                + au.getName()
                + "\" for title \""
                + titleName
                + "\" to title database");
      }
    }

    // get or create the TdbTitle for this
    TdbTitle title = getTdbTitle(props, au);
    try {
      // add AU to title
      title.addTdbAu(au);
    } catch (TdbException ex) {
      // if we can't add au to title, remove for plugin and re-throw exception
      removeTdbAuForPlugin(au);
      throw ex;
    }

    // process title links
    Map<String, Map<String, String>> linkMap = new HashMap<String, Map<String, String>>();
    for (Map.Entry<Object, Object> entry : props.entrySet()) {
      String key = "" + entry.getKey();
      String value = "" + entry.getValue();
      if (key.startsWith("journal.link.")) {
        // skip to link name
        String param = key.substring("link.".length());
        int i;
        if (((i = param.indexOf(".type")) < 0) && ((i = param.indexOf(".journalId")) < 0)) {
          logger.warning(
              "Ignoring nexpected link key for au \"" + au.getName() + "\" key: \"" + key + "\"");
        } else {
          // get link map for linkName
          String lname = param.substring(0, i);
          Map<String, String> lmap = linkMap.get(lname);
          if (lmap == null) {
            lmap = new HashMap<String, String>();
            linkMap.put(lname, lmap);
          }
          // add name and value to link map for link
          String name = param.substring(i + 1);
          lmap.put(name, value);
        }
      }
    }

    // add links to title from accumulated "type", "journalId" entries
    for (Map<String, String> lmap : linkMap.values()) {
      String name = lmap.get("type");
      String value = lmap.get("journalId");
      if ((name != null) && (value != null)) {
        try {
          TdbTitle.LinkType linkType = TdbTitle.LinkType.valueOf(name);
          title.addLinkToTdbTitleId(linkType, value);
        } catch (IllegalArgumentException ex) {
          logger.warning(
              "Ignoring unknown link type for au \"" + au.getName() + "\" name: \"" + name + "\"");
        }
      }
    }
  }
示例#10
0
  /**
   * Create a new TdbAu instance from the properties.
   *
   * @param props the properties
   * @return a TdbAu instance set built from the properties
   */
  private TdbAu newTdbAu(Properties props) {
    String pluginId = (String) props.get("plugin");
    if (pluginId == null) {
      throw new IllegalArgumentException("TdbAu plugin ID not specified");
    }

    String auName = props.getProperty("title");
    if (auName == null) {
      throw new IllegalArgumentException("TdbAu title not specified");
    }

    // create a new TdbAu and set its elements
    TdbAu au = new TdbAu(auName, pluginId);

    // process attrs, and params
    Map<String, Map<String, String>> paramMap = new HashMap<String, Map<String, String>>();
    for (Map.Entry<Object, Object> entry : props.entrySet()) {
      String key = String.valueOf(entry.getKey());
      String value = String.valueOf(entry.getValue());
      if (key.startsWith("attributes.")) {
        // set attributes directly
        String name = key.substring("attributes.".length());
        try {
          au.setAttr(name, value);
        } catch (TdbException ex) {
          logger.warning(
              "Cannot set attribute \"" + name + "\" with value \"" + value + "\" -- ignoring");
        }

      } else if (key.startsWith("param.")) {
        // skip to param name
        String param = key.substring("param.".length());
        int i;
        if (((i = param.indexOf(".key")) < 0) && ((i = param.indexOf(".value")) < 0)) {
          logger.warning(
              "Ignoring unexpected param key for au \""
                  + auName
                  + "\" key: \""
                  + key
                  + "\" -- ignoring");
        } else {
          // get param map for pname
          String pname = param.substring(0, i);
          Map<String, String> pmap = paramMap.get(pname);
          if (pmap == null) {
            pmap = new HashMap<String, String>();
            paramMap.put(pname, pmap);
          }
          // add name and value to param map for pname
          String name = param.substring(i + 1);
          pmap.put(name, value);
        }

      } else if (!key.equals("title") // TdbAu has "name" property
          && !key.equals("plugin") // TdbAu has "pluginId" property
          && !key.equals("journalTitle") // TdbAu has "title" TdbTitle property
          && !key.startsWith("journal.")) { // TdbAu has "title" TdbTitle property
        // translate all other properties into AU properties
        try {
          au.setPropertyByName(key, value);
        } catch (TdbException ex) {
          logger.warning(
              "Cannot set property \"" + key + "\" with value \"" + value + "\" -- ignoring");
        }
      }
    }

    // set param from accumulated "key", and "value" entries
    for (Map<String, String> pmap : paramMap.values()) {
      String name = pmap.get("key");
      String value = pmap.get("value");
      if (name == null) {
        logger.warning("Ignoring property with null name");
      } else if (value == null) {
        logger.warning("Ignoring property \"" + name + "\" with null value");
      } else {
        try {
          au.setParam(name, value);
        } catch (TdbException ex) {
          logger.warning(
              "Cannot set param \"" + name + "\" with value \"" + value + "\" -- ignoring");
        }
      }
    }

    return au;
  }
  public HashMap<String, Properties> generateResourceBundle(String baseName)
      throws GenerationException {
    // be sure to have at least the default URI constant settings
    if (uriGeneration == null) {
      uriGeneration = GenerationSetting.createDefault(caseFormat, "", "");
    }
    Pattern pattern = Pattern.compile(Pattern.quote(getPrefix()) + "(.+)");
    HashMap<String, URI> splitUris = new HashMap<>();
    for (Resource nextSubject : model.subjects()) {
      if (nextSubject instanceof URI) {
        Matcher matcher = pattern.matcher(nextSubject.stringValue());
        if (matcher.find()) {
          String k = matcher.group(1);
          splitUris.put(k, (URI) nextSubject);
        }
      }
    }

    List<String> keys = new ArrayList<>();
    keys.addAll(splitUris.keySet());
    Collections.sort(keys, String.CASE_INSENSITIVE_ORDER);

    HashMap<String, Properties> bundles = new HashMap<>();
    // Default we have for sure
    bundles.put(baseName, new Properties());
    for (String key : keys) {
      final URI resource = splitUris.get(key);
      //
      String nextKey = cleanKey(doCaseFormatting(key, uriGeneration.getCaseFormat()));

      for (URI p : LABEL_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".label")) {
              bundle.put(nextKey + ".label", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }

      for (URI p : COMMENT_PROPERTIES) {
        for (Value v : GraphUtil.getObjects(model, resource, p)) {
          if (v instanceof Literal) {
            final Literal lit = (Literal) v;
            final String lang = lit.getLanguage();
            final Properties bundle;
            if (lang == null) {
              bundle = bundles.get(baseName);
            } else if (bundles.containsKey(baseName + "_" + lang)) {
              bundle = bundles.get(baseName + "_" + lang);
            } else {
              bundle = new Properties();
              bundles.put(baseName + "_" + lang, bundle);
            }

            if (!bundle.containsKey(nextKey + ".comment")) {
              bundle.put(nextKey + ".comment", lit.getLabel().replaceAll("\\s+", " "));
            }
          }
        }
      }
    }

    if (getPreferredLanguage() != null) {
      log.debug("completing default Bundle with preferred language {}", getPreferredLanguage());
      final Properties defaultBundle = bundles.get(baseName);
      final Properties prefBundle = bundles.get(baseName + "_" + getPreferredLanguage());
      if (prefBundle != null) {
        for (Entry<Object, Object> key : prefBundle.entrySet()) {
          String nextKey = (String) key.getKey();
          if (!defaultBundle.containsKey(nextKey)) {
            log.trace("copying {} from {} to default Bundle", nextKey, getPreferredLanguage());
            defaultBundle.setProperty(nextKey, (String) key.getValue());
          }
        }
      } else {
        log.warn("No Bundle data found for preferred language {}", getPreferredLanguage());
      }
    }
    return bundles;
  }
示例#12
0
  /** Generate the XML report from all the test results */
  protected static void generateReport(
      Writer out, String classname, List<TestCase> results, Reader stdout, Reader stderr)
      throws IOException {
    int num_failures = getFailures(results);
    int num_skips = getSkips(results);
    int num_errors = getErrors(results);
    long total_time = getTotalTime(results);

    try {
      out.write(XML_DEF + "\n");

      out.write(
          "\n<testsuite "
              + "name=\""
              + classname
              + "\" "
              + "tests=\""
              + results.size()
              + "\" "
              + "failures=\""
              + num_failures
              + "\" "
              + "errors=\""
              + num_errors
              + "\" "
              + "skips=\""
              + num_skips
              + "\" "
              + "time=\""
              + (total_time / 1000.0)
              + "\">");

      out.write("\n<properties>");
      Properties props = System.getProperties();

      for (Map.Entry<Object, Object> tmp : props.entrySet()) {
        out.write(
            "\n    <property name=\""
                + tmp.getKey()
                + "\""
                + " value=\""
                + tmp.getValue()
                + "\"/>");
      }
      out.write("\n</properties>\n");

      for (TestCase result : results) {
        if (result == null) continue;

        try {
          writeTestCase(out, result);
        } catch (Throwable t) {
          t.printStackTrace();
        }
      }

      if (stdout != null) writeOutput(1, stdout, out);
      if (stderr != null) writeOutput(2, stderr, out);
    } finally {
      out.write("\n</testsuite>\n");
    }
  }