KickJava   Java API By Example, From Geeks To Geeks.

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


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.NodeData;
17 import info.magnolia.cms.security.AccessDeniedException;
18
19 import java.io.ByteArrayInputStream JavaDoc;
20 import java.io.IOException JavaDoc;
21 import java.io.InputStream JavaDoc;
22 import java.text.ParseException JavaDoc;
23 import java.text.SimpleDateFormat JavaDoc;
24 import java.util.Calendar JavaDoc;
25 import java.util.Date JavaDoc;
26 import java.util.GregorianCalendar JavaDoc;
27 import java.util.Hashtable JavaDoc;
28 import java.util.Iterator JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.jcr.PathNotFoundException;
32 import javax.jcr.PropertyType;
33 import javax.jcr.RepositoryException;
34 import javax.jcr.Value;
35
36 import org.apache.commons.codec.binary.Base64;
37 import org.apache.commons.io.IOUtils;
38 import org.apache.commons.lang.BooleanUtils;
39 import org.apache.commons.lang.StringUtils;
40 import org.jdom.Document;
41 import org.jdom.Element;
42 import org.jdom.input.SAXBuilder;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46
47 /**
48  * Date: May 24, 2005 Time: 4:59:37 PM
49  * @author Sameer Charles $Id :$
50  */

