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

📄 resourcestatement.java

📁 外国人写的c#语法解析器
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/**
 * Soft Gems Resource parser. Created by Mike Lischke.
 * 
 * The sourceNode code in this file can freely be used for any purpose provided this notice remains 
 * unchanged in the file.
 * 
 * Copyright 2004 by Mike Lischke, www.soft-gems.net, public@soft-gems.net. All rights reserved.
 */

package net.softgems.resourceparser.xml;

import java.util.HashMap;

import net.softgems.resourceparser.RCMainFrame;
import net.softgems.resourceparser.main.RCParserTokenTypes;

import org.jdom.Element;

import antlr.collections.AST;

/**
 * This class converts a resource statement AST to XML.
 */
public class ResourceStatement extends AST2XMLConverter
{

  /** This map allows for a quick lookup of resource types. */
  private HashMap resourceMap = new HashMap();

  //------------------------------------------------------------------------------------------------
  
  /**
   * The constructor of the class.
   * 
   * @param astNode The root node of the subtree we will working on.
   */
  public ResourceStatement(RCMainFrame owner, AST astNode)
  {
    super(owner, astNode);

    resourceMap.put("accelerators", new Integer(RCParserTokenTypes.LITERAL_accelerators));
    resourceMap.put("bitmap", new Integer(RCParserTokenTypes.LITERAL_bitmap));
    resourceMap.put("cursor", new Integer(RCParserTokenTypes.LITERAL_cursor));
    resourceMap.put("dialog", new Integer(RCParserTokenTypes.LITERAL_dialog));
    resourceMap.put("dialogex", new Integer(RCParserTokenTypes.LITERAL_dialogex));
    resourceMap.put("font", new Integer(RCParserTokenTypes.LITERAL_font));
    resourceMap.put("icon", new Integer(RCParserTokenTypes.LITERAL_icon));
    resourceMap.put("menu", new Integer(RCParserTokenTypes.LITERAL_menu));
    resourceMap.put("menuex", new Integer(RCParserTokenTypes.LITERAL_menuex));
    resourceMap.put("messagetable", new Integer(RCParserTokenTypes.LITERAL_messagetable));
    resourceMap.put("rcdata", new Integer(RCParserTokenTypes.LITERAL_rcdata));
    resourceMap.put("versioninfo", new Integer(RCParserTokenTypes.LITERAL_versioninfo));
    resourceMap.put("textinclude", new Integer(RCParserTokenTypes.LITERAL_textinclude));
    resourceMap.put("designinfo", new Integer(RCParserTokenTypes.LITERAL_designinfo));
    resourceMap.put("toolbar", new Integer(RCParserTokenTypes.LITERAL_toolbar));
    resourceMap.put("dlginit", new Integer(RCParserTokenTypes.LITERAL_dlginit));
    resourceMap.put("user defined", new Integer(RCParserTokenTypes.USER_DEFINED));
  }

  //------------------------------------------------------------------------------------------------
  
  /**
   * Converts a "accelerators" entry to XML.
   * 
   * @param sourceNode The node containing the AST data to convert.
   * @param resourceElement The entry to which the new nodes must be added.
   */
  private void convertAcceleratorsEntry(AST sourceNode, Element resourceElement)
  {
    resourceElement.setAttribute("type", "accelerators");

    AST currentNode = sourceNode.getFirstChild();
    if (currentNode != null)
    {
      if (currentNode.getType() == RCParserTokenTypes.RESOURCE_ATTRIBUTES)
      {
        convertResourceAttributes(resourceElement, currentNode);
        currentNode = currentNode.getNextSibling();
      }
    }    

    // Optional common resource info part.
    if (currentNode != null)
    {
      while (currentNode.getType() == RCParserTokenTypes.COMMON_RESOURCE_INFO)
      {
        convertCommonResourceInfo(currentNode, resourceElement);
        currentNode = currentNode.getNextSibling();
      }
    }

    while (currentNode != null)
    {
      AST acceleratorNode = currentNode.getFirstChild();
      
      Element acceleratorElement = new Element("entry");
      resourceElement.addContent(acceleratorElement);
      processEntryWithEvaluationAsAttribute(acceleratorNode, "key", acceleratorElement);
      acceleratorNode = acceleratorNode.getNextSibling();
      processEntryWithEvaluationAsAttribute(acceleratorNode, "id", acceleratorElement);
      acceleratorNode = acceleratorNode.getNextSibling();
      
      if ((acceleratorNode != null) && (acceleratorNode.getType() == RCParserTokenTypes.ACCELERATOR_TYPE))
      {
        acceleratorElement.setAttribute("type", acceleratorNode.getFirstChild().getText());
        acceleratorNode = acceleratorNode.getNextSibling();
      }

      while (acceleratorNode != null)
      {
        Element extraElement = new Element("option");
        acceleratorElement.addContent(extraElement);
        extraElement.setAttribute("value", acceleratorNode.getFirstChild().getText());
        
        acceleratorNode = acceleratorNode.getNextSibling();
      }
      currentNode = currentNode.getNextSibling();
    }
  }

