001: /*
002: * CONFIDENTIAL AND PROPRIETARY SOURCE CODE OF
003: * NETSCAPE COMMUNICATIONS CORPORATION
004: *
005: * Copyright (c) 1996 Netscape Communications Corporation.
006: * All Rights Reserved.
007: * Use of this Source Code is subject to the terms of the applicable
008: * license agreement from Netscape Communications Corporation.
009: */
010:
011: package soif;
012:
013: import util.ReportError;
014:
015: /**
016: Represent nodes in the taxonomy.
017: *
018: */
019: public class TaxonomyNode {
020: /* short cut vars */
021: private String id;
022: private String parentId;
023: private String label;
024:
025: private AVPairs list;
026:
027: /*--------------*/
028: /* constructors */
029: /*--------------*/
030:
031: /**
032: * Constructor.
033: * @param id id
034: * @param parentId parent id
035: * @param taxonomyId taxonomy id
036: * @param description description
037: * @exception IllegalArgumentException on null id
038: */
039: public TaxonomyNode(String id, String parentId, String taxonomyId,
040: String description) {
041: if (id == null) {
042: throw new IllegalArgumentException(
043: "classification id cannot be null");
044: }
045: insert("id", id);
046: insert("parent-id", parentId);
047: insert("taxonomy-id", taxonomyId);
048: insert("description", description);
049:
050: setLabel();
051:
052: if (parentId != null) {
053: if ((parentId.compareTo("ROOT") != 0)
054: && (id.substring(0, parentId.length()).compareTo(
055: parentId) != 0)) {
056: ReportError.reportError(ReportError.INTERNAL,
057: "TaxonomyNode", "TaxonomyNode",
058: Messages.TAXIDPARENTMISMATCH);
059: System.out.println(toString());
060: }
061: }
062: }
063:
064: /**
065: * Constructor.
066: * @param soif soif chunk
067: * @exception IllegalArgumentException on null id
068: */
069: public TaxonomyNode(SOIF soif) {
070: id = soif.getValue("Id");
071: if (id == null) {
072: throw new IllegalArgumentException(
073: "classification id cannot be null");
074: }
075: parentId = soif.getValue("Parent-Id");
076: list = soif.list;
077: setLabel();
078: }
079:
080: /**
081: * Set the label to be printed on the diagram node.
082: */
083: public void setLabel() {
084: if (parentId == null) {
085: return;
086: }
087: if (parentId.length() == 0) {
088: return;
089: }
090:
091: if (id.length() > 0) {
092: if ("ROOT".compareTo(parentId) == 0) {
093: label = id;
094: } else {
095: // System.out.println( "p [" + parentId + "]" );
096: // System.out.println( "i [" + id + "]" );
097: label = id.substring((parentId.length() + 1), id
098: .length());
099: }
100: } else {
101: label = "";
102: }
103: }
104:
105: /**
106: * Return the label.
107: */
108: public String getLabel() {
109: return label;
110: }
111:
112: /**
113: * Return the id.
114: */
115: public String getId() {
116: return id;
117: }
118:
119: /**
120: * Return the parent id.
121: */
122: public String getParentId() {
123: return parentId;
124: }
125:
126: /**
127: * Get value by attribute.
128: * Ignores case of attribute name.
129: * @param s the attribute
130: */
131: public String getValue(String s) {
132: return list.getValue(s);
133: }
134:
135: /**
136: * Set value by attribute.
137: * Ignores case of attribute name.
138: * Note that it is possible to set id, taxonomyid
139: * and parentid here.
140: * However, because this information is significant
141: * to other nodes in the tree, this isn't really
142: * recommended.
143: * Safer methods will eventually be provided and
144: * use of this method may be restricted to non id
145: * attributes.
146: * <p>
147: * Note that in certain cases this setValue recurses
148: * and that the boolean success value only refers
149: * the the success of the principal and not the
150: * secondary setValue()s.
151: * In this context the secondary setValue() calls
152: * reference an AVPair which must exist for the
153: * TaxonomyNode so this is not an issue.
154: * @param s the attribute
155: * @param v the value
156: */
157: public boolean setValue(String s, String v) {
158: if ("Id".equalsIgnoreCase(s)) {
159: id = v;
160: setLabel();
161: }
162: if ("Parent-Id".equalsIgnoreCase(s)) {
163: parentId = v;
164: if ("ROOT".compareTo(v) == 0) {
165: setValue("Id", label);
166: } else {
167: setValue("Id", v + ":" + label);
168: }
169: }
170:
171: return list.setValue(s, v);
172: }
173:
174: /**
175: * Inserts a new AVPairs instance.
176: * The inserter of duplicates (particular of
177: * key attributes) is assumed to know what they're doing.
178: * @param att the attribute
179: * @param val the value
180: */
181: public void insert(String att, String val) {
182: if (list == null) {
183: list = new AVPairs(att, val);
184: } else {
185: list.insertAfter(new AVPairs(att, val));
186: }
187:
188: if ("id".equalsIgnoreCase(att) == true) {
189: id = val;
190: setLabel();
191: }
192: if ("Parent-Id".equalsIgnoreCase(att) == true) {
193: parentId = val;
194: setLabel();
195: }
196: }
197:
198: /**
199: * Remove AVPair by attribute.
200: * Ignores case of attribute name.
201: * @param s the attribute
202: */
203: public void remove(String s) {
204: list = list.remove(list.getAVPair(s));
205: if ("id".equalsIgnoreCase(s) == true) {
206: id = "";
207: setLabel();
208: }
209: if ("Parent-Id".equalsIgnoreCase(s) == true) {
210: parentId = "";
211: setLabel();
212: }
213: }
214:
215: /**
216: * A convenience method which bundles insert(),
217: * setValue() and remove().
218: * If the proposed value is length 0, remove() is called.
219: * Otherwise, setValue() is called.
220: * If setValue() fails, insertAfter() is called.
221: * @param att attribute
222: * @param val value
223: */
224: public void update(String att, String val) {
225: list = list.update(att, val);
226: }
227:
228: /**
229: * Return SOIF.
230: */
231: public String toSOIF() {
232: StringBuffer sb = new StringBuffer();
233:
234: sb.append("@"
235: + (String) ((parentId == null) ? "TAXONOMY"
236: : ((parentId.length() == 0) ? "TAXONOMY"
237: : "CLASSIFICATION")) + " { -\n");
238: sb.append(list.toSOIFList());
239: sb.append("\n}\n");
240:
241: return sb.toString();
242: }
243:
244: public String toString() {
245: return "TaxonomyNode instance: (" + Header.VERSION + ")\n"
246: + "\tparentId\t= [" + parentId + "]\n" + "\tid\t= ["
247: + id + "]\n" + "\tlabel\t= [" + label + "]\n"
248: + "\tlist\t= [" + list.toString() + "]\n" + ")\n";
249: }
250: }
|