001: /*
002: * Copyright (C) 2004 Joe Walnes.
003: * Copyright (C) 2006, 2007 XStream Committers.
004: * All rights reserved.
005: *
006: * The software in this package is published under the terms of the BSD
007: * style license a copy of which has been included with this distribution in
008: * the LICENSE.txt file.
009: *
010: * Created on 07. March 2004 by Joe Walnes
011: */
012: package com.thoughtworks.xstream.io.xml.xppdom;
013:
014: import java.util.ArrayList;
015: import java.util.HashMap;
016: import java.util.List;
017: import java.util.Map;
018:
019: public class Xpp3Dom {
020: protected String name;
021:
022: protected String value;
023:
024: protected Map attributes;
025:
026: protected List childList;
027:
028: protected Map childMap;
029:
030: protected Xpp3Dom parent;
031:
032: public Xpp3Dom(String name) {
033: this .name = name;
034: childList = new ArrayList();
035: childMap = new HashMap();
036: }
037:
038: // ----------------------------------------------------------------------
039: // Name handling
040: // ----------------------------------------------------------------------
041:
042: public String getName() {
043: return name;
044: }
045:
046: // ----------------------------------------------------------------------
047: // Value handling
048: // ----------------------------------------------------------------------
049:
050: public String getValue() {
051: return value;
052: }
053:
054: public void setValue(String value) {
055: this .value = value;
056: }
057:
058: // ----------------------------------------------------------------------
059: // Attribute handling
060: // ----------------------------------------------------------------------
061:
062: public String[] getAttributeNames() {
063: if (null == attributes) {
064: return new String[0];
065: } else {
066: return (String[]) attributes.keySet()
067: .toArray(new String[0]);
068: }
069: }
070:
071: public String getAttribute(String name) {
072: return (null != attributes) ? (String) attributes.get(name)
073: : null;
074: }
075:
076: public void setAttribute(String name, String value) {
077: if (null == attributes) {
078: attributes = new HashMap();
079: }
080:
081: attributes.put(name, value);
082: }
083:
084: // ----------------------------------------------------------------------
085: // Child handling
086: // ----------------------------------------------------------------------
087:
088: public Xpp3Dom getChild(int i) {
089: return (Xpp3Dom) childList.get(i);
090: }
091:
092: public Xpp3Dom getChild(String name) {
093: return (Xpp3Dom) childMap.get(name);
094: }
095:
096: public void addChild(Xpp3Dom xpp3Dom) {
097: xpp3Dom.setParent(this );
098: childList.add(xpp3Dom);
099: childMap.put(xpp3Dom.getName(), xpp3Dom);
100: }
101:
102: public Xpp3Dom[] getChildren() {
103: if (null == childList) {
104: return new Xpp3Dom[0];
105: } else {
106: return (Xpp3Dom[]) childList.toArray(new Xpp3Dom[0]);
107: }
108: }
109:
110: public Xpp3Dom[] getChildren(String name) {
111: if (null == childList) {
112: return new Xpp3Dom[0];
113: } else {
114: ArrayList children = new ArrayList();
115: int size = this .childList.size();
116:
117: for (int i = 0; i < size; i++) {
118: Xpp3Dom configuration = (Xpp3Dom) this .childList.get(i);
119: if (name.equals(configuration.getName())) {
120: children.add(configuration);
121: }
122: }
123:
124: return (Xpp3Dom[]) children.toArray(new Xpp3Dom[0]);
125: }
126: }
127:
128: public int getChildCount() {
129: if (null == childList) {
130: return 0;
131: }
132:
133: return childList.size();
134: }
135:
136: // ----------------------------------------------------------------------
137: // Parent handling
138: // ----------------------------------------------------------------------
139:
140: public Xpp3Dom getParent() {
141: return parent;
142: }
143:
144: public void setParent(Xpp3Dom parent) {
145: this.parent = parent;
146: }
147: }
|