1 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 ; 20 import java.io.IOException ; 21 import java.io.InputStream ; 22 import java.text.ParseException ; 23 import java.text.SimpleDateFormat ; 24 import java.util.Calendar ; 25 import java.util.Date ; 26 import java.util.GregorianCalendar ; 27 import java.util.Hashtable ; 28 import java.util.Iterator ; 29 import java.util.Map ; 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 51 public class XmlImport implements ImportHandler { 52 53 56 private static Logger log = LoggerFactory.getLogger(XmlExport.class); 57 58 61 private static final String E_CONTENT = "content"; 63 private static final String E_PROPERTY = "property"; 65 private static final String A_NAME = "name"; 67 private static final String A_TYPE = "type"; 69 72 public static final String DATE_FORMAT = "dateFormat"; 74 77 public static final String DEFAULT_DATE_FORMAT = "EEE MMM dd hh:mm:ss zzzz yyyy"; 79 82 private boolean binaryAsLink = true; 83 84 private Map params = new Hashtable (); 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 inStream) throws RepositoryException, IOException { 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 e) { 102 log.error("failed to import"); log.error(e.getMessage(), e); 104 } 105 finally { 106 IOUtils.closeQuietly(inStream); 107 } 108 } 109 110 public void setParameter(String key, Object value) { 111 this.params.put(key, value); 112 } 113 114 public Object getParameter(String 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()); } 129 } 130 if (content == null) { 132 return; 133 } 134 Iterator 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()); } 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 e) { 165 log.error(e.getMessage(), e); 166 } 167 return null; 168 } 169 170 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 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()); } 188 } 189 catch (AccessDeniedException ade) { 190 log.error(ade.getMessage()); 191 } 192 catch (Exception e) { 193 log.error(e.getMessage(), e); 194 } 195 } 196 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 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 (value)).longValue()); 216 break; 217 case PropertyType.DOUBLE: 218 nodeData.setValue((new Double (value)).doubleValue()); 219 break; 220 case PropertyType.DATE: 221 String dateFormat = (String ) this.getParameter(DATE_FORMAT); 223 if (StringUtils.isEmpty(dateFormat)) { 224 dateFormat = DEFAULT_DATE_FORMAT; 225 } 226 SimpleDateFormat simpleFormat = new SimpleDateFormat (dateFormat); 227 try { 228 Date date = simpleFormat.parse(value); 229 Calendar cal = new GregorianCalendar (); 230 cal.setTime(date); 231 nodeData.setValue(cal); 232 } 233 catch (ParseException e) { 234 log.error("Failed to parse date with the given format " + dateFormat, e); } 236 break; 237 case PropertyType.BOOLEAN: 238 nodeData.setValue(BooleanUtils.toBoolean(value)); 239 break; 240 case PropertyType.BINARY: 241 nodeData.setValue(new ByteArrayInputStream (Base64.decodeBase64(value.getBytes()))); 242 break; 243 case PropertyType.REFERENCE: 244 247 nodeData.setValue(value); 248 } 249 } 250 251 } 252 | Popular Tags |