001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.core.blob.binary;
021
022import java.io.File;
023import java.io.FileOutputStream;
024import java.io.IOException;
025
026import javax.xml.parsers.DocumentBuilder;
027import javax.xml.parsers.DocumentBuilderFactory;
028import javax.xml.parsers.ParserConfigurationException;
029import javax.xml.transform.OutputKeys;
030import javax.xml.transform.Result;
031import javax.xml.transform.Transformer;
032import javax.xml.transform.TransformerException;
033import javax.xml.transform.TransformerFactory;
034import javax.xml.transform.dom.DOMSource;
035import javax.xml.transform.stream.StreamResult;
036
037import org.nuxeo.common.xmap.annotation.XNode;
038import org.nuxeo.common.xmap.annotation.XObject;
039import org.w3c.dom.Document;
040import org.w3c.dom.Element;
041
042/**
043 * Descriptor for the configuration of an on-disk binaries storage.
044 */
045@XObject(value = BinaryManagerRootDescriptor.BINARY_STORE)
046public class BinaryManagerRootDescriptor {
047
048    public static final String BINARY_STORE = "binary-store";
049
050    public static final String DIGEST = "digest";
051
052    public static final String DEPTH = "depth";
053
054    /**
055     * The digest, for instance {@code MD5} or {@code SHA-256}.
056     */
057    @XNode(DIGEST)
058    public String digest;
059
060    /**
061     * The number of intermediate sub-directories to use.
062     */
063    @XNode(DEPTH)
064    public int depth;
065
066    /**
067     * Writes the descriptor to an XML file.
068     *
069     * @param out the output file to use
070     * @throws IOException
071     */
072    public void write(File out) throws IOException {
073        DocumentBuilder parser;
074        try {
075            parser = DocumentBuilderFactory.newInstance().newDocumentBuilder();
076        } catch (ParserConfigurationException e) {
077            throw (IOException) new IOException().initCause(e);
078        }
079        Document doc = parser.newDocument();
080
081        Element root = doc.createElement(BINARY_STORE);
082        doc.appendChild(root);
083        root.appendChild(doc.createElement(DIGEST)).appendChild(doc.createTextNode(digest));
084        root.appendChild(doc.createElement(DEPTH)).appendChild(doc.createTextNode(String.valueOf(depth)));
085
086        try {
087            Transformer trans = TransformerFactory.newInstance().newTransformer();
088            trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
089            trans.setOutputProperty(OutputKeys.INDENT, "yes");
090            // don't use StreamResult(out) as it fails on paths with spaces
091            Result outputTarget = new StreamResult(new FileOutputStream(out));
092            trans.transform(new DOMSource(doc), outputTarget);
093        } catch (TransformerException e) {
094            throw (IOException) new IOException().initCause(e);
095        }
096    }
097
098}