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 *     Nuxeo - initial API and implementation
018 *
019 */
020
021package org.nuxeo.ecm.core.api.blobholder;
022
023import java.io.IOException;
024
025import org.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.Blobs;
027import org.nuxeo.ecm.core.api.DocumentModel;
028import org.nuxeo.ecm.core.api.NuxeoException;
029
030/**
031 * {@link BlobHolder} implemention based on a {@link DocumentModel} and a Xpath pointing to a String fields. (Typical
032 * use case is the Note DocType).
033 *
034 * @author tiry
035 */
036public class DocumentStringBlobHolder extends DocumentBlobHolder {
037
038    protected String mt;
039
040    public DocumentStringBlobHolder(DocumentModel doc, String path) {
041        super(doc, path);
042    }
043
044    public DocumentStringBlobHolder(DocumentModel doc, String path, String mime_type) {
045        super(doc, path);
046        this.mt = mime_type;
047    }
048
049    @Override
050    public Blob getBlob() {
051        String string = (String) doc.getProperty(xPath).getValue();
052        if (string == null) {
053            return null;
054        }
055        Blob blob = Blobs.createBlob(string, mt);
056        String ext = ".txt";
057        if ("text/html".equals(mt)) {
058            ext = ".html";
059        } else if ("text/xml".equals(mt)) {
060            ext = ".xml";
061        } else if ("text/x-web-markdown".equals(mt)) {
062            ext = ".md";
063        }
064        String title = doc.getTitle();
065        if (!title.endsWith(ext)) {
066            title = title.concat(ext);
067        }
068        blob.setFilename(title);
069        return blob;
070    }
071
072    @Override
073    public void setBlob(Blob blob) {
074        if (blob == null) {
075            doc.getProperty(xPath).setValue(null);
076            mt = null;
077        } else {
078            String string;
079            try {
080                string = blob.getString();
081            } catch (IOException e) {
082                throw new NuxeoException(e);
083            }
084            // strip '\0 chars from text
085            if (string.indexOf('\0') >= 0) {
086                string = string.replace("\0", "");
087            }
088            doc.getProperty(xPath).setValue(string);
089            mt = blob.getMimeType();
090        }
091    }
092
093}