configdescriptionmodel.java

来自「swing编写的库存管理程序。毕业设计类」· Java 代码 · 共 484 行 · 第 1/2 页

JAVA
484
字号
  /**
   * Returns the value at the specified index.
   * @param index the requested index
   * @return the value at <code>index</code>
   */
  public Object getElementAt(final int index)
  {
    final ConfigDescriptionEntry entry = get (index);
    if (entry == null)
    {
      return null;
    }
    return entry.getKeyName();
  }

  /**
   * Imports all entries from the given report configuration. Only new entries
   * will be added to the list. This does not add report properties supplied via
   * the System.properties.
   * 
   * @param config the report configuration from where to add the entries.
   */
  public void importFromConfig (final ReportConfiguration config)
  {
    final Iterator it = config.findPropertyKeys("");
    while (it.hasNext())
    {
      final String keyname = (String) it.next();
      if (System.getProperties().containsKey(keyname))
      {
        continue;
      }
      final TextConfigDescriptionEntry entry = new TextConfigDescriptionEntry(keyname);
      if (contains(entry) == false)
      {
        add(entry);
      }
    }
  }

  /**
   * Loads the entries from the given xml file. The file must be in the 
   * format of the config-description.xml file.
   * 
   * @param in the inputstream from where to read the file
   * @throws IOException if an error occured while reading the file
   * @throws SAXException if an XML parse error occurs.
   * @throws ParserConfigurationException if the XML parser could not be 
   * initialized.
   */
  public void load (final InputStream in)
      throws IOException, SAXException, ParserConfigurationException
  {
    content.clear();
    final Document doc = DOMUtilities.parseInputStream(in);
    final Element e = doc.getDocumentElement();
    final NodeList list = e.getElementsByTagName("key");
    for (int i = 0; i < list.getLength(); i++)
    {
      final Element keyElement = (Element) list.item(i);
      final String keyName = keyElement.getAttribute("name");
      final boolean keyGlobal = StringUtil.parseBoolean (keyElement.getAttribute("global"), false);
      final boolean keyHidden = StringUtil.parseBoolean (keyElement.getAttribute("hidden"), false);
      final String descr = getDescription(keyElement);

      final NodeList enumNodes = keyElement.getElementsByTagName("enum");
      if (enumNodes.getLength() != 0)
      {
        final String[] alteratives = collectEnumEntries((Element) enumNodes.item(0));
        final EnumConfigDescriptionEntry en = new EnumConfigDescriptionEntry(keyName);
        en.setDescription(descr);
        en.setGlobal(keyGlobal);
        en.setHidden(keyHidden);
        en.setOptions(alteratives);
        add(en);
        continue;
      }

      final NodeList classNodes = keyElement.getElementsByTagName("class");
      if (classNodes.getLength() != 0)
      {
        final Element classElement = (Element) classNodes.item(0);
        final String className = classElement.getAttribute("instanceof");
        if (className == null)
        {
          throw new SAXException("class element: instanceof attribute missing.");
        }
        final Class baseClass;
        try
        {
          baseClass = this.getClass().getClassLoader().loadClass(className);
        }
        catch (Exception ex)
        {
          throw new SAXException("Failed to load base class", ex);
        }
        final ClassConfigDescriptionEntry ce = new ClassConfigDescriptionEntry(keyName);
        ce.setBaseClass(baseClass);
        ce.setDescription(descr);
        ce.setGlobal(keyGlobal);
        ce.setHidden(keyHidden);
        add(ce);
        continue;
      }

      final NodeList textNodes = keyElement.getElementsByTagName("text");
      if (textNodes.getLength() != 0)
      {
        final TextConfigDescriptionEntry textEntry = new TextConfigDescriptionEntry(keyName);
        textEntry.setDescription(descr);
        textEntry.setGlobal(keyGlobal);
        textEntry.setHidden(keyHidden);
        add(textEntry);
        continue;
      }
    }
  }

  /**
   * A parser helper method which collects all enumeration entries from
   * the given element.
   * 
   * @param element the element from where to read the enumeration entries.
   * @return the entries as string array.
   */
  private String[] collectEnumEntries(final Element element)
  {
    final NodeList nl = element.getElementsByTagName("text");
    final String[] retval = new String[nl.getLength()];
    for (int i = 0; i < nl.getLength(); i++)
    {
      retval[i] = DOMUtilities.getText((Element) nl.item(i));
    }
    return retval;
  }

  /**
   * A parser helper method that returns the CDATA description of the 
   * given element.
   *  
   * @param e the element from where to read the description.
   * @return the description text.
   */
  private String getDescription (final Element e)
  {
    final NodeList descr = e.getElementsByTagName("description");
    if (descr.getLength() == 0)
    {
      return "";
    }
    return DOMUtilities.getText((Element) descr.item(0));
  }
  
  /**
   * Saves the model into an xml file.
   * 
   * @param out the target output stream.
   * @param encoding the encoding of the content.
   * @throws IOException if an error occurs.
   */
  public void save (final OutputStream out, final String encoding) throws IOException
  {
    final PrintWriter writer = new PrintWriter(new OutputStreamWriter (out, encoding));
    writer.println("<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>");
    writer.println("<!DOCTYPE config-description [");
    writer.println("<!ELEMENT config-description  (key*)>");
    writer.println("<!ELEMENT key                 (description?, (class | enum | text))>");
    writer.println("<!ATTLIST key");
    writer.println("   name   CDATA #REQUIRED");
    writer.println("   global CDATA #IMPLIED");
    writer.println("   hidden CDATA #IMPLIED");
    writer.println(" >");
    writer.println();
    writer.println("<!ELEMENT description         (#PCDATA)>");
    writer.println("<!ELEMENT class               EMPTY>");
    writer.println("<!ATTLIST class");
    writer.println("  instanceof CDATA #REQUIRED");
    writer.println(" >");
    writer.println();
    writer.println("<!ELEMENT enum               (text)*>");
    writer.println("<!ELEMENT text               (#PCDATA)>");
    writer.println(" ]>");
    final DOMWriter dwriter = DOMWriter.getInstance();
    dwriter.writeTag(writer, "config-description");

    final CharacterEntityParser parser = CharacterEntityParser.createXMLEntityParser();
    for (int i = 0; i < getSize(); i++)
    {
      final ConfigDescriptionEntry entry = get(i);
      final AttributeList p = new AttributeList();
      p.setAttribute("name", entry.getKeyName());
      p.setAttribute("global", String.valueOf(entry.isGlobal()));
      p.setAttribute("hidden", String.valueOf(entry.isHidden()));
      dwriter.writeTag(writer, "key", p, false);
      if (entry.getDescription() != null)
      {
        dwriter.writeTag(writer, "description");
        writer.write(parser.encodeEntities(entry.getDescription()));
        dwriter.writeCloseTag(writer, "description");
      }
      if (entry instanceof ClassConfigDescriptionEntry)
      {
        final ClassConfigDescriptionEntry ce = (ClassConfigDescriptionEntry) entry;
        if (ce.getBaseClass() != null)
        {
          dwriter.writeTag
            (writer, "class", "instanceof", ce.getBaseClass().getName(), DOMWriter.CLOSE);
        }
        else
        {
          dwriter.writeTag
            (writer, "class", "instanceof", "java.lang.Object", DOMWriter.CLOSE);
        }
      }
      else if (entry instanceof TextConfigDescriptionEntry)
      {
        dwriter.writeTag(writer, "text", new AttributeList(), DOMWriter.CLOSE);
      }
      else if (entry instanceof EnumConfigDescriptionEntry)
      {
        final EnumConfigDescriptionEntry en = (EnumConfigDescriptionEntry) entry;
        dwriter.writeTag(writer, "enum");

        final String[] alts = en.getOptions();
        if (alts != null)
        {
          for (int optCount = 0; optCount < alts.length; optCount++)
          {
            dwriter.writeTag (writer, "text");
            writer.write(parser.encodeEntities(alts[optCount]));
            dwriter.writeCloseTag(writer, "text");
          }
        }
        dwriter.writeCloseTag(writer, "enum");
      }
      dwriter.writeCloseTag(writer, "key");
    }
    dwriter.writeCloseTag(writer, "config-description");
    writer.flush();
  }
}

⌨️ 快捷键说明

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