001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.search.documents; 029 030import org.opencms.file.CmsFile; 031import org.opencms.file.CmsObject; 032import org.opencms.file.CmsResource; 033import org.opencms.main.CmsException; 034import org.opencms.search.CmsIndexException; 035import org.opencms.search.I_CmsSearchIndex; 036import org.opencms.search.extractors.CmsExtractionResult; 037import org.opencms.search.extractors.I_CmsExtractionResult; 038import org.opencms.util.CmsHtmlExtractor; 039import org.opencms.util.CmsStringUtil; 040import org.opencms.xml.page.CmsXmlPage; 041import org.opencms.xml.page.CmsXmlPageFactory; 042 043import java.util.Iterator; 044import java.util.LinkedHashMap; 045import java.util.List; 046import java.util.Locale; 047 048/** 049 * Lucene document factory class to extract index data from a cms resource 050 * of type <code>CmsResourceTypeXmlPage</code>.<p> 051 * 052 * @since 6.0.0 053 */ 054public class CmsDocumentXmlPage extends A_CmsVfsDocument { 055 056 /** 057 * Creates a new instance of this lucene document factory.<p> 058 * 059 * @param name name of the documenttype 060 */ 061 public CmsDocumentXmlPage(String name) { 062 063 super(name); 064 } 065 066 /** 067 * Returns the raw text content of a given vfs resource of type <code>CmsResourceTypeXmlPage</code>.<p> 068 * 069 * @see org.opencms.search.documents.I_CmsSearchExtractor#extractContent(CmsObject, CmsResource, I_CmsSearchIndex) 070 */ 071 public I_CmsExtractionResult extractContent(CmsObject cms, CmsResource resource, I_CmsSearchIndex index) 072 throws CmsException { 073 074 logContentExtraction(resource, index); 075 try { 076 CmsFile file = readFile(cms, resource); 077 CmsXmlPage page = CmsXmlPageFactory.unmarshal(cms, file); 078 Locale locale = index.getLocaleForResource(cms, resource, page.getLocales()); 079 080 List<String> elements = page.getNames(locale); 081 StringBuffer content = new StringBuffer(); 082 LinkedHashMap<String, String> items = new LinkedHashMap<String, String>(); 083 for (Iterator<String> i = elements.iterator(); i.hasNext();) { 084 String elementName = i.next(); 085 String value = page.getStringValue(cms, elementName, locale); 086 String extracted = CmsHtmlExtractor.extractText(value, page.getEncoding()); 087 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(extracted)) { 088 items.put(elementName, extracted); 089 content.append(extracted); 090 content.append('\n'); 091 } 092 } 093 094 return new CmsExtractionResult(content.toString(), items); 095 096 } catch (Exception e) { 097 throw new CmsIndexException( 098 Messages.get().container(Messages.ERR_TEXT_EXTRACTION_1, resource.getRootPath()), 099 e); 100 } 101 } 102 103 /** 104 * @see org.opencms.search.documents.I_CmsDocumentFactory#isLocaleDependend() 105 */ 106 public boolean isLocaleDependend() { 107 108 return true; 109 } 110 111 /** 112 * @see org.opencms.search.documents.I_CmsDocumentFactory#isUsingCache() 113 */ 114 public boolean isUsingCache() { 115 116 return true; 117 } 118}