001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Florent Guillaume
011 */
012
013package org.nuxeo.ecm.core.blob.binary;
014
015import java.io.File;
016import java.io.FileOutputStream;
017import java.io.IOException;
018
019import javax.xml.parsers.DocumentBuilder;
020import javax.xml.parsers.DocumentBuilderFactory;
021import javax.xml.parsers.ParserConfigurationException;
022import javax.xml.transform.OutputKeys;
023import javax.xml.transform.Result;
024import javax.xml.transform.Transformer;
025import javax.xml.transform.TransformerException;
026import javax.xml.transform.TransformerFactory;
027import javax.xml.transform.dom.DOMSource;
028import javax.xml.transform.stream.StreamResult;
029
030import org.nuxeo.common.xmap.annotation.XNode;
031import org.nuxeo.common.xmap.annotation.XObject;
032import org.w3c.dom.Document;
033import org.w3c.dom.Element;
034
035/**
036 * Descriptor for the configuration of an on-disk binaries storage.
037 */
038@XObject(value = BinaryManagerRootDescriptor.BINARY_STORE)
039public class BinaryManagerRootDescriptor {
040
041    public static final String BINARY_STORE = "binary-store";
042
043    public static final String DIGEST = "digest";
044
045    public static final String DEPTH = "depth";
046
047    /**
048     * The digest, for instance {@code MD5} or {@code SHA-256}.
049     */
050    @XNode(DIGEST)
051    public String digest;
052
053    /**
054     * The number of intermediate sub-directories to use.
055     */
056    @XNode(DEPTH)
057    public int depth;
058
059    /**
060     * Writes the descriptor to an XML file.
061     *
062     * @param out the output file to use
063     * @throws IOException
064     */
065    public void write(File out) throws IOException {
066        DocumentBuilder parser;
067        try {
068            parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
069        } catch (ParserConfigurationException e) {
070            throw (IOException) new IOException().initCause(e);
071        }
072        Document doc = parser.newDocument();
073
074        Element root = doc.createElement(BINARY_STORE);
075        doc.appendChild(root);
076        root.appendChild(doc.createElement(DIGEST)).appendChild(doc.createTextNode(digest));
077        root.appendChild(doc.createElement(DEPTH)).appendChild(doc.createTextNode(String.valueOf(depth)));
078
079        try {
080            Transformer trans = TransformerFactory.newInstance().newTransformer();
081            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
082            trans.setOutputProperty(OutputKeys.INDENT, "yes");
083            // don't use StreamResult(out) as it fails on paths with spaces
084            Result outputTarget = new StreamResult(new FileOutputStream(out));
085            trans.transform(new DOMSource(doc), outputTarget);
086        } catch (TransformerException e) {
087            throw (IOException) new IOException().initCause(e);
088        }
089    }
090
091}