/** * @see org.opencms.search.extractors.A_CmsTextExtractor#extractText(java.io.InputStream, java.lang.String) */ @Override public I_CmsExtractionResult extractText(InputStream in, String encoding) throws Exception { ZipInputStream zin = new ZipInputStream(in); ZipEntry ze; boolean FOUND_CONTENT = false; String result = ""; while (!FOUND_CONTENT) { ze = zin.getNextEntry(); FOUND_CONTENT = ze.getName().equalsIgnoreCase("content.xml"); if (FOUND_CONTENT) { result = readContent(zin); try { zin.close(); } catch (Exception e) { e.printStackTrace(System.err); } } } result = removeControlChars(result); return new CmsExtractionResult(result); }
/** * Returns the extraction result in the requested file in the disk cache, or <code>null</code> if the * file is not found in the cache, or is found but out-dated.<p> * * @param rfsName the file RFS name to look up in the cache * * @return the extraction result stored in the requested file in the RFS disk cache, or <code>null</code> */ public CmsExtractionResult getCacheObject(String rfsName) { try { File f = new File(rfsName); if (f.exists()) { long age = f.lastModified(); if ((System.currentTimeMillis() - age) > 3600000) { // file has not been touched for 1 hour, touch the file with the current date f.setLastModified(System.currentTimeMillis()); } byte[] byteContent = CmsFileUtil.readFile(f); return CmsExtractionResult.fromBytes(byteContent); } } catch (IOException e) { // unable to read content } // this code can be reached only in case of an error return null; }
/** * @see org.opencms.search.extractors.I_CmsExtractionResult#merge(java.util.List) */ public I_CmsExtractionResult merge(List<I_CmsExtractionResult> extractionResults) { //prepare copy Map<Locale, LinkedHashMap<String, String>> contentItems = new HashMap<Locale, LinkedHashMap<String, String>>( m_locales.size()); for (Locale locale : m_locales) { LinkedHashMap<String, String> originalLocalValues = m_contentItems.get(locale); LinkedHashMap<String, String> localeValues = new LinkedHashMap<String, String>(originalLocalValues); contentItems.put(locale, localeValues); } HashMap<String, String> fieldMappings = new HashMap<String, String>(m_fieldMappings.size()); for (String fieldMapping : m_fieldMappings.keySet()) { fieldMappings.put(fieldMapping, m_fieldMappings.get(fieldMapping)); } //merge content from the other extraction results for (Locale locale : m_locales) { Map<String, String> localeValues = contentItems.get(locale); for (I_CmsExtractionResult result : extractionResults) { if (result.getLocales().contains(locale)) { Map<String, String> resultLocaleValues = result.getContentItems(locale); for (String item : Arrays.asList(ITEMS_TO_MERGE)) { localeValues = mergeItem(item, localeValues, resultLocaleValues); } } } } return new CmsExtractionResult(m_defaultLocale, contentItems, fieldMappings); }
/** Creates a new multilingual extraction result. * @param defaultLocale the default (best fitting) locale of the result. * @param multilingualContentItems the content items for the different locales * @param fieldMappings special mappings to search fields with values extracted from the content */ public CmsExtractionResult( Locale defaultLocale, Map<Locale, LinkedHashMap<String, String>> multilingualContentItems, Map<String, String> fieldMappings) { m_defaultLocale = defaultLocale; m_contentItems = null != multilingualContentItems ? removeNullEntries(multilingualContentItems) : new HashMap<Locale, LinkedHashMap<String, String>>(1); // set the locales m_locales = new HashSet<Locale>(); for (Locale locale : m_contentItems.keySet()) { if (null != locale) { m_locales.add(locale); } } // ensure that a version for the default locale is present just to prevent null-checks if (null == m_contentItems.get(m_defaultLocale)) { m_contentItems.put(m_defaultLocale, new LinkedHashMap<String, String>()); } m_fieldMappings = null != fieldMappings ? fieldMappings : new HashMap<String, String>(); }
/** * @see org.opencms.search.extractors.A_CmsTextExtractor#extractText(java.io.InputStream, java.lang.String) */ @Override public I_CmsExtractionResult extractText(InputStream in, String encoding) throws Exception { ZipInputStream zin = new ZipInputStream(in); ZipEntry ze; boolean FOUND_CONTENT = false; String result = ""; while (!FOUND_CONTENT) { ze = zin.getNextEntry(); FOUND_CONTENT = ze.getName().equalsIgnoreCase("content.xml"); if (FOUND_CONTENT) { result = readContent(zin); try { zin.close(); } catch (Exception e) { e.printStackTrace(System.err); } } } result = removeControlChars(result); return new CmsExtractionResult(result); }
/** * Returns the extraction result in the requested file in the disk cache, or <code>null</code> if the * file is not found in the cache, or is found but out-dated.<p> * * @param rfsName the file RFS name to look up in the cache * * @return the extraction result stored in the requested file in the RFS disk cache, or <code>null</code> */ public CmsExtractionResult getCacheObject(String rfsName) { try { File f = new File(rfsName); if (f.exists()) { long age = f.lastModified(); if ((System.currentTimeMillis() - age) > 3600000) { // file has not been touched for 1 hour, touch the file with the current date f.setLastModified(System.currentTimeMillis()); } byte[] byteContent = CmsFileUtil.readFile(f); return CmsExtractionResult.fromBytes(byteContent); } } catch (IOException e) { // unable to read content } // this code can be reached only in case of an error return null; }
/** * @see org.opencms.search.extractors.I_CmsTextExtractor#extractText(byte[], java.lang.String) */ @Override public I_CmsExtractionResult extractText(byte[] content, String encoding) throws Exception { String result = ""; try { // RTF always uses ASCII, so we don't need to care about the encoding String input = new String(content); // workaround to remove RTF keywords that cause a NPE in Java 1.4 // this is a known bug in Java 1.4 that was fixed in 1.5 // please see http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5042109 for the official bug report input = TS_REMOVE_PATTERN.matcher(input).replaceAll(""); // use build in RTF parser from Swing API RTFEditorKit rtfEditor = new RTFEditorKit(); Document doc = rtfEditor.createDefaultDocument(); rtfEditor.read(new StringReader(input), doc, 0); result = doc.getText(0, doc.getLength()); result = removeControlChars(result); } catch (Exception e) { if (LOG.isErrorEnabled()) { LOG.error(Messages.get().container(Messages.LOG_EXTRACT_TEXT_ERROR_0), e); } } return new CmsExtractionResult(result); }
content = CmsExtractionResult.fromBytes(oldDoc.getContentBlob());
/** * Just returns an empty extraction result since the content can't be extracted form a generic resource.<p> * * @see org.opencms.search.documents.I_CmsSearchExtractor#extractContent(CmsObject, CmsResource, CmsSearchIndex) */ public I_CmsExtractionResult extractContent(CmsObject cms, CmsResource resource, CmsSearchIndex index) throws CmsIndexException { if (resource == null) { throw new CmsIndexException(Messages.get().container(Messages.ERR_NO_RAW_CONTENT_1, index.getLocale())); } // just return an empty result set return new CmsExtractionResult(""); }
content = CmsExtractionResult.fromBytes(oldContent);
/** * Just returns an empty extraction result since the content can't be extracted form a generic resource.<p> * * @see org.opencms.search.documents.I_CmsSearchExtractor#extractContent(CmsObject, CmsResource, CmsSearchIndex) */ public I_CmsExtractionResult extractContent(CmsObject cms, CmsResource resource, CmsSearchIndex index) throws CmsIndexException { if (resource == null) { throw new CmsIndexException(Messages.get().container(Messages.ERR_NO_RAW_CONTENT_1, index.getLocale())); } // just return an empty result set return new CmsExtractionResult(""); }
/** * @see org.opencms.search.extractors.I_CmsTextExtractor#extractText(java.io.InputStream, java.lang.String) */ @Override public I_CmsExtractionResult extractText(InputStream in, String encoding) throws Exception { String result = ""; try { if (CmsStringUtil.isEmpty(encoding)) { encoding = OpenCms.getSystemInfo().getDefaultEncoding(); } result = CmsHtmlExtractor.extractText(in, encoding); result = removeControlChars(result); } catch (Exception e) { if (LOG.isErrorEnabled()) { LOG.error(Messages.get().container(Messages.LOG_EXTRACT_TEXT_ERROR_0), e); } } return new CmsExtractionResult(result); } }
return new CmsExtractionResult(content.toString(), items);
/** * @see org.opencms.search.extractors.I_CmsTextExtractor#extractText(java.io.InputStream, java.lang.String) */ @Override public I_CmsExtractionResult extractText(InputStream in, String encoding) throws Exception { String result = ""; try { if (CmsStringUtil.isEmpty(encoding)) { encoding = OpenCms.getSystemInfo().getDefaultEncoding(); } result = CmsHtmlExtractor.extractText(in, encoding); result = removeControlChars(result); } catch (Exception e) { if (LOG.isErrorEnabled()) { LOG.error(Messages.get().container(Messages.LOG_EXTRACT_TEXT_ERROR_0), e); } } return new CmsExtractionResult(result); } }
return new CmsExtractionResult(content.toString(), items); } catch (Exception e) { throw new CmsIndexException(
return new CmsExtractionResult(content.toString(), items);
/** * Returns the raw text content of a given vfs resource containing plain text data.<p> * * @see org.opencms.search.documents.I_CmsSearchExtractor#extractContent(CmsObject, CmsResource, CmsSearchIndex) */ public I_CmsExtractionResult extractContent(CmsObject cms, CmsResource resource, CmsSearchIndex index) throws CmsException { CmsFile file = readFile(cms, resource); try { CmsProperty encProp = cms.readPropertyObject( resource, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true); String encoding = encProp.getValue(OpenCms.getSystemInfo().getDefaultEncoding()); return new CmsExtractionResult(new String(file.getContents(), encoding)); } catch (Exception e) { throw new CmsIndexException( Messages.get().container(Messages.ERR_TEXT_EXTRACTION_1, resource.getRootPath()), e); } }
return new CmsExtractionResult(content.toString(), items);
/** * Returns the raw text content of a given vfs resource containing plain text data.<p> * * @see org.opencms.search.documents.I_CmsSearchExtractor#extractContent(CmsObject, CmsResource, CmsSearchIndex) */ public I_CmsExtractionResult extractContent(CmsObject cms, CmsResource resource, CmsSearchIndex index) throws CmsException { logContentExtraction(resource, index); CmsFile file = readFile(cms, resource); try { CmsProperty encProp = cms.readPropertyObject( resource, CmsPropertyDefinition.PROPERTY_CONTENT_ENCODING, true); String encoding = encProp.getValue(OpenCms.getSystemInfo().getDefaultEncoding()); return new CmsExtractionResult(new String(file.getContents(), encoding)); } catch (Exception e) { throw new CmsIndexException( Messages.get().container(Messages.ERR_TEXT_EXTRACTION_1, resource.getRootPath()), e); } }
return new CmsExtractionResult(resourceLocale, items, fieldMappings);