Ejemplo n.º 1
0
  private void typeDeletion(String name) {
    BibtexEntryType type = BibtexEntryType.getType(name);

    if (type instanceof CustomEntryType) {
      if (BibtexEntryType.getStandardType(name) == null) {
        int reply =
            JOptionPane.showConfirmDialog(
                frame,
                Globals.lang(
                    "All entries of this " + "type will be declared " + "typeless. Continue?"),
                Globals.lang("Delete custom format") + " '" + StringUtil.nCase(name) + '\'',
                JOptionPane.YES_NO_OPTION,
                JOptionPane.WARNING_MESSAGE);
        if (reply != JOptionPane.YES_OPTION) {
          return;
        }
      }
      BibtexEntryType.removeType(name);
      updateTypesForEntries(StringUtil.nCase(name));
      changed.remove(name);
      reqLists.remove(name);
      optLists.remove(name);
      if (biblatexMode) {
        opt2Lists.remove(name);
      }
    }
    // messageLabel.setText("'"+type.getName()+"' "+
    //        Globals.lang("is a standard type."));

  }
Ejemplo n.º 2
0
  private void applyChanges() {
    valueChanged(new ListSelectionEvent(new JList(), 0, 0, false));
    // Iterate over our map of required fields, and list those types if necessary:

    List<String> types = typeComp.getFields();
    for (Map.Entry<String, List<String>> stringListEntry : reqLists.entrySet()) {
      if (!types.contains(stringListEntry.getKey())) {
        continue;
      }

      List<String> reqFields = stringListEntry.getValue();
      List<String> optFields = optLists.get(stringListEntry.getKey());
      List<String> opt2Fields = opt2Lists.get(stringListEntry.getKey());
      String[] reqStr = new String[reqFields.size()];
      reqStr = reqFields.toArray(reqStr);
      String[] optStr = new String[optFields.size()];
      optStr = optFields.toArray(optStr);
      String[] opt2Str;
      if (opt2Fields != null) {
        opt2Str = opt2Fields.toArray(new String[opt2Fields.size()]);
      } else {
        opt2Str = new String[0];
      }

      // If this type is already existing, check if any changes have
      // been made
      boolean changesMade = true;

      if (defaulted.contains(stringListEntry.getKey())) {
        // This type should be reverted to its default setup.
        // System.out.println("Defaulting: "+typeName);
        String nm = StringUtil.nCase(stringListEntry.getKey());
        BibtexEntryType.removeType(nm);

        updateTypesForEntries(nm);
        continue;
      }

      BibtexEntryType oldType = BibtexEntryType.getType(stringListEntry.getKey());
      if (oldType != null) {
        String[] oldReq = oldType.getRequiredFields(), oldOpt = oldType.getOptionalFields();
        if (biblatexMode) {
          String[] priOpt = oldType.getPrimaryOptionalFields();
          String[] secOpt = Util.getRemainder(oldOpt, priOpt);
          if (equalArrays(oldReq, reqStr)
              && equalArrays(oldOpt, optStr)
              && equalArrays(secOpt, opt2Str)) {
            changesMade = false;
          }
        } else if (equalArrays(oldReq, reqStr) && equalArrays(oldOpt, optStr)) {
          changesMade = false;
        }
      }

      if (changesMade) {
        // System.out.println("Updating: "+typeName);
        CustomEntryType typ =
            biblatexMode
                ? new CustomEntryType(
                    StringUtil.nCase(stringListEntry.getKey()), reqStr, optStr, opt2Str)
                : new CustomEntryType(StringUtil.nCase(stringListEntry.getKey()), reqStr, optStr);

        BibtexEntryType.ALL_TYPES.put(stringListEntry.getKey().toLowerCase(), typ);
        updateTypesForEntries(typ.getName());
      }
    }

    Set<Object> toRemove = new HashSet<Object>();
    for (String o : BibtexEntryType.ALL_TYPES.keySet()) {
      if (!types.contains(o)) {
        toRemove.add(o);
      }
    }

    // Remove those that should be removed:
    if (!toRemove.isEmpty()) {
      for (Object aToRemove : toRemove) {
        typeDeletion((String) aToRemove);
      }
    }

    updateTables();
  }
