1:
42:
43: package ;
44:
45: import ;
46: import ;
47:
48: import ;
49: import ;
50: import ;
51: import ;
52: import ;
53: import ;
54: import ;
55: import ;
56: import ;
57: import ;
58: import ;
59: import ;
60: import ;
61:
62:
65: public class ModelWriter {
66:
67:
68: private static SafeTagList safeTags;
69:
70:
75: public static SafeTagList getSafeTags() {
76: if (safeTags == null) {
77: safeTags = new SafeTagList();
78: safeTags.add(ClassModelTags.OBJECTS_TAG);
79: safeTags.add(ClassModelTags.OBJECT_TAG);
80: safeTags.add(ClassModelTags.CONSTRUCTOR_TAG);
81: safeTags.add(ClassModelTags.ELEMENT_PROPERTY_TAG);
82: safeTags.add(ClassModelTags.LOOKUP_PROPERTY_TAG);
83: safeTags.add(ClassModelTags.ATTRIBUTE_PROPERTY_TAG);
84: safeTags.add(ClassModelTags.PARAMETER_TAG);
85: safeTags.add(ClassModelTags.INCLUDE_TAG);
86: safeTags.add(ClassModelTags.IGNORED_PROPERTY_TAG);
87: safeTags.add(ClassModelTags.MANUAL_TAG);
88: safeTags.add(ClassModelTags.MAPPING_TAG);
89: safeTags.add(ClassModelTags.TYPE_TAG);
90: }
91: return safeTags;
92: }
93:
94:
95: private XMLWriterSupport writerSupport;
96:
97:
98: private DescriptionModel model;
99:
100:
103: public ModelWriter() {
104: this.writerSupport = new XMLWriterSupport(getSafeTags(), 0);
105: }
106:
107:
112: public DescriptionModel getModel() {
113: return this.model;
114: }
115:
116:
121: public void setModel(final DescriptionModel model) {
122: this.model = model;
123: }
124:
125:
132: public static void writeXMLHeader(final Writer writer) throws IOException {
133: writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
134: writer.write(XMLWriterSupport.getLineSeparator());
135: }
136:
137:
145: protected void writeStandardComment(final Writer writer, final Comments comments) throws IOException {
146: if ((comments == null) || (comments.getOpenTagComment() == null)) {
147: writer.write(
148: "<!-- CVSTag: $Id: ModelWriter.java,v 1.3 2005/10/18 13:32:20 mungady Exp $ "
149: + comments + " -->"
150: );
151: writer.write(XMLWriterSupport.getLineSeparator());
152: }
153: else {
154: writeComment(writer, comments.getOpenTagComment());
155: }
156: }
157:
158:
166: protected void writeComment(final Writer writer, final String[] comments) throws IOException {
167: if (comments == null) {
168: return;
169: }
170: for (int i = 0; i < comments.length; i++) {
171: this.writerSupport.indent(writer, XMLWriterSupport.INDENT_ONLY);
172: writer.write("<!--");
173: writer.write(comments[i]);
174: writer.write("-->");
175: writer.write(XMLWriterSupport.getLineSeparator());
176: }
177: }
178:
179:
187: protected void writeOpenComment(final Writer writer, final Comments comments) throws IOException {
188: if (comments == null) {
189: return;
190: }
191: if (comments.getOpenTagComment() == null) {
192: return;
193: }
194: writeComment(writer, comments.getOpenTagComment());
195: }
196:
197:
205: protected void writeCloseComment(final Writer writer, final Comments comments) throws IOException {
206: if (comments == null) {
207: return;
208: }
209: if (comments.getCloseTagComment() == null) {
210: return;
211: }
212: writeComment(writer, comments.getCloseTagComment());
213: }
214:
215:
225: protected void writeTag(final Writer writer,
226: final String tagName,
227: final AttributeList attributes,
228: final Comments comments) throws IOException {
229: if (comments == null) {
230: this.writerSupport.writeTag(writer, tagName, attributes, XMLWriterSupport.CLOSE);
231: }
232: else {
233: writeOpenComment(writer, comments);
234: if (comments.getCloseTagComment() != null) {
235: this.writerSupport.writeTag(writer, tagName, attributes, XMLWriterSupport.OPEN);
236: writeCloseComment(writer, comments);
237: this.writerSupport.writeCloseTag(writer, tagName);
238: }
239: else {
240: this.writerSupport.writeTag(writer, tagName, attributes, XMLWriterSupport.CLOSE);
241: }
242: }
243: }
244:
245:
256: protected void writeTag(final Writer writer,
257: final String tagName,
258: final String attribute,
259: final String value,
260: final Comments comments) throws IOException {
261: if (comments == null) {
262: this.writerSupport.writeTag(writer, tagName, attribute, value , XMLWriterSupport.CLOSE);
263: }
264: else {
265: writeOpenComment(writer, comments);
266: if (comments.getCloseTagComment() != null) {
267: this.writerSupport.writeTag(
268: writer, tagName, attribute, value, XMLWriterSupport.OPEN
269: );
270: writeCloseComment(writer, comments);
271: this.writerSupport.writeCloseTag(writer, tagName);
272: }
273: else {
274: this.writerSupport.writeTag(
275: writer, tagName, attribute, value, XMLWriterSupport.CLOSE
276: );
277: }
278: }
279: }
280:
281:
288: public void write(final Writer writer) throws IOException {
289:
290: writeStandardComment(writer, this.model.getModelComments());
291: this.writerSupport.writeTag(writer, ClassModelTags.OBJECTS_TAG);
292: final String[] sources = this.model.getSources();
293: for (int i = 0; i < sources.length; i++) {
294: final Comments comments = this.model.getIncludeComment(sources[i]);
295: writeTag(
296: writer,
297: ClassModelTags.INCLUDE_TAG, ClassModelTags.SOURCE_ATTR, sources[i], comments
298: );
299: }
300:
301: for (int i = 0; i < this.model.size(); i++) {
302: final ClassDescription cd = this.model.get(i);
303: writeClassDescription(writer, cd);
304: }
305:
306: final ManualMappingInfo[] mappings = getModel().getMappingModel().getManualMapping();
307: for (int i = 0; i < mappings.length; i++) {
308: final ManualMappingInfo mi = mappings[i];
309: writeManualMapping(writer, mi);
310: }
311:
312: final MultiplexMappingInfo[] mmappings = getModel().getMappingModel().getMultiplexMapping();
313: for (int i = 0; i < mmappings.length; i++) {
314: final MultiplexMappingInfo mi = mmappings[i];
315: writeMultiplexMapping(writer, mi);
316: }
317:
318: writeCloseComment(writer, this.model.getModelComments());
319: this.writerSupport.writeCloseTag(writer, ClassModelTags.OBJECTS_TAG);
320:
321: }
322:
323:
331: protected void writeManualMapping(final Writer writer, final ManualMappingInfo mi) throws IOException {
332: final AttributeList al = new AttributeList();
333: al.setAttribute(ClassModelTags.CLASS_ATTR, mi.getBaseClass().getName());
334: al.setAttribute(ClassModelTags.READ_HANDLER_ATTR, mi.getReadHandler().getName());
335: al.setAttribute(ClassModelTags.WRITE_HANDLER_ATTR, mi.getWriteHandler().getName());
336: writeTag(writer, ClassModelTags.MANUAL_TAG, al, mi.getComments());
337: }
338:
339:
347: protected void writeMultiplexMapping(final Writer writer, final MultiplexMappingInfo mi)
348: throws IOException {
349:
350: final TypeInfo[] tis = mi.getChildClasses();
351:
352: final AttributeList al = new AttributeList();
353: al.setAttribute(ClassModelTags.BASE_CLASS_ATTR, mi.getBaseClass().getName());
354: al.setAttribute(ClassModelTags.TYPE_ATTR, mi.getTypeAttribute());
355: getWriterSupport().writeTag(writer, ClassModelTags.MAPPING_TAG, al, XMLWriterSupport.OPEN);
356:
357: for (int j = 0; j < tis.length; j++) {
358: final AttributeList tiAttr = new AttributeList();
359: tiAttr.setAttribute(ClassModelTags.NAME_ATTR, tis[j].getName());
360: tiAttr.setAttribute(ClassModelTags.CLASS_ATTR, tis[j].getType().getName());
361: writeTag(writer, ClassModelTags.TYPE_TAG, tiAttr, tis[j].getComments());
362: }
363:
364: getWriterSupport().writeCloseTag(writer, ClassModelTags.MAPPING_TAG);
365: }
366:
367:
375: protected void writeClassDescription(final Writer writer, final ClassDescription cd) throws IOException {
376:
377: if (cd.isUndefined()) {
378: return;
379: }
380:
381: final AttributeList al = new AttributeList();
382: al.setAttribute(ClassModelTags.CLASS_ATTR, cd.getName());
383: if (cd.getRegisterKey() != null) {
384: al.setAttribute(ClassModelTags.REGISTER_NAMES_ATTR, cd.getRegisterKey());
385: }
386: if (cd.isPreserve()) {
387: al.setAttribute(ClassModelTags.IGNORE_ATTR, "true");
388: }
389: this.writerSupport.writeTag(writer, ClassModelTags.OBJECT_TAG, al, XMLWriterSupport.OPEN);
390:
391: final TypeInfo[] constructorInfo = cd.getConstructorDescription();
392: if (constructorInfo != null && constructorInfo.length != 0) {
393: this.writerSupport.writeTag(writer, ClassModelTags.CONSTRUCTOR_TAG);
394: for (int i = 0; i < constructorInfo.length; i++) {
395: final AttributeList constructorList = new AttributeList();
396: constructorList.setAttribute(
397: ClassModelTags.CLASS_ATTR, constructorInfo[i].getType().getName()
398: );
399: constructorList.setAttribute(
400: ClassModelTags.PROPERTY_ATTR, constructorInfo[i].getName()
401: );
402: writeTag(writer, ClassModelTags.PARAMETER_TAG, constructorList,
403: constructorInfo[i].getComments());
404: }
405: this.writerSupport.writeCloseTag(writer, ClassModelTags.CONSTRUCTOR_TAG);
406: }
407:
408: final PropertyInfo[] properties = cd.getProperties();
409: for (int i = 0; i < properties.length; i++) {
410: writePropertyInfo(writer, properties[i]);
411: }
412:
413: this.writerSupport.writeCloseTag(writer, ClassModelTags.OBJECT_TAG);
414: }
415:
416:
424: private void writePropertyInfo(final Writer writer, final PropertyInfo ipi) throws IOException {
425: final AttributeList props = new AttributeList();
426: props.setAttribute(ClassModelTags.NAME_ATTR, ipi.getName());
427:
428: if (ipi instanceof IgnoredPropertyInfo) {
429: writeTag(writer, ClassModelTags.IGNORED_PROPERTY_TAG, props, ipi.getComments());
430: return;
431: }
432:
433: if (ipi.getPropertyType().equals(PropertyType.ATTRIBUTE)) {
434: props.setAttribute(ClassModelTags.ATTRIBUTE_ATTR, ipi.getXmlName());
435: props.setAttribute(ClassModelTags.ATTRIBUTE_HANDLER_ATTR, ipi.getXmlHandler());
436: writeTag(writer, ClassModelTags.ATTRIBUTE_PROPERTY_TAG, props, ipi.getComments());
437: }
438: else if (ipi.getPropertyType().equals(PropertyType.ELEMENT)) {
439: if (ipi.getComments() == null || ipi.getComments().getOpenTagComment() == null)
440: {
441: this.writerSupport.indent(writer, XMLWriterSupport.INDENT_ONLY);
442: writer.write("<!-- property type is " + ipi.getType() + " -->");
443: writer.write(System.getProperty("line.separator", "\n"));
444: }
445: props.setAttribute(ClassModelTags.ELEMENT_ATTR, ipi.getXmlName());
446: writeTag(writer, ClassModelTags.ELEMENT_PROPERTY_TAG, props, ipi.getComments());
447: }
448: else {
449: props.setAttribute(ClassModelTags.LOOKUP_ATTR, ipi.getXmlName());
450: writeTag(writer, ClassModelTags.LOOKUP_PROPERTY_TAG, props, ipi.getComments());
451: }
452: }
453:
454:
459: public XMLWriterSupport getWriterSupport() {
460: return this.writerSupport;
461: }
462: }