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