KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nightlabs > editor2d > iofilter > ManifestWriter


1 /* *****************************************************************************
2  * NightLabs Editor2D - Graphical editor framework *
3  * Copyright (C) 2004-2005 NightLabs - http://NightLabs.org *
4  * *
5  * This library is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or (at your option) any later version. *
9  * *
10  * This library is distributed in the hope that it will be useful, *
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
13  * Lesser General Public License for more details. *
14  * *
15  * You should have received a copy of the GNU Lesser General Public *
16  * License along with this library; if not, write to the *
17  * Free Software Foundation, Inc., *
18  * 51 Franklin St, Fifth Floor, *
19  * Boston, MA 02110-1301 USA *
20  * *
21  * Or get it online : *
22  * http://www.gnu.org/copyleft/lesser.html *
23  * *
24  * *
25  ******************************************************************************/

26
27 package org.nightlabs.editor2d.iofilter;
28
29 import java.util.Collection JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import org.nightlabs.editor2d.ImageDrawComponent;
33 import org.nightlabs.editor2d.MultiLayerDrawComponent;
34 import org.nightlabs.editor2d.impl.ImageDrawComponentImpl;
35
36 public class ManifestWriter
37 {
38     public static final String JavaDoc DEFAULT_CONTENT_TYPE = "vnd.nightlabs.editor2d.mldc";
39     public static final String JavaDoc DEFAULT_CONTENT_NAME = "content.xml";
40     public static final String JavaDoc DEFAULT_XML_VERSION = "1.0";
41     public static final String JavaDoc DEFAULT_ENCODING = "UTF-8";
42     public static final String JavaDoc DEFAULT_IMAGE_DIR = "images";
43     public static final String JavaDoc DEFAULT_CONTENT_MEDIA_TYPE = "text/xml";
44     public static final String JavaDoc DEFAULT_XMLNS = "http://jfire.nightlabs.org/editor2d/manifest";
45     
46     public static final String JavaDoc DEFAULT_MANIFEST_DTD = "Manifest.dtd";
47     public static final String JavaDoc DEFAULT_MANIFEST_IDENTIFIER = "-//NightLabs GmbH//DTD Editor2D 1.0//EN";
48     public static final String JavaDoc DEFAULT_CONTENT_VERSION = "1.0";
49     
50     protected static final String JavaDoc ROW_BREAK = "\n";
51     
52     protected StringBuffer JavaDoc stringBuffer;
53     public ManifestWriter()
54     {
55         this(DEFAULT_CONTENT_TYPE, DEFAULT_CONTENT_NAME, DEFAULT_XML_VERSION, DEFAULT_ENCODING);
56     }
57
58     public ManifestWriter(String JavaDoc contentType)
59     {
60         this(contentType, DEFAULT_CONTENT_NAME, DEFAULT_XML_VERSION, DEFAULT_ENCODING);
61     }
62     
63     public ManifestWriter(String JavaDoc contentType, String JavaDoc contentName, String JavaDoc xmlVersion, String JavaDoc encoding)
64     {
65         super();
66         this.contentType = contentType;
67         this.contentName = contentName;
68         this.xmlVersion = xmlVersion;
69         this.encoding = encoding;
70         stringBuffer = new StringBuffer JavaDoc();
71     }
72     
73     /**
74      * the name of the contentType
75      */

76     protected String JavaDoc contentType = DEFAULT_CONTENT_TYPE;
77     public String JavaDoc getContentType() {
78         return contentType;
79     }
80     public void setContentType(String JavaDoc contentType) {
81         this.contentType = contentType;
82     }
83
84     /**
85      * the name of the file which contains the content
86      */

87     protected String JavaDoc contentName = DEFAULT_CONTENT_NAME;
88     public String JavaDoc getContentName() {
89         return contentName;
90     }
91     public void setContentName(String JavaDoc contentName) {
92         this.contentName = contentName;
93     }
94
95     /**
96      * the version of the XML
97      */

98     protected String JavaDoc xmlVersion = DEFAULT_XML_VERSION;
99     public String JavaDoc getXmlVersion() {
100         return xmlVersion;
101     }
102     public void setXmlVersion(String JavaDoc xmlVersion) {
103         this.xmlVersion = xmlVersion;
104     }
105
106     /**
107      * The encoding for the Charset
108      */

109     protected String JavaDoc encoding = DEFAULT_ENCODING;
110     public String JavaDoc getEncoding() {
111         return encoding;
112     }
113     public void setEncoding(String JavaDoc encoding) {
114         this.encoding = encoding;
115     }
116
117     /**
118      * the name of the ImageDirectory
119      */

120     protected String JavaDoc imageDirectory = DEFAULT_IMAGE_DIR;
121     public String JavaDoc getImageDirectory() {
122         return imageDirectory;
123     }
124     public void setImageDirectory(String JavaDoc imageDirectory) {
125         this.imageDirectory = imageDirectory;
126     }
127
128     /**
129      * the name of MediaType for the content
130      */

131     protected String JavaDoc contentMediaType = DEFAULT_CONTENT_MEDIA_TYPE;
132     public String JavaDoc getContentMediaType() {
133         return contentMediaType;
134     }
135     public void setContentMediaType(String JavaDoc contentMediaType) {
136         this.contentMediaType = contentMediaType;
137     }
138
139     /**
140      * the XML NameSpace
141      */

142     protected String JavaDoc xmlns = DEFAULT_XMLNS;
143     public String JavaDoc getXmlNS() {
144         return xmlns;
145     }
146     public void setXmlNS(String JavaDoc xmlns) {
147         this.xmlns = xmlns;
148     }
149
150     /**
151      * the name of the DTD-File for the Manifest
152      */

153     protected String JavaDoc manifestDTD = DEFAULT_MANIFEST_DTD;
154     public String JavaDoc getManifestDTD() {
155         return manifestDTD;
156     }
157     public void setManifestDTD(String JavaDoc manifestDTD) {
158         this.manifestDTD = manifestDTD;
159     }
160
161     /**
162      * the name for the ManifestIdentifier
163      */

164     protected String JavaDoc manifestIdentifier = DEFAULT_MANIFEST_IDENTIFIER;
165     public String JavaDoc getManifestIdentifier() {
166         return manifestIdentifier;
167     }
168     public void setManifestIdentifier(String JavaDoc manifestIdentifier) {
169         this.manifestIdentifier = manifestIdentifier;
170     }
171
172 // protected String contentVersion = DEFAULT_CONTENT_VERSION;
173
// public String getContentVersion() {
174
// return contentVersion;
175
// }
176
// public void setContentVersion(String contentVersion) {
177
// this.contentVersion = contentVersion;
178
// }
179

180     protected void writeHeader(StringBuffer JavaDoc sb)
181     {
182         sb.append("<?xml version=\"");
183         sb.append(xmlVersion);
184         sb.append("\" encoding=\"");
185         sb.append(encoding);
186         sb.append("\"?>");
187         sb.append(ROW_BREAK);
188     }
189
190     protected void writeNS(StringBuffer JavaDoc sb)
191     {
192         sb.append("<manifest:manifest ");
193         sb.append("xmlns:manifest=\"");
194         sb.append(xmlns);
195         sb.append("\">");
196         sb.append(ROW_BREAK);
197     }
198     
199     protected void writeDTD(StringBuffer JavaDoc sb)
200     {
201         sb.append("<!DOCTYPE manifest:manifest PUBLIC \"");
202         sb.append(manifestIdentifier);
203         sb.append("\" \"");
204         sb.append(manifestDTD);
205         sb.append("\">");
206         sb.append(ROW_BREAK);
207     }
208     
209     protected void writeContentType(StringBuffer JavaDoc sb)
210     {
211         sb.append("<manifest:file-entry manifest:media-type=\"application/");
212         sb.append(contentType);
213         sb.append("\" manifest:full-path=\"/\"/>");
214         sb.append(ROW_BREAK);
215     }
216     
217     protected void writeContent(StringBuffer JavaDoc sb)
218     {
219         sb.append("<manifest:file-entry manifest:media-type=\"");
220         sb.append(contentMediaType);
221         sb.append("\" manifest:full-path=\"");
222         sb.append(contentName);
223         sb.append("\"/>");
224         sb.append(ROW_BREAK);
225     }
226     
227 // protected void writeContentVersion(StringBuffer sb)
228
// {
229
// sb.append("<version="+contentVersion+">");
230
// sb.append(ROW_BREAK);
231
// }
232

233     protected void writeImages(StringBuffer JavaDoc sb, MultiLayerDrawComponent mldc)
234     {
235         Collection JavaDoc imageDCs = mldc.getDrawComponents(ImageDrawComponentImpl.class);
236         if (imageDCs != null)
237         {
238             for (Iterator JavaDoc it = imageDCs.iterator(); it.hasNext(); )
239             {
240                 ImageDrawComponent image = (ImageDrawComponent) it.next();
241                 String JavaDoc imageType = getImageType(image);
242                 sb.append("<manifest:file-entry manifest:media-type=\"");
243                 sb.append(imageType);
244                 sb.append("\" manifest:full-path=\"");
245                 sb.append(imageDirectory);
246                 sb.append("/");
247                 sb.append(image.getName());
248                 sb.append("\"/>");
249                 sb.append(ROW_BREAK);
250             }
251         }
252     }
253         
254     protected String JavaDoc getImageType(ImageDrawComponent img)
255     {
256         String JavaDoc fileExtension = getFileExtension(img.getName());
257         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
258         sb.append("image/");
259         sb.append(fileExtension);
260         return sb.toString();
261     }
262     
263     protected String JavaDoc getFileExtension(String JavaDoc fileName)
264     {
265     StringBuffer JavaDoc sb = new StringBuffer JavaDoc(fileName);
266     int index = sb.lastIndexOf(".");
267     if (index == -1) {
268         return null;
269     }
270     return sb.substring(index+1);
271     }
272     
273     public boolean containsImages(MultiLayerDrawComponent mldc)
274     {
275         Collection JavaDoc imageDCs = mldc.getDrawComponents(ImageDrawComponentImpl.class);
276         if (imageDCs != null) {
277             return true;
278         }
279         return false;
280     }
281     
282     protected void closeHeader(StringBuffer JavaDoc sb)
283     {
284         sb.append("</manifest:manifest>");
285     }
286     
287     public StringBuffer JavaDoc writeManifest(MultiLayerDrawComponent mldc)
288     {
289         stringBuffer = new StringBuffer JavaDoc();
290         writeHeader(stringBuffer);
291 // writeContentVersion(stringBuffer);
292
writeDTD(stringBuffer);
293         writeNS(stringBuffer);
294         writeContentType(stringBuffer);
295         writeContent(stringBuffer);
296         writeImages(stringBuffer, mldc);
297         closeHeader(stringBuffer);
298         return stringBuffer;
299     }
300 }
301
Popular Tags