001/*
002 * (C) Copyright 2006-2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Bogdan Stefanescu
016 *     Thierry Delprat
017 */
018package org.nuxeo.apidoc.introspection;
019
020import java.io.File;
021import java.io.FileInputStream;
022import java.io.IOException;
023import java.net.URL;
024import java.util.ArrayList;
025import java.util.Collection;
026import java.util.HashMap;
027import java.util.List;
028import java.util.Map;
029import java.util.zip.ZipEntry;
030import java.util.zip.ZipFile;
031
032import org.apache.commons.logging.Log;
033import org.apache.commons.logging.LogFactory;
034import org.nuxeo.apidoc.api.BaseNuxeoArtifact;
035import org.nuxeo.apidoc.api.ComponentInfo;
036import org.nuxeo.apidoc.api.ExtensionInfo;
037import org.nuxeo.apidoc.api.ExtensionPointInfo;
038import org.nuxeo.apidoc.api.ServiceInfo;
039import org.nuxeo.apidoc.documentation.DocumentationHelper;
040import org.nuxeo.common.utils.FileUtils;
041import org.nuxeo.common.utils.Path;
042
043public class ComponentInfoImpl extends BaseNuxeoArtifact implements ComponentInfo {
044
045    protected final BundleInfoImpl bundle;
046
047    protected final String name;
048
049    protected final Map<String, ExtensionPointInfo> extensionPoints;
050
051    protected final Collection<ExtensionInfo> extensions;
052
053    protected final List<String> serviceNames = new ArrayList<String>();
054
055    protected final List<ServiceInfo> services = new ArrayList<ServiceInfo>();
056
057    protected URL xmlFileUrl;
058
059    protected String componentClass;
060
061    protected String documentation; // TODO
062
063    protected static final Log log = LogFactory.getLog(ComponentInfoImpl.class);
064
065    public ComponentInfoImpl(BundleInfoImpl bundleInfo, String name) {
066        this.bundle = bundleInfo;
067        this.name = name;
068        extensionPoints = new HashMap<String, ExtensionPointInfo>();
069        extensions = new ArrayList<ExtensionInfo>();
070    }
071
072    @Override
073    public String getName() {
074        return name;
075    }
076
077    @Override
078    public BundleInfoImpl getBundle() {
079        return bundle;
080    }
081
082    @Override
083    public Collection<ExtensionPointInfo> getExtensionPoints() {
084        return extensionPoints.values();
085    }
086
087    @Override
088    public Collection<ExtensionInfo> getExtensions() {
089        return extensions;
090    }
091
092    public void addExtensionPoint(ExtensionPointInfoImpl xp) {
093        extensionPoints.put(xp.getId(), xp);
094    }
095
096    @Override
097    public ExtensionPointInfo getExtensionPoint(String name) {
098        return extensionPoints.get(name);
099    }
100
101    public void addExtension(ExtensionInfoImpl xt) {
102        extensions.add(xt);
103    }
104
105    @Override
106    public String getDocumentation() {
107        return documentation;
108    }
109
110    @Override
111    public String getDocumentationHtml() {
112        return DocumentationHelper.getHtml(getDocumentation());
113    }
114
115    public void setDocumentation(String documentation) {
116        this.documentation = documentation;
117    }
118
119    public void addService(String serviceName) {
120        serviceNames.add(serviceName);
121        ServiceInfo si = new ServiceInfoImpl(serviceName, this);
122        services.add(si);
123    }
124
125    @Override
126    public List<String> getServiceNames() {
127        return serviceNames;
128    }
129
130    @Override
131    public String getComponentClass() {
132        return componentClass;
133    }
134
135    public void setComponentClass(String componentClass) {
136        this.componentClass = componentClass;
137    }
138
139    @Override
140    public boolean isXmlPureComponent() {
141        return componentClass == null;
142    }
143
144    @Override
145    public URL getXmlFileUrl() {
146        return xmlFileUrl;
147    }
148
149    public void setXmlFileUrl(URL xmlFileUrl) {
150        this.xmlFileUrl = xmlFileUrl;
151    }
152
153    @Override
154    public String getXmlFileName() {
155        if (xmlFileUrl == null) {
156            return "";
157        }
158        String path = xmlFileUrl.getPath();
159        String[] parts = path.split("!");
160        if (parts.length == 2) {
161            return parts[1];
162        } else {
163            return path;
164        }
165    }
166
167    @Override
168    public String getXmlFileContent() {
169        if (xmlFileUrl == null) {
170            return "";
171        }
172        String path = xmlFileUrl.getPath();
173        String[] parts = path.split("!");
174
175        File jar = new File(parts[0].replace("file:", ""));
176        if (!jar.exists()) {
177            return "Unable to locate Bundle :" + parts[0];
178        }
179
180        try {
181            String xml;
182            if (jar.getAbsolutePath().endsWith(".xml")) {
183                xml = FileUtils.read(new FileInputStream(jar));
184            } else if (jar.isDirectory()) {
185                File file = new File(new Path(jar.getAbsolutePath()).append(parts[1]).toString());
186                if (!file.exists()) {
187                    return "Unable to locate file :" + file.getAbsolutePath();
188                }
189                xml = FileUtils.readFile(file);
190            } else {
191                ZipFile jarArchive = new ZipFile(jar);
192                ZipEntry entry = jarArchive.getEntry(parts[1].substring(1));
193                xml = FileUtils.read(jarArchive.getInputStream(entry));
194            }
195            return DocumentationHelper.secureXML(xml);
196        } catch (IOException e) {
197            log.error("Error while getting XML file", e);
198            return "";
199        }
200    }
201
202    @Override
203    public String getId() {
204        return name;
205    }
206
207    @Override
208    public String getVersion() {
209        return bundle.getVersion();
210    }
211
212    @Override
213    public String getArtifactType() {
214        return TYPE_NAME;
215    }
216
217    @Override
218    public List<ServiceInfo> getServices() {
219        return services;
220    }
221
222    @Override
223    public String getHierarchyPath() {
224        return getBundle().getHierarchyPath() + "/" + getId();
225    }
226
227}