Ejemplo n.º 3
0
  @Override
  public String format(String field) {
    int i;
    field =
        field
            .replaceAll("&|\\\\&", "&amp;")
            .replaceAll("[\\n]{2,}", "<p>")
            .replaceAll("\\n", "<br>");

    StringBuilder sb = new StringBuilder();
    StringBuffer currentCommand = null;

    char c;
    boolean escaped = false, incommand = false;

    for (i = 0; i < field.length(); i++) {
      c = field.charAt(i);
      if (escaped && (c == '\\')) {
        sb.append('\\');
        escaped = false;
      } else if (c == '\\') {
        if (incommand) {
          /* Close Command */
          String command = currentCommand.toString();
          Object result = Globals.HTMLCHARS.get(command);
          if (result != null) {
            sb.append((String) result);
          } else {
            sb.append(command);
          }
        }
        escaped = true;
        incommand = true;
        currentCommand = new StringBuffer();
      } else if (!incommand && ((c == '{') || (c == '}'))) {
        // Swallow the brace.
      } else if (Character.isLetter(c)
          || (c == '%')
          || (Globals.SPECIAL_COMMAND_CHARS.contains(String.valueOf(c)))) {
        escaped = false;

        if (!incommand) {
          sb.append(c);
        } else {
          currentCommand.append(c);
          testCharCom:
          if ((currentCommand.length() == 1)
              && (Globals.SPECIAL_COMMAND_CHARS.contains(currentCommand.toString()))) {
            // This indicates that we are in a command of the type
            // \^o or \~{n}
            if (i >= (field.length() - 1)) {
              break testCharCom;
            }

            String command = currentCommand.toString();
            i++;
            c = field.charAt(i);
            // System.out.println("next: "+(char)c);
            String combody;
            if (c == '{') {
              String part = StringUtil.getPart(field, i, false);
              i += part.length();
              combody = part;
            } else {
              combody = field.substring(i, i + 1);
              // System.out.println("... "+combody);
            }
            Object result = Globals.HTMLCHARS.get(command + combody);

            if (result != null) {
              sb.append((String) result);
            }

            incommand = false;
            escaped = false;
          } else {
            //	Are we already at the end of the string?
            if ((i + 1) == field.length()) {
              String command = currentCommand.toString();
              Object result = Globals.HTMLCHARS.get(command);
              /* If found, then use translated version. If not,
               * then keep
               * the text of the parameter intact.
               */
              if (result != null) {
                sb.append((String) result);
              } else {
                sb.append(command);
              }
            }
          }
        }
      } else {
        String argument;

        if (!incommand) {
          sb.append(c);
        } else if (Character.isWhitespace(c) || (c == '{') || (c == '}')) {
          // First test if we are already at the end of the string.
          // if (i >= field.length()-1)
          // break testContent;

          String command = currentCommand.toString();

          // Then test if we are dealing with a italics or bold
          // command.
          // If so, handle.
          if (command.equals("em") || command.equals("emph") || command.equals("textit")) {
            String part = StringUtil.getPart(field, i, true);

            i += part.length();
            sb.append("<em>").append(part).append("</em>");
          } else if (command.equals("textbf")) {
            String part = StringUtil.getPart(field, i, true);
            i += part.length();
            sb.append("<b>").append(part).append("</b>");
          } else if (c == '{') {
            String part = StringUtil.getPart(field, i, true);
            i += part.length();
            argument = part;
            if (argument != null) {
              // handle common case of general latex command
              Object result = Globals.HTMLCHARS.get(command + argument);
              // System.out.print("command: "+command+", arg: "+argument);
              // System.out.print(", result: ");
              // If found, then use translated version. If not, then keep
              // the
              // text of the parameter intact.
              if (result != null) {
                sb.append((String) result);
              } else {
                sb.append(argument);
              }
            }
          } else if (c == '}') {
            // This end brace terminates a command. This can be the case in
            // constructs like {\aa}. The correct behaviour should be to
            // substitute the evaluated command and swallow the brace:
            Object result = Globals.HTMLCHARS.get(command);
            if (result != null) {
              sb.append((String) result);
            } else {
              // If the command is unknown, just print it:
              sb.append(command);
            }
          } else {
            Object result = Globals.HTMLCHARS.get(command);
            if (result != null) {
              sb.append((String) result);
            } else {
              sb.append(command);
            }
            sb.append(' ');
          }
        } /* else if (c == '}') {
             System.out.printf("com term by }: '%s'\n", currentCommand.toString());

             argument = "";
          }*/ else {
          /*
           * TODO: this point is reached, apparently, if a command is
           * terminated in a strange way, such as with "$\omega$".
           * Also, the command "\&" causes us to get here. The former
           * issue is maybe a little difficult to address, since it
           * involves the LaTeX math mode. We don't have a complete
           * LaTeX parser, so maybe it's better to ignore these
           * commands?
           */
        }

        incommand = false;
        escaped = false;
      }
    }

    return sb.toString();
  }