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.introspection; 020 021import java.io.IOException; 022import java.io.InputStream; 023import java.util.Enumeration; 024import java.util.HashMap; 025import java.util.Map; 026import java.util.zip.ZipEntry; 027import java.util.zip.ZipFile; 028 029import org.nuxeo.apidoc.documentation.DefaultDocumentationType; 030import org.nuxeo.apidoc.documentation.ResourceDocumentationItem; 031import org.nuxeo.common.utils.FileUtils; 032import org.nuxeo.common.utils.Path; 033 034public class EmbeddedDocExtractor { 035 036 public static final String DOC_PREFIX = "doc/"; 037 038 public static final String PARENT_DOC_PREFIX = "doc-parent/"; 039 040 public static void extractEmbeddedDoc(ZipFile jarFile, BundleInfoImpl bi) throws IOException { 041 042 Enumeration<? extends ZipEntry> entries = jarFile.entries(); 043 044 Map<String, ResourceDocumentationItem> localDocs = new HashMap<String, ResourceDocumentationItem>(); 045 Map<String, ResourceDocumentationItem> parentDocs = new HashMap<String, ResourceDocumentationItem>(); 046 while (entries.hasMoreElements()) { 047 ZipEntry entry = entries.nextElement(); 048 049 if (entry.getName().startsWith(PARENT_DOC_PREFIX) && !entry.isDirectory()) { 050 InputStream is = jarFile.getInputStream(entry); 051 String content = FileUtils.read(is); 052 is.close(); 053 String name = new Path(entry.getName()).lastSegment(); 054 if (name.length() >= 6 && name.substring(0, 6).equalsIgnoreCase("readme")) { 055 056 ResourceDocumentationItem docItem = new ResourceDocumentationItem(name, content, bi, 057 DefaultDocumentationType.DESCRIPTION.toString()); 058 059 parentDocs.put(DefaultDocumentationType.DESCRIPTION.toString(), docItem); 060 } else { 061 ResourceDocumentationItem docItem = new ResourceDocumentationItem(name, content, bi, 062 DefaultDocumentationType.HOW_TO.toString()); 063 parentDocs.put(DefaultDocumentationType.HOW_TO.toString(), docItem); 064 } 065 } 066 if (entry.getName().startsWith(DOC_PREFIX) && !entry.isDirectory()) { 067 InputStream is = jarFile.getInputStream(entry); 068 String content = FileUtils.read(is); 069 is.close(); 070 String name = new Path(entry.getName()).lastSegment(); 071 if (name.length() >= 6 && name.substring(0, 6).equalsIgnoreCase("readme")) { 072 073 ResourceDocumentationItem docItem = new ResourceDocumentationItem(name, content, bi, 074 DefaultDocumentationType.DESCRIPTION.toString()); 075 localDocs.put(DefaultDocumentationType.DESCRIPTION.toString(), docItem); 076 } else { 077 ResourceDocumentationItem docItem = new ResourceDocumentationItem(name, content, bi, 078 DefaultDocumentationType.HOW_TO.toString()); 079 localDocs.put(DefaultDocumentationType.HOW_TO.toString(), docItem); 080 } 081 } 082 } 083 bi.setLiveDoc(localDocs); 084 bi.setParentLiveDoc(parentDocs); 085 } 086}