예제 #1
0
 /**
  * Parses s and recreates the KeywordGroup from it.
  *
  * @param s The String representation obtained from KeywordGroup.toString()
  */
 public static AbstractGroup fromString(String s, BibtexDatabase db, int version)
     throws Exception {
   if (!s.startsWith(KeywordGroup.ID)) {
     throw new Exception(
         "Internal error: KeywordGroup cannot be created from \""
             + s
             + "\". "
             + "Please report this on www.sf.net/projects/jabref");
   }
   QuotedStringTokenizer tok =
       new QuotedStringTokenizer(
           s.substring(KeywordGroup.ID.length()),
           AbstractGroup.SEPARATOR,
           AbstractGroup.QUOTE_CHAR);
   switch (version) {
     case 0:
       {
         String name = tok.nextToken();
         String field = tok.nextToken();
         String expression = tok.nextToken();
         // assume caseSensitive=false and regExp=true for old groups
         return new KeywordGroup(
             StringUtil.unquote(name, AbstractGroup.QUOTE_CHAR),
             StringUtil.unquote(field, AbstractGroup.QUOTE_CHAR),
             StringUtil.unquote(expression, AbstractGroup.QUOTE_CHAR),
             false,
             true,
             GroupHierarchyType.INDEPENDENT);
       }
     case 1:
     case 2:
       {
         String name = tok.nextToken();
         String field = tok.nextToken();
         String expression = tok.nextToken();
         boolean caseSensitive = Integer.parseInt(tok.nextToken()) == 1;
         boolean regExp = Integer.parseInt(tok.nextToken()) == 1;
         return new KeywordGroup(
             StringUtil.unquote(name, AbstractGroup.QUOTE_CHAR),
             StringUtil.unquote(field, AbstractGroup.QUOTE_CHAR),
             StringUtil.unquote(expression, AbstractGroup.QUOTE_CHAR),
             caseSensitive,
             regExp,
             GroupHierarchyType.INDEPENDENT);
       }
     case 3:
       {
         String name = tok.nextToken();
         int context = Integer.parseInt(tok.nextToken());
         String field = tok.nextToken();
         String expression = tok.nextToken();
         boolean caseSensitive = Integer.parseInt(tok.nextToken()) == 1;
         boolean regExp = Integer.parseInt(tok.nextToken()) == 1;
         return new KeywordGroup(
             StringUtil.unquote(name, AbstractGroup.QUOTE_CHAR),
             StringUtil.unquote(field, AbstractGroup.QUOTE_CHAR),
             StringUtil.unquote(expression, AbstractGroup.QUOTE_CHAR),
             caseSensitive,
             regExp,
             GroupHierarchyType.getByNumber(context));
       }
     default:
       throw new UnsupportedVersionException("KeywordGroup", version);
   }
 }
예제 #2
0
  /**
   * Recursive worker method for the populateGroupsTable methods.
   *
   * @param cursor The current GroupTreeNode in the GroupsTree
   * @param parentID The integer ID associated with the cursors's parent node
   * @param currentID The integer value to associate with the cursor
   * @param out The output (PrintStream or Connection) object to which the DML should be written.
   * @param database_id Id of jabref database to which the groups/entries are part of
   */
  private int populateGroupsTable(
      GroupTreeNode cursor, int parentID, int currentID, Object out, int database_id)
      throws SQLException {

    AbstractGroup group = cursor.getGroup();
    String searchField = null;
    String searchExpr = null;
    String caseSens = null;
    String reg_exp = null;
    GroupHierarchyType hierContext = group.getHierarchicalContext();
    if (group instanceof KeywordGroup) {
      searchField = ((KeywordGroup) group).getSearchField();
      searchExpr = ((KeywordGroup) group).getSearchExpression();
      caseSens = ((KeywordGroup) group).isCaseSensitive() ? "1" : "0";
      reg_exp = ((KeywordGroup) group).isRegExp() ? "1" : "0";
    } else if (group instanceof SearchGroup) {
      searchExpr = ((SearchGroup) group).getSearchExpression();
      caseSens = ((SearchGroup) group).isCaseSensitive() ? "1" : "0";
      reg_exp = ((SearchGroup) group).isRegExp() ? "1" : "0";
    }
    // Protect all quotes in the group descriptions:
    if (searchField != null) {
      searchField = StringUtil.quote(searchField, "'", '\\');
    }
    if (searchExpr != null) {
      searchExpr = StringUtil.quote(searchExpr, "'", '\\');
    }

    SQLUtil.processQuery(
        out,
        "INSERT INTO groups (label, parent_id, group_types_id, search_field, "
            + "search_expression, case_sensitive, reg_exp, hierarchical_context, database_id) "
            + "VALUES ('"
            + group.getName()
            + "', "
            + parentID
            + ", (SELECT group_types_id FROM group_types where label='"
            + group.getTypeId()
            + "')"
            + ", "
            + (searchField != null ? '\'' + searchField + '\'' : "NULL")
            + ", "
            + (searchExpr != null ? '\'' + searchExpr + '\'' : "NULL")
            + ", "
            + (caseSens != null ? '\'' + caseSens + '\'' : "NULL")
            + ", "
            + (reg_exp != null ? '\'' + reg_exp + '\'' : "NULL")
            + ", "
            + hierContext.ordinal()
            + ", '"
            + database_id
            + "');");
    // recurse on child nodes (depth-first traversal)
    Object response =
        SQLUtil.processQueryWithResults(
            out,
            "SELECT groups_id FROM groups WHERE label='"
                + cursor.getGroup().getName()
                + "' AND database_id='"
                + database_id
                + "' AND parent_id='"
                + parentID
                + "';");
    // setting values to ID and myID to be used in case of textual SQL
    // export
    int myID = currentID;
    if (response instanceof Statement) {
      ResultSet rs = ((Statement) response).getResultSet();
      rs.next();
      myID = rs.getInt("groups_id");
    }
    for (Enumeration<GroupTreeNode> e = cursor.children(); e.hasMoreElements(); ) {
      ++currentID;
      currentID = populateGroupsTable(e.nextElement(), myID, currentID, out, database_id);
    }
    return currentID;
  }