public Object getPrototype() { if (prototypes == null) { prototypes = new HashMap<String, Object>(); } if (!prototypes.containsKey(current.getName())) { prototypes.put(current.getName(), factory.create(current.getName())); } return prototypes.get(current.getName()); }
private XMLElement writeStorable(DOMStorable o) throws IOException { String tagName = factory.getName(o); if (tagName == null) throw new IllegalArgumentException("no tag name for:" + o); openElement(tagName); XMLElement element = current; if (objectids.containsKey(o)) { addAttribute("ref", (String) objectids.get(o)); } else { String id = Integer.toString(objectids.size(), 16); objectids.put(o, id); addAttribute("id", id); o.write(this); } closeElement(); return element; }
public void writeObject(Object o) throws IOException { if (o == null) { openElement("null"); closeElement(); } else if (o instanceof DOMStorable) { writeStorable((DOMStorable) o); } else if (o instanceof String) { openElement("string"); addText((String) o); closeElement(); } else if (o instanceof Integer) { openElement("int"); addText(o.toString()); closeElement(); } else if (o instanceof Long) { openElement("long"); addText(o.toString()); closeElement(); } else if (o instanceof Double) { openElement("double"); // Remove the awkard .0 at the end of each number String str = o.toString(); if (str.endsWith(".0")) str = str.substring(0, str.length() - 2); addText(str); closeElement(); } else if (o instanceof Float) { openElement("float"); // Remove the awkard .0 at the end of each number String str = o.toString(); if (str.endsWith(".0")) str = str.substring(0, str.length() - 2); addText(str); closeElement(); } else if (o instanceof Boolean) { openElement("boolean"); addText(o.toString()); closeElement(); } else if (o instanceof Color) { Color c = (Color) o; openElement("color"); addAttribute("rgba", "#" + Integer.toHexString(c.getRGB())); closeElement(); } else if (o instanceof int[]) { openElement("intArray"); int[] a = (int[]) o; for (int i = 0; i < a.length; i++) { writeObject(new Integer(a[i])); } closeElement(); } else if (o instanceof float[]) { openElement("floatArray"); float[] a = (float[]) o; for (int i = 0; i < a.length; i++) { writeObject(new Float(a[i])); } closeElement(); } else if (o instanceof double[]) { openElement("doubleArray"); double[] a = (double[]) o; for (int i = 0; i < a.length; i++) { writeObject(new Double(a[i])); } closeElement(); } else if (o instanceof Font) { Font f = (Font) o; openElement("font"); addAttribute("name", f.getName()); addAttribute("style", f.getStyle()); addAttribute("size", f.getSize()); closeElement(); } else if (o instanceof Enum) { openElement("enum"); Enum e = (Enum) o; addAttribute("type", factory.getEnumName(e)); addText(factory.getEnumValue(e)); closeElement(); } else { throw new IllegalArgumentException("unable to store: " + o + " " + o.getClass()); } }