Ejemplo n.º 1
0
  /**
   * Reads an XML-representation of a list of some general type.
   *
   * @param tag The tag for the list <code>Element</code>.
   * @param type The type of the items to be added. This type needs to have a constructor accepting
   *     a single <code>String</code>.
   * @return The list.
   * @exception XMLStreamException if a problem was encountered during parsing.
   */
  public <T> List<T> readList(String tag, Class<T> type) throws XMLStreamException {

    expectTag(tag);

    final int length = getAttribute(FreeColObject.ARRAY_SIZE_TAG, -1);
    if (length < 0) return Collections.<T>emptyList();

    List<T> list = new ArrayList<>(length);
    for (int x = 0; x < length; x++) {
      try {
        final String value = getAttribute("x" + x, (String) null);
        T object = null;
        if (value != null) {
          Constructor<T> c = type.getConstructor(type);
          object = c.newInstance(new Object[] {value});
        }
        list.add(object);
      } catch (IllegalAccessException
          | InstantiationException
          | InvocationTargetException
          | NoSuchMethodException e) {
        throw new RuntimeException(e);
      }
    }

    closeTag(tag);
    return list;
  }
Ejemplo n.º 2
0
 /**
  * Close the current tag, checking that it did indeed close correctly.
  *
  * @param tag The expected tag name.
  * @exception XMLStreamException if a closing tag is not found.
  */
 public void closeTag(String tag) throws XMLStreamException {
   if (nextTag() != XMLStreamConstants.END_ELEMENT) {
     throw new XMLStreamException(
         "Parse error, END_ELEMENT expected," + " not: " + getLocalName());
   }
   expectTag(tag);
 }
Ejemplo n.º 3
0
  /**
   * Reads an XML-representation of a list of <code>FreeColGameObjectType</code>s.
   *
   * @param tag The tag for the list <code>Element</code>.
   * @param spec The <code>Specification</code> to find items in.
   * @param type The type of the items to be added. The type must exist in the supplied
   *     specification.
   * @return The list.
   * @exception XMLStreamException if a problem was encountered during parsing.
   */
  public <T extends FreeColGameObjectType> List<T> readList(
      Specification spec, String tag, Class<T> type) throws XMLStreamException {

    expectTag(tag);

    final int length = getAttribute(FreeColObject.ARRAY_SIZE_TAG, -1);
    if (length < 0) return Collections.<T>emptyList();

    List<T> list = new ArrayList<>(length);
    for (int x = 0; x < length; x++) {
      T value = getType(spec, "x" + x, type, (T) null);
      if (value == null) logger.warning("Null list value(" + x + ")");
      list.add(value);
    }

    closeTag(tag);
    return list;
  }