51 public class XmlImport implements ImportHandler {
52
53     /**
54      * Logger.
55      */

56     private static Logger log = LoggerFactory.getLogger(XmlExport.class);
57
58     /**
59      * XML structure constants
60      */

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

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

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

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

69     /**
70      * params
71      */

72     public static final String JavaDoc DATE_FORMAT = "dateFormat"; //$NON-NLS-1$
73

74     /**
75      * default
76      */

77     public static final String JavaDoc DEFAULT_DATE_FORMAT = "EEE MMM dd hh:mm:ss zzzz yyyy"; //$NON-NLS-1$
78

79     /**
80      * fields
81      */

82     private boolean binaryAsLink = true;
83
84     private Map JavaDoc params = new Hashtable JavaDoc();
85
86     public void setBinaryAsLink(boolean binaryAsLink) {
87         this.binaryAsLink = binaryAsLink;
88     }
89
90     public boolean getBinaryAsLink() {
91         return this.binaryAsLink;
92     }
93
94     public void importContent(Content target, InputStream JavaDoc inStream) throws RepositoryException, IOException JavaDoc {
95         try {
96             SAXBuilder builder = new SAXBuilder();
97             Document document = builder.build(inStream);
98             this.importContent(target, document.getRootElement());
99             target.save();
100         }
101         catch (Exception JavaDoc e) {
102             log.error("failed to import"); //$NON-NLS-1$
103
log.error(e.getMessage(), e);
104         }
105         finally {
106             IOUtils.closeQuietly(inStream);
107         }
108     }
109
110     public void setParameter(String JavaDoc key, Object JavaDoc value) {
111         this.params.put(key, value);
112     }
113
114     public Object JavaDoc getParameter(String JavaDoc key) {
115         return this.params.get(key);
116     }
117
118     private void importContent(Content content, Element element) {
119         if (element.getName().equalsIgnoreCase(E_CONTENT)) {
120             content = this.addContent(content, element);
121         }
122         else if (element.getName().equalsIgnoreCase(E_PROPERTY)) {
123             this.addProperty(content, element);
124         }
125         else {
126             if (log.isDebugEnabled()) {
127                 log.debug("Undefined type - " + element.getName()); //$NON-NLS-1$
128
}
129         }
130         // if not allowed or some repository exception occured
131
if (content == null) {
132             return;
133         }
134         Iterator JavaDoc children = element.getChildren().iterator();
135         while (children.hasNext()) {
136             Element subElement = (Element) children.next();
137             importContent(content, subElement);
138         }
139     }
140
141     private Content addContent(Content content, Element element) {
142         try {
143             return content.getContent(element.getAttributeValue(A_NAME));
144         }
145         catch (PathNotFoundException e) {
146             try {
147                 Content newContent = content.createContent(element.getAttributeValue(A_NAME), element
148                     .getAttributeValue(A_TYPE));
149                 if (log.isDebugEnabled()) {
150                     log.debug("Adding content - " + newContent.getHandle()); //$NON-NLS-1$
151
}
152                 return newContent;
153             }
154             catch (AccessDeniedException ade) {
155                 log.error(ade.getMessage());
156             }
157             catch (RepositoryException re) {
158                 log.error(re.getMessage(), re);
159             }
160         }
161         catch (AccessDeniedException ade) {
162             log.error(ade.getMessage());
163         }
164         catch (Exception JavaDoc e) {
165             log.error(e.getMessage(), e);
166         }
167         return null;
168     }
169
170     /**
171      * set property value or create a new property if not exist. PropertyType.REFERENCE are handled differently, since
172      * there is no difference in storage of String and Reference type it must be set while creating a property.
173      */

174     private void addProperty(Content content, Element element) {
175         NodeData nodeData = content.getNodeData(element.getAttributeValue(A_NAME));
176         int type = PropertyType.valueFromName(element.getAttributeValue(A_TYPE));
177         String JavaDoc value = element.getText();
178         if (!nodeData.isExist()) {
179             try {
180                 if (type == PropertyType.REFERENCE) {
181                     Value refValue = content.getJCRNode().getSession().getValueFactory().createValue(value, type);
182                     nodeData = content.createNodeData(element.getAttributeValue(A_NAME), refValue);
183                 }
184                 nodeData = content.createNodeData(element.getAttributeValue(A_NAME));
185                 if (log.isDebugEnabled()) {
186                     log.debug("Adding property - " + nodeData.getHandle()); //$NON-NLS-1$
187
}
188             }
189             catch (AccessDeniedException ade) {
190                 log.error(ade.getMessage());
191             }
192             catch (Exception JavaDoc e) {
193                 log.error(e.getMessage(), e);
194             }
195         }
196         // set value and type
197
try {
198             this.setPropertyValue(nodeData, type, value);
199         }
200         catch (AccessDeniedException ade) {
201             log.error(ade.getMessage());
202         }
203         catch (RepositoryException re) {
204             log.error(re.getMessage(), re);
205         }
206     }
207
208     private void setPropertyValue(NodeData nodeData, int type, String JavaDoc value) throws AccessDeniedException,
209         RepositoryException {
210         switch (type) {
211             case PropertyType.STRING:
212                 nodeData.setValue(value);
213                 break;
214             case PropertyType.LONG:
215                 nodeData.setValue((new Long JavaDoc(value)).longValue());
216                 break;
217             case PropertyType.DOUBLE:
218                 nodeData.setValue((new Double JavaDoc(value)).doubleValue());
219                 break;
220             case PropertyType.DATE:
221                 // todo
222
String JavaDoc dateFormat = (String JavaDoc) this.getParameter(DATE_FORMAT);
223                 if (StringUtils.isEmpty(dateFormat)) {
224                     dateFormat = DEFAULT_DATE_FORMAT;
225                 }
226                 SimpleDateFormat JavaDoc simpleFormat = new SimpleDateFormat JavaDoc(dateFormat);
227                 try {
228                     Date JavaDoc date = simpleFormat.parse(value);
229                     Calendar JavaDoc cal = new GregorianCalendar JavaDoc();
230                     cal.setTime(date);
231                     nodeData.setValue(cal);
232                 }
233                 catch (ParseException JavaDoc e) {
234                     log.error("Failed to parse date with the given format " + dateFormat, e); //$NON-NLS-1$
235
}
236                 break;
237             case PropertyType.BOOLEAN:
238                 nodeData.setValue(BooleanUtils.toBoolean(value));
239                 break;
240             case PropertyType.BINARY:
241                 nodeData.setValue(new ByteArrayInputStream JavaDoc(Base64.decodeBase64(value.getBytes())));
242                 break;
243             case PropertyType.REFERENCE:
244                 /**
245                  * this property must exist before of the same type as REFERENCE
246                  */

247                 nodeData.setValue(value);
248         }
249     }
250
251 }
252
Popular Tags