/** {@inheritDoc} */ @Override protected void readChild(FreeColXMLReader xr) throws XMLStreamException { final Game game = getGame(); final String tag = xr.getLocalName(); if (ALARM_TAG.equals(tag)) { Player player = xr.findFreeColGameObject(game, PLAYER_TAG, Player.class, (Player) null, true); // @compat 0.10.5 if (getName() != null) { // Alarm used to imply contact, but only set contacted if // we also have a valid name for the settlement. setContacted(player); } // end @compat alarm.put(player, new Tension(xr.getAttribute(VALUE_TAG, 0))); xr.closeTag(ALARM_TAG); } else if (CONTACT_LEVEL_TAG.equals(tag)) { ContactLevel cl = xr.getAttribute(LEVEL_TAG, ContactLevel.class, ContactLevel.UNCONTACTED); Player player = xr.findFreeColGameObject(game, PLAYER_TAG, Player.class, (Player) null, true); contactLevels.put(player, cl); xr.closeTag(CONTACT_LEVEL_TAG); // @compat 0.10.5 } else if (IS_VISITED_TAG.equals(tag)) { Player player = xr.findFreeColGameObject(game, PLAYER_TAG, Player.class, (Player) null, true); setScouted(player); xr.closeTag(IS_VISITED_TAG); // end @compat } else if (MISSIONARY_TAG.equals(tag)) { xr.nextTag(); missionary = xr.readFreeColGameObject(game, Unit.class); missionary.setLocationNoUpdate(this); xr.closeTag(MISSIONARY_TAG); // @compat 0.10.1 } else if (OLD_UNITS_TAG.equals(tag)) { while (xr.nextTag() != XMLStreamConstants.END_ELEMENT) { super.readChild(xr); } // end @compat } else if (OWNED_UNITS_TAG.equals(tag)) { Unit unit = xr.makeFreeColGameObject(game, ID_ATTRIBUTE_TAG, Unit.class, true); addOwnedUnit(unit); xr.closeTag(OWNED_UNITS_TAG); } else { super.readChild(xr); } }
/** * 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; }
/** {@inheritDoc} */ @Override public void readChild(FreeColXMLReader xr) throws XMLStreamException { final Specification spec = getSpecification(); final String tag = xr.getLocalName(); if (INPUT_TAG.equals(tag)) { GoodsType type = xr.getType(spec, GOODS_TYPE_TAG, GoodsType.class, (GoodsType) null); addInput(type, xr.getAttribute(VALUE_TAG, -1)); xr.closeTag(INPUT_TAG); } else if (OUTPUT_TAG.equals(tag)) { GoodsType type = xr.getType(spec, GOODS_TYPE_TAG, GoodsType.class, (GoodsType) null); addOutput(type, xr.getAttribute(VALUE_TAG, -1)); xr.closeTag(OUTPUT_TAG); } else { super.readChild(xr); } }
/** * 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; }
/** * Initialize this object from an XML-representation of this object. * * @param xr The <code>FreeColXMLReader</code> to read from. * @throws XMLStreamException if a problem was encountered during parsing. */ public void readFromXML(FreeColXMLReader xr) throws XMLStreamException { // id is hard-wired String acc = xr.getAttribute(ACCELERATOR_TAG, ""); putValue(ACCELERATOR_KEY, ("".equals(acc)) ? null : KeyStroke.getKeyStroke(acc)); xr.closeTag(getXMLElementTagName()); }