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

📄 syntaxparser.java

📁 模仿WINDOWS的WINZIP的一款软件
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
  /**
   *
   * @param attr
   */
  private void resetFontAttrs(HashMap attr) {
    attr.put(TextAttribute.FAMILY, this.def_Familyname);
    attr.put(TextAttribute.SIZE, this.def_Size);
    attr.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
    attr.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_REGULAR);
    attr.put(TextAttribute.FOREGROUND, this.def_Foreground);

  }

  /**
   * 返回类树
   * @return
   */
  public TreeSet getClasses() {
    TreeSet classes = new TreeSet();
    if (element != null) {
      Element e = this.getChildElement(element, this.CLASSES);
      String[] ss = this.convetToArray(this.getAttrValue(e, this.VALUE));
      for (int i = 0; i < ss.length; i++) {
        classes.add(ss[i]);
      }
      Element[] es = this.getChildElements(e, this.CLASS);
      for (int j = 0; j < es.length; j++) {
        classes.add(getElementValue(es[j]));

      }

    }
    return classes;
  }

  /**
   * 返回关键词树
   * @return
   */
  public TreeSet getKeywords() {
    TreeSet keywords = new TreeSet();
    if (element != null) {
      Element e = this.getChildElement(element, this.KEYWORDS);
      String[] ss = this.convetToArray(this.getAttrValue(e, this.VALUE));
      for (int i = 0; i < ss.length; i++) {
        keywords.add(ss[i]);
      }
      Element[] es = this.getChildElements(e, this.KEYWORD);

      for (int j = 0; j < es.length; j++) {
        keywords.add(getElementValue(es[j]));
      }

    }

    return keywords;
  }

  /**
   * 得到相应的ELement-style
   * @return
   */
  private Element getStyle() {
    NodeList list = doc.getElementsByTagName(this.STYLE);
    for (int i = 0; i < list.getLength(); i++) {
      element = (Element) list.item(i);
      if (SyntaxParser.getAttrValue(element,
          this.CONTENTTYPE).equalsIgnoreCase(contentType)) {
        System.out.println("现在解析类型是:" + contentType);
        return element;
      }
    }
    return null;

  }

  /**
   * open a XML File and parse it
   * @param is Input stream
   * @throws SyntaxException
   */
  public void openFile(File f) throws SyntaxException {
    try {
      this.syntaxFile = f;
      DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
      DocumentBuilder builder = factory.newDocumentBuilder();
      doc = builder.parse(f);
      doc.normalize();
    }
    catch (ParserConfigurationException e) {
      throw new SyntaxException(e.toString());
    }
    catch (SAXException e) {
      throw new SyntaxException(e.toString());
    }
    catch (IOException e) {
      throw new SyntaxException(e.toString());
    }

  }

  ///////////////////////////////////////////////////////////////////////
  /**
   * 返回一个子element
   * @param parent
   * @param tagName
   * @return
   */
  public static Element getChildElement(Element parent, String tagName) {
    // first see if parent is null. the caller can call this method
    // safely if parent is null. it is the same to the following
    // methods getChild...()
    if (parent != null) {
      NodeList nodes = parent.getElementsByTagName(tagName);
      Element e = (Element) nodes.item(0);
      return (Element) nodes.item(0); // if length == 0, item(0) will return null
    }
    return null;
  }

  /**
   * 返回所有子elements
   * @param parent
   * @param tagName
   * @return
   */
  public static Element[] getChildElements(Element parent, String tagName) {
    if (parent != null) {
      NodeList nodes = parent.getElementsByTagName(tagName);
      int size = nodes.getLength();
      Element[] children = new Element[size];
      for (int i = 0; i < children.length; i++) {
        children[i] = (Element) nodes.item(i);
      }
      return children;
    }
    return new Element[0];
  }

  /**
   * 得到Element自己的值
   * @param e
   * @return
   */
  public static String getElementValue(Element e) {
    if (e != null) {
      return e.getFirstChild().getNodeValue();
    }
    return "";
  }

  /**
   * 返回一个element的值<a>fasddfds</a>
   * @param parent
   * @param tagName
   * @return
   */
  public static String getChildValue(Element parent, String tagName) {
    if (parent != null) {
      NodeList nodes = parent.getElementsByTagName(tagName);
      if (nodes.getLength() > 0) {
        Node first = nodes.item(0).getFirstChild();
        if (first != null) {
          return first.getNodeValue();
        }
      }

    }
    return "";
  }

  /**
   * 返回一个属性值
   * @param e
   * @param attrName
   * @param value
   * @return
   */
  public static String getAttrValue(Element e, String attrName) {
    if (e != null) {
      NamedNodeMap nnm = e.getAttributes();
      if (nnm != null) {
        Node attr = nnm.getNamedItem(attrName);
        if (attr != null) {
          return attr.getNodeValue();
        }
      }
    }
    return null;

  }

  /////////////////////////////////////////////////////////////////////////
  /**
   * 解析颜色,一般的颜色可以用英文表示,如red等
   * 其他可以#ffffff的形式表示
   * @param nm
   * @param def
   * @return
   */
  public static Color getColor(String nm, Color def) {
    Color value = null;
    try {
      if (nm == null) {
        return def;
      }
      if (nm.startsWith("#")) {
        value = Color.decode(nm);
        if (value != null) {
          return value;
        }
      }

      if (nm.startsWith("rgb:")) {
        String str = nm.substring(4);
        String[] ss = convetToArray(str);
        int r = (new Integer(ss[0])).intValue();
        int b = (new Integer(ss[2])).intValue();
        int g = (new Integer(ss[1])).intValue();
        value = new Color(r, g, b);
        if (value != null) {
          return value;
        }
      }
      value = (Color) clrMap.get(nm.toLowerCase());
      if (value != null) {
        return value;
      }
    }
    catch (Exception ex) {
      ex.printStackTrace();
    }
    finally {
      if (value == null) {
        return def;
      }
      else {
        return value;
      }

    }
  }

  ////////////////////////////////////////////////////////////////////////////////////////////////////
  public static String[] convetToArray(String str) {
    PropertySerials p = new PropertySerials(str, ',');
    return p.getSerials();
  }

  /**
   * just for test
   * @param args
   */
  public static void main(String[] args) {
    Color clr = SyntaxParser.getColor("rgb:0,0,255", Color.black);
    System.out.println(clr);
    String[] ss = SyntaxParser.convetToArray("abstract,break,volatile,while");
    for (int i = 0; i < ss.length; i++) {
      System.out.println(ss[i]);
    }

    SyntaxParser p = new SyntaxParser(new File("java.xml"), "text/java");

  }

}

⌨️ 快捷键说明

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