001: // XMLResourceWriter.java
002: // $Id: XMLResourceWriter.java,v 1.10 2007/02/09 22:32:04 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 java.util.Vector;
012: import java.util.Enumeration;
013:
014: import org.w3c.tools.resources.Resource;
015: import org.w3c.tools.resources.AttributeHolder;
016: import org.w3c.tools.resources.ContainerInterface;
017: import org.w3c.tools.resources.ResourceFrame;
018: import org.w3c.tools.resources.Attribute;
019: import org.w3c.tools.resources.SimpleAttribute;
020: import org.w3c.tools.resources.ArrayAttribute;
021: import org.w3c.tools.resources.FrameArrayAttribute;
022:
023: /**
024: * @version $Revision: 1.10 $
025: * @author Benoît Mahé (bmahe@w3.org)
026: */
027: public class XMLResourceWriter extends XMLWriter implements JigXML {
028:
029: protected void startResource(AttributeHolder res)
030: throws IOException {
031: String classname = res.getClass().getName();
032: writer.write('<');
033: writer.write(RESOURCE_TAG);
034: writer.write(' ');
035: writer.write(CLASS_ATTR);
036: writer.write("='");
037: writer.write(classname);
038: writer.write("'>\n");
039: }
040:
041: protected void startResourceDescr(AttributeHolder res)
042: throws IOException {
043: String classname = res.getClass().getName();
044: writer.write('<');
045: writer.write(RESOURCE_TAG);
046: writer.write(' ');
047: writer.write(CLASS_ATTR);
048: writer.write("='");
049: writer.write(classname);
050: writer.write("'>\n");
051: Vector interfaces = new Vector(10);
052: Class c = res.getClass();
053: Class intfs[] = c.getInterfaces();
054: if (intfs != null) {
055: for (int i = 0; i < intfs.length; i++)
056: interfaces.addElement(intfs[i].getName());
057: }
058: writeInherit(c.getSuperclass(), interfaces);
059: level++;
060: for (int i = 0; i < interfaces.size(); i++) {
061: String s = (String) interfaces.elementAt(i);
062: indent();
063: writer.write('<');
064: writer.write(IMPLEMENTS_TAG);
065: writer.write(' ');
066: writer.write(CLASS_ATTR);
067: writer.write("='");
068: writer.write(s);
069: writer.write("'/>\n");
070: }
071: level--;
072: writeChildren(res);
073: }
074:
075: protected void writeInherit(Class c, Vector interfaces)
076: throws IOException {
077: if (c != null) {
078: level++;
079: indent();
080: writer.write('<');
081: writer.write(INHERIT_TAG);
082: writer.write(' ');
083: writer.write(CLASS_ATTR);
084: writer.write("='");
085: writer.write(c.getName());
086: writer.write("'>\n");
087: Class intfs[] = c.getInterfaces();
088: if (intfs != null) {
089: for (int i = 0; i < intfs.length; i++)
090: interfaces.addElement(intfs[i].getName());
091: }
092: writeInherit(c.getSuperclass(), interfaces);
093: indent();
094: writer.write("</");
095: writer.write(INHERIT_TAG);
096: writer.write(">\n");
097: level--;
098: }
099: }
100:
101: protected void writeChildren(AttributeHolder res)
102: throws IOException {
103: if (res instanceof ContainerInterface) {
104: Vector vids = new Vector();
105: Enumeration e = ((ContainerInterface) res)
106: .enumerateResourceIdentifiers(true);
107: while (e.hasMoreElements()) {
108: vids.addElement((String) e.nextElement());
109: }
110: int len = vids.size();
111: level++;
112: indent();
113: writer.write('<');
114: writer.write(CHILDREN_TAG);
115: writer.write(' ');
116: writer.write(LENGTH_ATTR);
117: writer.write("='");
118: writer.write(String.valueOf(len));
119: writer.write("'>\n");
120: level++;
121: for (int i = 0; i < len; i++) {
122: indent();
123: writer.write('<');
124: writer.write(CHILD_TAG);
125: writer.write('>');
126: writer.write(encode((String) vids.elementAt(i)));
127: writer.write("</");
128: writer.write(CHILD_TAG);
129: writer.write(">\n");
130: }
131: level--;
132: indent();
133: writer.write("</");
134: writer.write(CHILDREN_TAG);
135: writer.write(">\n");
136: level--;
137: }
138: }
139:
140: protected void writeAttribute(Attribute attr, Object value,
141: boolean descr) throws IOException {
142: level++;
143: if (attr instanceof SimpleAttribute) {
144: indent();
145: writer.write('<');
146: writer.write(ATTRIBUTE_TAG);
147: writer.write(' ');
148: writer.write(NAME_ATTR);
149: writer.write("='");
150: writer.write(attr.getName());
151: writer.write("' ");
152: writer.write(FLAG_ATTR);
153: writer.write("='");
154: writer.write(attr.getFlag());
155: writer.write("' ");
156: writer.write(CLASS_ATTR);
157: writer.write("='");
158: writer.write(attr.getClass().getName());
159: writer.write("'>");
160: if (value == null) {
161: writer.write(NULL);
162: } else {
163: String pickled = ((SimpleAttribute) attr).pickle(value);
164: writer.write(encode(pickled));
165: }
166: writer.write("</");
167: writer.write(ATTRIBUTE_TAG);
168: writer.write(">\n");
169: } else if (attr instanceof ArrayAttribute) {
170: indent();
171: writer.write('<');
172: writer.write(ARRAY_TAG);
173: writer.write(' ');
174: writer.write(NAME_ATTR);
175: writer.write("='");
176: writer.write(attr.getName());
177: writer.write("' ");
178: writer.write(FLAG_ATTR);
179: writer.write("='");
180: writer.write(attr.getFlag());
181: writer.write("' ");
182: writer.write(CLASS_ATTR);
183: writer.write("='");
184: writer.write(attr.getClass().getName());
185: writer.write("' ");
186: String values[] = null;
187: if (value == null)
188: values = new String[0];
189: else
190: values = ((ArrayAttribute) attr).pickle(value);
191: int len = values.length;
192: writer.write(LENGTH_ATTR);
193: writer.write("='");
194: writer.write(String.valueOf(len));
195: writer.write("'>\n");
196: level++;
197: String rval;
198: for (int i = 0; i < len; i++) {
199: indent();
200: writer.write('<');
201: writer.write(VALUE_TAG);
202: writer.write('>');
203: rval = values[i];
204: if (rval == null) {
205: writer.write(NULL);
206: } else {
207: writer.write(encode(rval));
208: }
209: writer.write("</");
210: writer.write(VALUE_TAG);
211: writer.write(">\n");
212: }
213: level--;
214: indent();
215: writer.write("</");
216: writer.write(ARRAY_TAG);
217: writer.write(">\n");
218: } else if (attr instanceof FrameArrayAttribute) {
219: indent();
220: writer.write('<');
221: writer.write(RESARRAY_TAG);
222: writer.write(' ');
223: writer.write(NAME_ATTR);
224: writer.write("='");
225: writer.write(attr.getName());
226: writer.write("' ");
227: writer.write(CLASS_ATTR);
228: writer.write("='");
229: writer.write(attr.getClass().getName());
230: writer.write("' ");
231: ResourceFrame frames[] = null;
232: if (value == null)
233: frames = new ResourceFrame[0];
234: else
235: frames = (ResourceFrame[]) value;
236: int len = frames.length;
237: writer.write(LENGTH_ATTR);
238: writer.write("='");
239: writer.write(String.valueOf(len));
240: writer.write("'>\n");
241: if (descr) {
242: for (int i = 0; i < len; i++) {
243: writeResourceDescription(frames[i]);
244: }
245: } else {
246: for (int i = 0; i < len; i++) {
247: writeResource(frames[i]);
248: }
249: }
250: indent();
251: writer.write("</");
252: writer.write(RESARRAY_TAG);
253: writer.write(">\n");
254: }
255: level--;
256: }
257:
258: public void writeResourceDescription(AttributeHolder holder)
259: throws IOException {
260: level++;
261: indent();
262: startResourceDescr(holder);
263: Attribute attrs[] = holder.getAttributes();
264: for (int j = 0; j < attrs.length; j++) {
265: Object value = holder.unsafeGetValue(j, null);
266: //if (value != null)
267: if (!attrs[j].checkFlag(Attribute.DONTSAVE))
268: writeAttribute(attrs[j], value, true);
269: }
270: indent();
271: closeResource();
272: level--;
273: }
274:
275: public void writeResource(AttributeHolder holder)
276: throws IOException {
277: level++;
278: indent();
279: startResource(holder);
280: Attribute attrs[] = holder.getAttributes();
281: for (int j = 0; j < attrs.length; j++) {
282: Object value = holder.unsafeGetValue(j, null);
283: //if (value != null)
284: if (!attrs[j].checkFlag(Attribute.DONTSAVE))
285: writeAttribute(attrs[j], value, false);
286: }
287: indent();
288: closeResource();
289: level--;
290: }
291:
292: public XMLResourceWriter(Writer writer) {
293: super(writer);
294: }
295:
296: }
|