1 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 ; 20 import java.io.InputStream ; 21 import java.io.OutputStream ; 22 import java.util.Collection ; 23 import java.util.Hashtable ; 24 import java.util.Iterator ; 25 import java.util.Map ; 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 47 public class XmlExport implements ExportHandler { 48 49 52 private static Logger log = LoggerFactory.getLogger(XmlExport.class); 53 54 57 private static final String E_CONTENT = "content"; 59 private static final String E_PROPERTY = "property"; 61 private static final String A_NAME = "name"; 63 private static final String A_TYPE = "type"; 65 68 public static final String DEFAULT_ENCODING = "UTF-8"; 70 73 public static final String ENCODING = "encoding"; 75 78 private boolean binaryAsLink = true; 79 80 private Map params = new Hashtable (); 81 82 85 89 public XmlExport() { 90 } 91 92 95 public void setBinaryAsLink(boolean binaryAsLink) { 96 this.binaryAsLink = binaryAsLink; 97 } 98 99 public boolean getBinaryAsLink() { 100 return this.binaryAsLink; 101 } 102 103 106 public Object exportContent(final Content content) { 107 return new Document(domExport(content)); 108 } 109 110 public void exportContent(final Content content, OutputStream outStream) throws RepositoryException, IOException { 111 String encoding = ((String ) 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 key, Object value) { 120 this.params.put(key, value); 121 } 122 123 public Object getParameter(String 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 export(elt, content.getChildren(ItemType.NT_BASE)); 140 141 exportNodeData(elt, content.getNodeDataCollection()); 143 144 return elt; 145 } 146 147 private void exportNodeData(Element elt, Collection nodeData) { 148 149 Iterator 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 contentChildren) { 158 159 Iterator 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 t) { 178 log.warn("export() skipped a property because of " + t); } 180 181 elt.addContent(pElt); 182 } 183 184 private void exportValue(final org.jdom.Element pElt, final Property property) { 185 186 String sContent; 187 188 try { 189 if (property.getType() == PropertyType.BINARY) { 190 191 if (this.binaryAsLink) { 192 sContent = property.getPath(); 193 } 194 else { 195 StringBuffer stringBuffer = new StringBuffer (); 196 try { 197 InputStream is = property.getStream(); 198 byte[] buffer = new byte[8192]; 199 200 while ((is.read(buffer)) > 0) { 201 stringBuffer.append(new String (buffer)); 202 } 203 IOUtils.closeQuietly(is); 204 } 205 catch (Exception e) { 206 log.error("Failed to read input stream", e); 207 } 208 209 sContent = new String (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 t) { 220 221 log.warn("exportValue() failure", t); 223 sContent = "exportValue() failure " + t.toString(); } 225 pElt.addContent(new org.jdom.Text(sContent)); 226 } 227 228 231 private org.jdom.output.XMLOutputter getXMLOutputter(String 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 |