KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > core > ie > XmlExport


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.core.ie;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.ItemType;
17 import info.magnolia.cms.core.NodeData;
18
19 import java.io.IOException JavaDoc;
20 import java.io.InputStream JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Hashtable JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.jcr.Property;
28 import javax.jcr.PropertyType;
29 import javax.jcr.RepositoryException;
30
31 import org.apache.commons.codec.binary.Base64;
32 import org.apache.commons.io.IOUtils;
33 import org.apache.commons.lang.StringUtils;
34 import org.jdom.Document;
35 import org.jdom.Element;
36 import org.jdom.output.Format;
37 import org.jdom.output.Format.TextMode;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41
42 /**
43  * This utility class provides static methods for turning a content [node] into an XML document.
44  * @author Mettraux John (john.mettraux >at< openwfe.org)
45  * @version 0.1 $Id :$
46  */

47 public class XmlExport implements ExportHandler {
48
49     /**
50      * Logger.
51      */

52     private static Logger log = LoggerFactory.getLogger(XmlExport.class);
53
54     /**
55      * XML structure constants
56      */

57     private static final String JavaDoc E_CONTENT = "content"; //$NON-NLS-1$
58

59     private static final String JavaDoc E_PROPERTY = "property"; //$NON-NLS-1$
60

61     private static final String JavaDoc A_NAME = "name"; //$NON-NLS-1$
62

63     private static final String JavaDoc A_TYPE = "type"; //$NON-NLS-1$
64

65     /**
66      * default properties
67      */

68     public static final String JavaDoc DEFAULT_ENCODING = "UTF-8"; //$NON-NLS-1$
69

70     /**
71      * basic parameters
72      */

73     public static final String JavaDoc ENCODING = "encoding"; //$NON-NLS-1$
74

75     /**
76      * fields
77      */

78     private boolean binaryAsLink = true;
79
80     private Map JavaDoc params = new Hashtable JavaDoc();
81
82     //
83
// CONSTRUCTORS
84

85     /**
86      * Builds an XmlExporter, if the parameter 'embedBinaryContent' is set to true, binary content will be integrated in
87      * the generated XML documents as base64.
88      */

89     public XmlExport() {
90     }
91
92     //
93
// PUBLIC METHODS
94

95     public void setBinaryAsLink(boolean binaryAsLink) {
96         this.binaryAsLink = binaryAsLink;
97     }
98
99     public boolean getBinaryAsLink() {
100         return this.binaryAsLink;
101     }
102
103     /**
104      * Turns the given Content instance into a JDOM XML document.
105      */

106     public Object JavaDoc exportContent(final Content content) {
107         return new Document(domExport(content));
108     }
109
110     public void exportContent(final Content content, OutputStream JavaDoc outStream) throws RepositoryException, IOException JavaDoc {
111         // check if the encoding is set by config parameters
112
String JavaDoc encoding = ((String JavaDoc) this.getParameter(ENCODING));
113         if (StringUtils.isEmpty(encoding)) {
114             encoding = DEFAULT_ENCODING;
115         }
116         this.getXMLOutputter(encoding).output(((Document) this.exportContent(content)), outStream);
117     }
118
119     public void setParameter(String JavaDoc key, Object JavaDoc value) {
120         this.params.put(key, value);
121     }
122
123     public Object JavaDoc getParameter(String JavaDoc key) {
124         return this.params.get(key);
125     }
126
127     private Element domExport(final Content content) {
128
129         Element elt = new Element(E_CONTENT);
130         elt.setAttribute(A_NAME, content.getName());
131         try {
132             elt.setAttribute(A_TYPE, content.getNodeTypeName());
133         }
134         catch (RepositoryException re) {
135             log.error(re.getMessage(), re);
136         }
137
138         // collect all nodes
139
export(elt, content.getChildren(ItemType.NT_BASE));
140
141         // node data
142
exportNodeData(elt, content.getNodeDataCollection());
143
144         return elt;
145     }
146
147     private void exportNodeData(Element elt, Collection JavaDoc nodeData) {
148
149         Iterator JavaDoc it = nodeData.iterator();
150         while (it.hasNext()) {
151             NodeData nd = (NodeData) it.next();
152
153             export(elt, nd.getJCRProperty());
154         }
155     }
156
157     private void export(Element elt, Collection JavaDoc contentChildren) {
158
159         Iterator JavaDoc it = contentChildren.iterator();
160         while (it.hasNext()) {
161             Content c = (Content) it.next();
162             elt.addContent(domExport(c));
163         }
164     }
165
166     private void export(Element elt, Property property) {
167
168         Element pElt = new Element(E_PROPERTY);
169
170         try {
171
172             pElt.setAttribute(A_NAME, property.getName());
173             pElt.setAttribute(A_TYPE, PropertyType.nameFromValue(property.getType()));
174             exportValue(pElt, property);
175
176         }
177         catch (final Throwable JavaDoc t) {
178             log.warn("export() skipped a property because of " + t); //$NON-NLS-1$
179
}
180
181         elt.addContent(pElt);
182     }
183
184     private void exportValue(final org.jdom.Element pElt, final Property property) {
185
186         String JavaDoc sContent;
187
188         try {
189             if (property.getType() == PropertyType.BINARY) {
190
191                 if (this.binaryAsLink) {
192                     sContent = property.getPath();
193                 }
194                 else {
195                     StringBuffer JavaDoc stringBuffer = new StringBuffer JavaDoc();
196                     try {
197                         InputStream JavaDoc is = property.getStream();
198                         byte[] buffer = new byte[8192];
199
200                         while ((is.read(buffer)) > 0) {
201                             stringBuffer.append(new String JavaDoc(buffer));
202                         }
203                         IOUtils.closeQuietly(is);
204                     }
205                     catch (Exception JavaDoc e) {
206                         log.error("Failed to read input stream", e);
207                     }
208
209                     sContent = new String JavaDoc(Base64.encodeBase64(stringBuffer.toString().getBytes()));
210                 }
211             }
212             else if (property.getType() == PropertyType.DATE) {
213                 sContent = property.getDate().getTime().toString();
214             }
215             else {
216                 sContent = property.getString();
217             }
218         }
219         catch (final Throwable JavaDoc t) {
220
221             log.warn("exportValue() failure", t); //$NON-NLS-1$
222

223             sContent = "exportValue() failure " + t.toString(); //$NON-NLS-1$
224
}
225         pElt.addContent(new org.jdom.Text(sContent));
226     }
227
228     /**
229      * A convenience method for setting up a pretty print XMLOutputter with a given encoding.
230      */

231     private org.jdom.output.XMLOutputter getXMLOutputter(String JavaDoc encoding) {
232         Format format = Format.getPrettyFormat();
233         format.setEncoding(encoding);
234         format.setTextMode(TextMode.PRESERVE);
235         return new org.jdom.output.XMLOutputter(format);
236     }
237
238 }
239
Popular Tags