Example #1
0
  /**
   * Returns a new {@code XMLConfig} with no entries.
   *
   * @return a new {@code XMLConfig} with no entries
   */
  static XMLConfig empty() {
    XMLConfig rv = naked();
    Document xmlDoc;
    try {
      xmlDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException ex) {
      throw new ConfigException(ex);
    }

    Element rootElement = xmlDoc.createElement(ROOT_PREFIX);

    Element numberElements = xmlDoc.createElement(Repository.NUMBER.getName());
    rootElement.appendChild(numberElements);

    Element stringElements = xmlDoc.createElement(Repository.STRING.getName());
    rootElement.appendChild(stringElements);

    Element booleanElements = xmlDoc.createElement(Repository.BOOLEAN.getName());
    rootElement.appendChild(booleanElements);

    xmlDoc.appendChild(rootElement);
    xmlDoc.setXmlStandalone(true);

    rv.stringElements = stringElements;
    rv.numberElements = numberElements;
    rv.booleanElements = booleanElements;
    rv.xmlDoc = xmlDoc;

    return rv;
  }
Example #2
0
  /**
   * Returns an {@code XMLConfig} without any XML bindings.
   *
   * @return an {@code XMLConfig} without any XML bindings
   */
  static XMLConfig naked() {
    XMLConfig rv = uninitialized();

    rv.flushedBooleanElements = new HashMap<String, Boolean>();
    rv.pendingBooleanElements = new HashMap<String, Boolean>();
    rv.deletedBooleanKeys = new ArrayList<String>();

    rv.flushedStringElements = new HashMap<String, String>();
    rv.pendingStringElements = new HashMap<String, String>();
    rv.deletedStringKeys = new ArrayList<String>();

    rv.flushedNumberElements = new HashMap<String, Number>();
    rv.pendingNumberElements = new HashMap<String, Number>();
    rv.deletedNumberKeys = new ArrayList<String>();

    rv.booleanRepoEvent = new ConfigEvent<BooleanRepoContext>(rv.eventKey);
    rv.stringRepoEvent = new ConfigEvent<StringRepoContext>(rv.eventKey);
    rv.numberRepoEvent = new ConfigEvent<NumberRepoContext>(rv.eventKey);

    return rv;
  }
Example #3
0
  /**
   * Sets up the document for use.
   *
   * @param rv the {@code XMLConfig} to set up
   * @param xmlDoc
   * @return
   */
  static XMLConfig readAndSetupEntriesImpl(final XMLConfig rv, final Document xmlDoc) {
    try {

      xmlDoc.setXmlStandalone(true);
      rv.xmlDoc = xmlDoc;
      Element root = xmlDoc.getDocumentElement();

      if (root == null) {
        throw new ConfigException("missing root element");
      }

      NodeList nodeList;
      Element repoElement;
      Node repoNode;
      NodeList elementNodes;
      int len;

      for (Repository repository : Repository.values()) {
        nodeList = root.getElementsByTagName(repository.getName());
        len = nodeList.getLength();
        if (len == 0) {
          throw Configs.newMissingRepoException(repository);
        } else if (len > 1) {
          throw Configs.newDuplicateRepoException(repository);
        }
        repoNode = nodeList.item(0);
        if (!(repoNode instanceof Element)) {
          throw newInvalidNodeClassException(Element.class);
        }
        repoElement = (Element) repoNode;

        switch (repository) {
          case NUMBER:
            rv.numberElements = repoElement;
            break;
          case BOOLEAN:
            rv.booleanElements = repoElement;
            break;
          case STRING:
            rv.stringElements = repoElement;
            break;
        }

        elementNodes = repoElement.getElementsByTagName(ENTRY_FLAG);
        len = elementNodes.getLength();
        for (int j = 0; j < len; ++j) {
          Node n = elementNodes.item(j);
          if (n instanceof Element) {
            Element e = (Element) n;
            Attr nameAttr = e.getAttributeNode(KEY_FLAG);
            if (nameAttr == null) {
              throw newMalformedKeyAttrException(repository);
            }
            String key = nameAttr.getValue();
            Attr valueAttr = e.getAttributeNode(VALUE_FLAG);
            if (valueAttr == null) {
              throw newMalformedValueAttrException(key, repository);
            }
            String value = valueAttr.getValue();
            // Number uses a different format
            if (repository == Repository.NUMBER) {
              Attr typeAttr = e.getAttributeNode(TYPE_FLAG);
              if (typeAttr == null) {
                throw newMalformedValueAttrException(key, repository);
              }
              rv.flushedNumberElements.put(
                  key,
                  Configs.parseNumberFromType(
                      value, Configs.numberTypeValueOf(typeAttr.getValue())));
              continue;
            }

            if (repository == Repository.BOOLEAN) {
              rv.flushedBooleanElements.put(key, Configs.parseBoolean(value));
            } else {
              rv.flushedStringElements.put(key, value);
            }
          }
        }
      }

    } catch (Configs.BooleanParsingException ex) {

      throw new ConfigException(ex);

    } catch (NumberFormatException ex) {

      throw new ConfigException(ex);

    } catch (DOMException ex) {

      throw new ConfigException(ex);
    }
    return rv;
  }