  //------------------------------------------------------------------------------------------------
  
  /**
   * Converts a "bitamp" entry to XML.
   * 
   * @param sourceNode The node containing the AST data to convert.
   * @param resourceElement The entry to which the new nodes must be added.
   */
  private void convertBitmapEntry(AST sourceNode, Element resourceElement)
  {
    resourceElement.setAttribute("type", "bitmap");

    AST currentNode = sourceNode.getFirstChild();
    if (currentNode != null)
    {
      if (currentNode.getType() == RCParserTokenTypes.RESOURCE_ATTRIBUTES)
      {
        convertResourceAttributes(resourceElement, currentNode);
        currentNode = currentNode.getNextSibling();
      }
    }
    
    if (currentNode.getType() == RCParserTokenTypes.FILE_NAME)
    {
      String filename = currentNode.getFirstChild().getText();
      if (filename.startsWith("\""))
        filename = filename.substring(1, filename.length() - 1);
      resourceElement.setAttribute("file-name", filename);
    }
    else
    {
      // Must be raw data, e.g. single literals (string, character, integer etc.) or
      // a collection of hex numbers enclosed by single quotes.
      convertRawDataToXML(currentNode, resourceElement);
    }
  }

  //------------------------------------------------------------------------------------------------
  
  /**
   * Converts a set of common dialog resource info (e.g. characteristics, ex-styles etc.) to XML.
   * 
   * @param node The first node in the list to convert.
   * @param element The target element to add the converted stuff to.
   * @return The first node after the common info.
   */
  private AST convertCommonDialogInfo(AST node, Element element)
  {
    AST currentNode = node;
    boolean doBreak = false;
    while (currentNode != null && !doBreak)
    {
      switch (currentNode.getType())
      {
        case RCParserTokenTypes.LITERAL_characteristics:
        {
          processEntryWithEvaluation(currentNode.getFirstChild(), "characteristics", element);
          break;
        }
        case RCParserTokenTypes.LITERAL_language:
        {
          convertLanguageEntry(currentNode, element);
          break;
        }
        case RCParserTokenTypes.LITERAL_version:
        {
          processEntryWithEvaluation(currentNode.getFirstChild(), "version", element);
          break;
        }
        case RCParserTokenTypes.LITERAL_caption:
        {
          Element subElement = new Element("caption");
          element.addContent(subElement);
          subElement.setAttribute("value", convertToString(currentNode.getFirstChild()));
          break;
        }
        case RCParserTokenTypes.LITERAL_class:
        {
          processEntryWithEvaluation(currentNode.getFirstChild(), "class", element);
          break;
        }
        case RCParserTokenTypes.LITERAL_exstyle:
        {
          processEntryWithEvaluation(currentNode.getFirstChild(), "exstyle", element);
          break;
        }
        case RCParserTokenTypes.LITERAL_menu:
        {
          processEntryWithEvaluation(currentNode.getFirstChild(), "menu", element);
          break;
        }
        case RCParserTokenTypes.LITERAL_font:
        {
          Element fontElement = new Element("font");
          element.addContent(fontElement);

          AST fontDataNode = currentNode.getFirstChild();
          processEntryWithEvaluation(fontDataNode, "point-size", fontElement);
          
          fontDataNode = fontDataNode.getNextSibling();
          processEntryWithEvaluation(fontDataNode, "type-face", fontElement);
          
          fontDataNode = fontDataNode.getNextSibling();
          if (fontDataNode != null)
          {
            processEntryWithEvaluation(fontDataNode, "weight", fontElement);

            fontDataNode = fontDataNode.getNextSibling();
            processEntryWithEvaluation(fontDataNode, "italic", fontElement);

            fontDataNode = fontDataNode.getNextSibling();
            processEntryWithEvaluation(fontDataNode, "charset", fontElement);
          }
          
          break;
        }
        case RCParserTokenTypes.LITERAL_style:
        {
          processEntryWithEvaluation(currentNode.getFirstChild(), "style", element);
          break;
        }
        default:
        {
          doBreak = true;
        }
      }
      currentNode = currentNode.getNextSibling();
    }
    return currentNode;
  }

