001: // XMLDescrWriter.java
002: // $Id: XMLDescrWriter.java,v 1.7 2007/04/04 12:14:48 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 1999.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.tools.resources.serialization.xml;
006:
007: import java.io.Writer;
008: import java.io.Reader;
009: import java.io.IOException;
010:
011: import org.w3c.tools.resources.Resource;
012: import org.w3c.tools.resources.ResourceFrame;
013: import org.w3c.tools.resources.Attribute;
014: import org.w3c.tools.resources.SimpleAttribute;
015: import org.w3c.tools.resources.ArrayAttribute;
016: import org.w3c.tools.resources.FrameArrayAttribute;
017: import org.w3c.tools.resources.serialization.AttributeDescription;
018: import org.w3c.tools.resources.serialization.ResourceDescription;
019: import org.w3c.tools.resources.serialization.EmptyDescription;
020:
021: /**
022: * @version $Revision: 1.7 $
023: * @author Benoît Mahé (bmahe@w3.org)
024: */
025: public class XMLDescrWriter extends XMLWriter implements JigXML {
026:
027: protected void startResource(ResourceDescription res)
028: throws IOException {
029: String classname = res.getClassName();
030: writer.write('<');
031: writer.write(RESOURCE_TAG);
032: writer.write(' ');
033: writer.write(CLASS_ATTR);
034: writer.write("='");
035: writer.write(classname);
036: writer.write("'>\n");
037: writeInherit(res.getClassHierarchy(), 1);
038: writeInterfaces(res.getInterfaces());
039: }
040:
041: protected void startDescription(ResourceDescription res)
042: throws IOException {
043: String classname = res.getClassName();
044: writer.write('<');
045: writer.write(DESCR_TAG);
046: writer.write(" ");
047: writer.write(CLASS_ATTR);
048: writer.write("='");
049: writer.write(classname);
050: String id = res.getIdentifier();
051: if (id == null)
052: id = NULL;
053: writer.write("' ");
054: writer.write(NAME_ATTR);
055: writer.write("='");
056: writer.write(id);
057: writer.write("'>\n");
058: }
059:
060: protected void closeDescription() throws IOException {
061: writer.write("</");
062: writer.write(DESCR_TAG);
063: writer.write(">\n");
064: }
065:
066: protected void writeInherit(String classes[], int idx)
067: throws IOException {
068: if (idx < classes.length) {
069: level++;
070: indent();
071: writer.write('<');
072: writer.write(INHERIT_TAG);
073: writer.write(' ');
074: writer.write(CLASS_ATTR);
075: writer.write("='");
076: writer.write(classes[idx]);
077: writer.write("'>\n");
078: writeInherit(classes, ++idx);
079: indent();
080: writer.write("</");
081: writer.write(INHERIT_TAG);
082: writer.write(">\n");
083: level--;
084: }
085: }
086:
087: protected void writeInterfaces(String classes[]) throws IOException {
088: level++;
089: for (int i = 0; i < classes.length; i++) {
090: indent();
091: writer.write('<');
092: writer.write(IMPLEMENTS_TAG);
093: writer.write(' ');
094: writer.write(CLASS_ATTR);
095: writer.write("='");
096: writer.write(classes[i]);
097: writer.write("'/>\n");
098: }
099: level--;
100: }
101:
102: protected void writeAttributeDescription(AttributeDescription descr)
103: throws IOException {
104: level++;
105: String classname = descr.getClassName();
106: Attribute attr = descr.getAttribute();
107: Object value = descr.getValue();
108: if (attr instanceof SimpleAttribute) {
109: indent();
110: writer.write("<" + ATTRIBUTE_TAG + " " + NAME_ATTR + "='");
111: writer.write(descr.getName());
112: writer.write("' " + FLAG_ATTR + "='");
113: writer.write(attr.getFlag());
114: writer.write("' " + CLASS_ATTR + "='");
115: writer.write(classname);
116: if (value == null) {
117: writer.write("'>" + NULL);
118: } else {
119: writer.write("'>");
120: writer.write(encode(((SimpleAttribute) attr)
121: .pickle(value)));
122: }
123: writer.write("</");
124: writer.write(ATTRIBUTE_TAG);
125: writer.write(">\n");
126: } else if (attr instanceof ArrayAttribute) {
127: indent();
128: writer.write('<');
129: writer.write(ARRAY_TAG);
130: writer.write(' ');
131: writer.write(NAME_ATTR);
132: writer.write("='");
133: writer.write(descr.getName());
134: writer.write("' ");
135: writer.write(FLAG_ATTR);
136: writer.write("='");
137: writer.write(attr.getFlag());
138: writer.write("' ");
139: writer.write(CLASS_ATTR);
140: writer.write("='");
141: writer.write(classname);
142: writer.write("' ");
143: String values[] = null;
144: if (value == null)
145: values = new String[0];
146: else
147: values = ((ArrayAttribute) attr).pickle(value);
148: int len = values.length;
149: writer.write(LENGTH_ATTR);
150: writer.write("='");
151: writer.write(String.valueOf(len));
152: writer.write("'>\n");
153: level++;
154: for (int i = 0; i < len; i++) {
155: indent();
156: writer.write('<');
157: writer.write(VALUE_TAG);
158: writer.write('>');
159: writer.write(encode(values[i]));
160: writer.write("</");
161: writer.write(VALUE_TAG);
162: writer.write(">\n");
163: }
164: level--;
165: indent();
166: writer.write("</");
167: writer.write(ARRAY_TAG);
168: writer.write(">\n");
169: } else if (attr instanceof FrameArrayAttribute) {
170: indent();
171: writer.write('<');
172: writer.write(RESARRAY_TAG);
173: writer.write(' ');
174: writer.write(NAME_ATTR);
175: writer.write("='");
176: writer.write(descr.getName());
177: writer.write("' ");
178: writer.write(CLASS_ATTR);
179: writer.write("='");
180: writer.write(classname);
181: writer.write("' ");
182: ResourceDescription frames[] = null;
183: if (value == null)
184: frames = new ResourceDescription[0];
185: else
186: frames = (ResourceDescription[]) value;
187: int len = frames.length;
188: writer.write(LENGTH_ATTR);
189: writer.write("='");
190: writer.write(String.valueOf(len));
191: writer.write("'>\n");
192: for (int i = 0; i < len; i++) {
193: writeResourceDescription(frames[i]);
194: }
195: indent();
196: writer.write("</");
197: writer.write(RESARRAY_TAG);
198: writer.write(">\n");
199: }
200: level--;
201: }
202:
203: public void writeResourceDescription(ResourceDescription resource)
204: throws IOException {
205: level++;
206: indent();
207: if (resource instanceof EmptyDescription) {
208: startDescription(resource);
209: indent();
210: closeDescription();
211: } else {
212: startResource(resource);
213: AttributeDescription attrs[] = resource
214: .getAttributeDescriptions();
215: for (int j = 0; j < attrs.length; j++)
216: writeAttributeDescription(attrs[j]);
217: indent();
218: closeResource();
219: }
220: level--;
221: }
222:
223: public XMLDescrWriter(Writer writer) {
224: super(writer);
225: }
226:
227: }
|