001/*
002 * (C) Copyright 2012 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 *     Thierry Delprat
018 */
019package org.nuxeo.apidoc.documentation;
020
021import java.util.Arrays;
022import java.util.HashMap;
023import java.util.List;
024import java.util.Map;
025
026import org.nuxeo.apidoc.api.AbstractDocumentationItem;
027import org.nuxeo.apidoc.api.BaseNuxeoArtifact;
028import org.nuxeo.apidoc.api.DocumentationItem;
029import org.nuxeo.apidoc.introspection.BundleGroupImpl;
030import org.nuxeo.apidoc.introspection.BundleInfoImpl;
031
032import com.cforcoding.jmd.MarkDownParserAndSanitizer;
033
034public class ResourceDocumentationItem extends AbstractDocumentationItem implements DocumentationItem {
035
036    protected String content;
037
038    protected String filename;
039
040    protected BaseNuxeoArtifact target;
041
042    protected String type;
043
044    public ResourceDocumentationItem(String filename, String content, BundleInfoImpl target, String type) {
045        this.content = content;
046        this.filename = filename;
047        this.target = target;
048        this.type = type;
049    }
050
051    public ResourceDocumentationItem(ResourceDocumentationItem other, BundleGroupImpl target) {
052        this.content = other.content;
053        this.filename = other.filename;
054        this.target = target;
055        this.type = other.type;
056    }
057
058    @Override
059    public String getTitle() {
060        return getCleanName() + " " + target.getId();
061    }
062
063    protected String getCleanName() {
064        if (filename == null || filename.toLowerCase().startsWith("readme")) {
065            return "ReadMe";
066        }
067        int idx = filename.indexOf(".");
068        if (idx > 0) {
069            return filename.substring(0, idx);
070        }
071        return filename;
072    }
073
074    @Override
075    public String getContent() {
076        MarkDownParserAndSanitizer parser = new MarkDownParserAndSanitizer();
077        String xHtml = parser.transform(content);
078        return xHtml;
079    }
080
081    @Override
082    public String getType() {
083        return type;
084    }
085
086    @Override
087    public String getRenderingType() {
088        return "html";
089    }
090
091    @Override
092    public List<String> getApplicableVersion() {
093        return Arrays.asList(target.getVersion());
094    }
095
096    @Override
097    public String getTarget() {
098        return target.getId();
099    }
100
101    @Override
102    public String getTargetType() {
103        return target.getArtifactType();
104    }
105
106    @Override
107    public boolean isApproved() {
108        return true;
109    }
110
111    @Override
112    public String getId() {
113        return getTargetType() + "--" + filename;
114    }
115
116    @Override
117    public String getUUID() {
118        return null;
119    }
120
121    @Override
122    public Map<String, String> getAttachments() {
123        return new HashMap<String, String>();
124    }
125
126    @Override
127    public boolean isPlaceHolder() {
128        return true;
129    }
130
131    @Override
132    public String getEditId() {
133        return null;
134    }
135
136    public boolean isReadOnly() {
137        return true;
138    }
139}