  //------------------------------------------------------------------------------------------------
  
  /**
   * Converts a common resource info entry to XML.
   * 
   * @param node The entry to convert.
   * @param element The target XML element where to add the converted stuff.
   */
  private void convertCommonResourceInfo(AST node, Element element)
  {
    AST currentNode = node.getFirstChild();
    if (currentNode != null)
    {
      Element infoElement = new Element("common-resource-info");
      element.addContent(infoElement);
      infoElement.setAttribute("name", currentNode.getText());
      if (currentNode.getType() == RCParserTokenTypes.LITERAL_language)
        convertLanguageEntry(currentNode, infoElement);
      else
        convertExpressionList(currentNode.getFirstChild(), infoElement);
    }
  }

  //------------------------------------------------------------------------------------------------
  
  /**
   * Converts the given node, which must be a control node in a dialog, to XML
   * 
   * @param node The node to convert.
   * @param element The target element where to add the result to.
   */
  private void convertConcreteControl(AST node, Element element)
  {
    Element controlElement = new Element("control");
    element.addContent(controlElement);
    controlElement.setAttribute("type", node.getText());
    switch (node.getType())
    {
      case RCParserTokenTypes.LITERAL_ltext:
      case RCParserTokenTypes.LITERAL_rtext:
      case RCParserTokenTypes.LITERAL_ctext:
      case 135: // RCParserTokenTypes.LITERAL_auto3state
      case RCParserTokenTypes.LITERAL_autocheckbox:
      case RCParserTokenTypes.LITERAL_autoradiobutton:
      case RCParserTokenTypes.LITERAL_checkbox:
      case RCParserTokenTypes.LITERAL_pushbox:
      case RCParserTokenTypes.LITERAL_pushbutton:
      case RCParserTokenTypes.LITERAL_defpushbutton:
      case RCParserTokenTypes.LITERAL_radiobutton:
      case 143: // RCParserTokenTypes.LITERAL_state3
      case RCParserTokenTypes.LITERAL_groupbox:
      case RCParserTokenTypes.LITERAL_userbutton:
      case RCParserTokenTypes.LITERAL_icon:
      case RCParserTokenTypes.LITERAL_scrollbar:
      {
        AST child = node.getFirstChild();
        processEntryWithEvaluation(child, "control-text", controlElement);
        child = child.getNextSibling();
        processEntryWithEvaluation(child, "id", controlElement);
        
        break;
      }
      case RCParserTokenTypes.LITERAL_edittext:
      case RCParserTokenTypes.LITERAL_bedit:
      case RCParserTokenTypes.LITERAL_hedit:
      case RCParserTokenTypes.LITERAL_iedit:
      case RCParserTokenTypes.LITERAL_combobox:
      case RCParserTokenTypes.LITERAL_listbox:
      {
        AST child = node.getFirstChild();
        processEntryWithEvaluation(child, "id", controlElement);
        
        break;
      }
    }
    
    // Common control trailing.
    node = node.getNextSibling();
    processEntryWithEvaluation(node, "left", controlElement);
    node = node.getNextSibling();
    processEntryWithEvaluation(node, "top", controlElement);
    node = node.getNextSibling();
    processEntryWithEvaluation(node, "width", controlElement);
    node = node.getNextSibling();
    processEntryWithEvaluation(node, "height", controlElement);
    node = node.getNextSibling();
    if (node != null)
    {
      processEntryWithEvaluation(node, "style", controlElement);
      
      // Everything after this point can only appear in DialogEx resource entries.
      node = node.getNextSibling();
      if (node != null)
      {
        processEntryWithEvaluation(node, "exstyle", controlElement);
        node = node.getNextSibling();
        if (node != null)
        {
          processEntryWithEvaluation(node, "help-id", controlElement);
  
          if (node.getNextSibling() != null)
          {
            Element dataElement = new Element("control-data");

⌨️ 快捷键说明

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