⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 icongrouploader.java

📁 具有不同语法高亮的编辑器实例
💻 JAVA
字号:
/*
 * IconGroupLoader.java - Loads icon groups from an XML file.
 */
package org.fife.rtext;

import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;

import javax.xml.parsers.*;
import javax.xml.transform.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import org.w3c.dom.*;
import org.xml.sax.InputSource;

import org.fife.io.UnicodeReader;
import org.fife.ui.rtextarea.IconGroup;

/**
 * A class that knows how to parse an XML file defining icon groups.  RText
 * uses this class to define its available icon groups.
 *
 * @author Robert Futrell
 * @version 0.6
 */
class IconGroupLoader {

  public static final String DEFAULT_ICON_GROUP_NAME = "Office 2003 Icons";

  private RText owner;
  private HashMap iconGroupMap;

  // Constants to use when specifying icon styles.
  private static final String WINDOWS_98_ICONS = "Windows 98 Icons";
  private static final String OFFICE_2003_ICONS = "Office 2003 Icons";

  public static final IconGroup WIN98_ICON_GROUP = new IconGroup(
      WINDOWS_98_ICONS, "org/fife/plaf/OfficeXP/images/");
  public static final IconGroup OFFICE2003_ICON_GROUP = new IconGroup(
      OFFICE_2003_ICONS, "org/fife/plaf/Office2003/images/");

  private static final String ROOT_ELEMENT = "IconGroups";
  private static final String GROUP = "group";
  private static final String NAME = "name";
  private static final String PATH = "path";
  private static final String LARGEICONSUBDIR = "largeIconSubDir";
  private static final String EXTENSION = "extension";
  private static final String JAR = "jar";

      /*****************************************************************************/

  /**
   * Constructor.
   *
   * @param owner The RText instance.
   */
  private IconGroupLoader(RText owner) {
    this.owner = owner;
    iconGroupMap = new HashMap(3);
  }

      /*****************************************************************************/

  /**
   * Reads all icon groups from the specified XML file.
   *
   * @param iconGroupFile The file from which to read.
   * @return A map of icon groups.  The key to each icon group is that
   *         icon group's name.  If no icon groups are available (which is
   *         an error), an empty map is returned.
   */
  private Map doLoad(String iconGroupFile) {

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    Document doc = null;
    try {
      db = dbf.newDocumentBuilder();
      InputSource is = new InputSource(new UnicodeReader(
          new FileInputStream(iconGroupFile), "UTF-8"));
      is.setEncoding("UTF-8");
      doc = db.parse(is); //db.parse(file);
    }
    catch (Exception e) {
      e.printStackTrace();
      owner.displayException(e);
      return iconGroupMap; // Will be empty.
    }

    // Traverse the XML tree.
    initializeFromXMLFile(doc);

    // Add default icon groups.
    iconGroupMap.put(WIN98_ICON_GROUP.getName(), WIN98_ICON_GROUP);
    iconGroupMap.put(OFFICE2003_ICON_GROUP.getName(),
                     OFFICE2003_ICON_GROUP);

    return iconGroupMap;

  }

      /*****************************************************************************/

  /**
   * Parses the XML document containing icon group definitions, adding them
   * to this icon group loader's icon group map.
   *
   * @param node The root node of the parsed XML document.
   * @return <code>true</code> if the initialization went okay;
   *         <code>false</code> if an error occured.
   */
  private boolean initializeFromXMLFile(Node node) {

    if (node == null) {
      return false;
    }

    int type = node.getNodeType();
    switch (type) {

      // Handle document nodes.
      case Node.DOCUMENT_NODE:
        boolean rc = initializeFromXMLFile(
            ( (Document) node).getDocumentElement());
        if (rc == false) {
          return false;
        }
        break;

        // Handle element nodes.
      case Node.ELEMENT_NODE:

        String nodeName = node.getNodeName();

        if (nodeName.equals(ROOT_ELEMENT)) {
          NodeList childNodes = node.getChildNodes();
          int childCount = childNodes == null ? 0 :
              childNodes.getLength();
          for (int i = 0; i < childCount; i++) {
            rc = initializeFromXMLFile(childNodes.item(i));
            // Just because one icon group is specified
            // with invalid XML doesn't mean they all are.
            //if (rc==false)
            //	return false;
          }
        }

        else if (nodeName.equals(GROUP)) {
          NamedNodeMap attributes = node.getAttributes();
          if (attributes == null) {
            return false;
          }
          String name = null;
          String path = ""; // Default to an empty path.
          String largeIconSubDir = null;
          String extension = null;
          String jar = null;
          int count = attributes.getLength();
          for (int i = 0; i < count; i++) {
            Node node2 = attributes.item(i);
            nodeName = node2.getNodeName();
            String nodeValue = node2.getNodeValue();
            if (nodeName.equals(NAME)) {
              name = nodeValue;
            }
            else if (nodeName.equals(PATH)) {
              path = nodeValue;
            }
            else if (nodeName.equals(LARGEICONSUBDIR)) {
              largeIconSubDir = nodeValue;
            }
            else if (nodeName.equals(EXTENSION)) {
              extension = nodeValue;
            }
            else if (nodeName.equals(JAR)) {
              jar = owner.getInstallLocation() +
                  "/" + nodeValue;
            }
          }
          if (name == null) {
            return false;
          }
          IconGroup group = new IconGroup(name, path,
                                          largeIconSubDir, extension, jar);
          iconGroupMap.put(group.getName(), group);
        }

        break;

        // Whitespace nodes.
      case Node.TEXT_NODE:
        break;

        // An error occured?
      default:
        System.err.println("... ERROR!!!");
        return false;

    }

    // Everything went okay.
    return true;

  }

      /*****************************************************************************/

  /**
   * Reads all icon groups from the specified XML file and returns a map
   * of them.
   *
   * @param RText The RText instance.
   * @param iconGroupFile The file from which to read.
   * @return A map of icon groups.  The key to each icon group is that
   *         icon group's name.
   */
  public static Map loadIconGroups(RText rtext, String iconGroupFile) {
    IconGroupLoader loader = new IconGroupLoader(rtext);
    return loader.doLoad(iconGroupFile);
  }

      /*****************************************************************************/

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -