001: /*
002: * Copyright 2001-2006 C:1 Financial Services GmbH
003: *
004: * This software is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License Version 2.1, as published by the Free Software Foundation.
007: *
008: * This software is distributed in the hope that it will be useful,
009: * but WITHOUT ANY WARRANTY; without even the implied warranty of
010: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
011: * Lesser General Public License for more details.
012: *
013: * You should have received a copy of the GNU Lesser General Public
014: * License along with this library; if not, write to the Free Software
015: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
016: */
017:
018: package de.finix.contelligent.xml.export;
019:
020: import java.io.IOException;
021: import java.io.Writer;
022: import java.text.DecimalFormat;
023: import java.text.DecimalFormatSymbols;
024: import java.util.Iterator;
025: import java.util.List;
026: import java.util.Locale;
027: import java.util.Map;
028:
029: import de.finix.contelligent.Type;
030: import de.finix.contelligent.logging.LoggingService;
031: import de.finix.contelligent.persistence.TypeProperty;
032: import de.finix.contelligent.util.XMLUtils;
033: import de.finix.contelligent.xml.DTDCatalog;
034: import de.finix.contelligent.xml.elements.DescriptionElement;
035: import de.finix.contelligent.xml.elements.TypeMetainfoElement;
036:
037: /**
038: * The default policy for transforming types into XML.
039: */
040: public class DefaultTypeXMLPolicy implements TypeXMLPolicy {
041: final static org.apache.log4j.Logger log = LoggingService
042: .getLogger(DefaultTypeXMLPolicy.class);
043:
044: public final static String DEFAULT_INDENT = " ";
045:
046: /**
047: * The format to use for formatting numbers! (we must always use the same
048: * locale so re-import works)
049: */
050: public final static DecimalFormat decimalFormat = new DecimalFormat(
051: "##################.####", new DecimalFormatSymbols(
052: Locale.US));
053:
054: final String typeElementName;
055:
056: final String indent;
057:
058: public DefaultTypeXMLPolicy() {
059: this (DEFAULT_INDENT);
060: }
061:
062: public DefaultTypeXMLPolicy(String indent) {
063: typeElementName = DTDCatalog.TYPE;
064: this .indent = indent;
065: }
066:
067: public void writeXMLHeader(Writer writer) throws IOException {
068: DTDCatalog dtdCatalog = DTDCatalog.getInstance();
069: StringBuffer output = new StringBuffer(160);
070: output
071: .append(
072: "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<!DOCTYPE ")
073: .append(DTDCatalog.TYPE_LIST).append(" PUBLIC \"")
074: .append(dtdCatalog.getPublicId(DTDCatalog.TYPE_LIST))
075: .append("\" \"").append(DTDCatalog.PUBLICID_URLPREFIX)
076: .append(dtdCatalog.getFileName(DTDCatalog.TYPE_LIST))
077: .append("\">\n\n").append("<").append(
078: DTDCatalog.TYPE_LIST).append(">\n");
079: writer.write(output.toString());
080: }
081:
082: public void writeXMLFooter(Writer writer) throws IOException {
083: writer.write("</" + DTDCatalog.TYPE_LIST + ">\n");
084: }
085:
086: public void writeType(Type type, Writer writer) throws IOException {
087: writeStartTag(type, writer);
088: writeMetainfo(type, writer);
089: writeProperties(type, writer);
090: writeEndTag(type, writer);
091: }
092:
093: protected void writeStartTag(Type type, Writer writer)
094: throws IOException {
095: writer.write(indent);
096: writer.write('<');
097: writer.write(typeElementName);
098: writer.write(" name=\"");
099: writer.write(type.getName());
100: writer.write("\"");
101: if (type.getBlueprintPath() != null) {
102: writer.write(" blueprintPath=\"");
103: writer.write(type.getBlueprintPath().toPath());
104: writer.write("\"");
105: }
106: writer.write(" group=\"");
107: writer.write(type.getTypeGroup());
108: writer.write("\"");
109: final String typeClassName = type.getClassName();
110: final String super TypeName = type.getSuperTypeName();
111: if (typeClassName != null && super TypeName == null) {
112: writer.write(" classImpl=\"");
113: writer.write(typeClassName);
114: writer.write("\"");
115: }
116: writer.write(">\n");
117: if (super TypeName != null) {
118: writer.write(indent);
119: writer.write(indent);
120: if (typeClassName != null) {
121: writer.write("<extends type=\"");
122: writer.write(super TypeName);
123: writer.write("\" classImpl=\"");
124: writer.write(typeClassName);
125: writer.write("\" />\n");
126: } else {
127: writer.write("<restricts type=\"");
128: writer.write(super TypeName);
129: writer.write("\" />\n");
130: }
131: }
132:
133: }
134:
135: protected void writeEndTag(Type type, Writer writer)
136: throws IOException {
137: writer.write(indent);
138: writer.write("</");
139: writer.write(typeElementName);
140: writer.write(">\n");
141: }
142:
143: protected void writeMetainfo(Type type, Writer writer)
144: throws IOException {
145: TypeMetainfoElement metaInfo = type.getMetainfoElement();
146: if (metaInfo != null) {
147: writer.write(indent);
148: writer.write(indent);
149: String icon = metaInfo.getIcon(); // normally required
150: if (icon != null) {
151: writer.write("<meta-info icon=\"");
152: writer.write(icon);
153: writer.write('"');
154: } else {
155: writer.write("<meta-info");
156: }
157: String gui = metaInfo.getGui();
158: if (gui != null) {
159: writer.write(" gui=\"");
160: writer.write(gui);
161: writer.write('"');
162: }
163: if (metaInfo.getRenderer() != null) {
164: writer.write(" renderer=\"");
165: writer.write(metaInfo.getRenderer());
166: writer.write('"');
167: }
168: if (metaInfo.getContent() != null) {
169: writer.write(" content=\"");
170: writer.write(metaInfo.getContent());
171: writer.write('"');
172: }
173: writer.write(" needsSecureTransfer=\"");
174: writer
175: .write(String.valueOf(metaInfo
176: .needsSecureTransfer()));
177: writer.write("\">\n");
178: List descriptionList = metaInfo.getDescriptionElementList();
179: if (descriptionList != null && descriptionList.size() > 0) {
180: Iterator descriptions = descriptionList.iterator();
181: while (descriptions.hasNext()) {
182: DescriptionElement element = (DescriptionElement) descriptions
183: .next();
184: writer.write(indent);
185: writer.write(indent);
186: writer.write(indent);
187: writer.write(indent);
188: writer.write("<description lang=\"");
189: writer.write(element.getLang());
190: writer.write("\" title=\"");
191: if (element.getTitle() != null) {
192: writer.write(element.getTitle());
193: }
194: String help = element.getHelp();
195: if (help == null) {
196: writer.write("\">");
197: } else {
198: writer.write("\" help=\"");
199: writer.write(help);
200: writer.write("\">");
201: }
202: writer.write("<![CDATA[");
203: writer.write(element.getValue());
204: writer.write("]]></description>\n");
205: }
206: }
207: writer.write(indent);
208: writer.write(indent);
209: writer.write("</meta-info>\n");
210: }
211: }
212:
213: protected void writeProperties(Type type, Writer writer)
214: throws IOException {
215: Map typePropertyMap = type.getOriginalPropertyMap();
216: Iterator it = typePropertyMap.values().iterator();
217: while (it.hasNext()) {
218: TypeProperty property = (TypeProperty) it.next();
219: writer.write(indent);
220: writer.write(indent);
221: writer.write("<property name=\"");
222: writer.write(property.getName());
223: writer.write("\" type=\"");
224: writer.write(property.getPropertyType());
225: writer.write("\" isFinal=\"");
226: writer.write(String.valueOf(property.isFinal()));
227: writer.write("\" isRequired=\"");
228: writer.write(String.valueOf(property.isRequired()));
229: writer.write("\" group=\"");
230: writer.write(property.getGroup());
231: writer.write("\" constraints=\"");
232: writer.write(XMLUtils.encodeAttribute(property
233: .getConstraints()));
234: writer.write("\" mode=\"");
235: writer.write(property.getMode());
236: writer.write("\"><![CDATA[");
237: writer.write(propertyValueToString(property.getValue()));
238: writer.write("]]></property>\n");
239: }
240: }
241:
242: static String propertyValueToString(Object value)
243: throws IOException {
244: if (value instanceof Double) {
245: return decimalFormat.format(value);
246: } else {
247: return value.toString();
248: }
249: }
250:
251: }
|