本文整理汇总了Java中org.apache.tika.parser.Parser类的典型用法代码示例。如果您正苦于以下问题:Java Parser类的具体用法?Java Parser怎么用?Java Parser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Parser类属于org.apache.tika.parser包,在下文中一共展示了Parser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractText
import org.apache.tika.parser.Parser; //导入依赖的package包/类
@Override
public void extractText(String mimeType, InputStream input, StringBuilder outputText, int maxSize)
throws IOException
{
try
{
Metadata meta = new Metadata();
ContentHandler handler = new BodyContentHandler();
Parser parser = new AutoDetectParser(new TikaConfig(getClass().getClassLoader()));
parser.parse(input, handler, meta, new ParseContext());
String content = handler.toString();
if( content.length() > maxSize )
{
content = content.substring(0, maxSize);
}
outputText.append(content);
if( LOGGER.isDebugEnabled() )
{
LOGGER.debug("Word Summary:" + content); //$NON-NLS-1$
}
}
catch( Exception e )
{
throw new RuntimeException(e);
}
}
示例2: convertWordDocumentIntoHtml
import org.apache.tika.parser.Parser; //导入依赖的package包/类
/**
* Converts a .docx document into HTML markup. This code
* is based on <a href="http://stackoverflow.com/a/9053258/313554">this StackOverflow</a> answer.
*
* @param wordDocument The converted .docx document.
* @return
*/
public ConvertedDocumentDTO convertWordDocumentIntoHtml(MultipartFile wordDocument) {
LOGGER.info("Converting word document: {} into HTML", wordDocument.getOriginalFilename());
try {
InputStream input = wordDocument.getInputStream();
Parser parser = new OOXMLParser();
StringWriter sw = new StringWriter();
SAXTransformerFactory factory = (SAXTransformerFactory)
SAXTransformerFactory.newInstance();
TransformerHandler handler = factory.newTransformerHandler();
handler.getTransformer().setOutputProperty(OutputKeys.ENCODING, "utf-8");
handler.getTransformer().setOutputProperty(OutputKeys.METHOD, "html");
handler.getTransformer().setOutputProperty(OutputKeys.INDENT, "yes");
handler.setResult(new StreamResult(sw));
Metadata metadata = new Metadata();
metadata.add(Metadata.CONTENT_TYPE, "text/html;charset=utf-8");
parser.parse(input, handler, metadata, new ParseContext());
return new ConvertedDocumentDTO(wordDocument.getOriginalFilename(), sw.toString());
}
catch (IOException | SAXException | TransformerException | TikaException ex) {
LOGGER.error("Conversion failed because an exception was thrown", ex);
throw new DocumentConversionException(ex.getMessage(), ex);
}
}
示例3: getFullText
import org.apache.tika.parser.Parser; //导入依赖的package包/类
private static String getFullText(final String filepath) throws IOException, SAXException, TikaException {
StringWriter writer = new StringWriter();
final TikaInputStream inputStream = TikaInputStream.get(new File(filepath));
try {
final Detector detector = new DefaultDetector();
final Parser parser = new AutoDetectParser(detector);
final Metadata metadata = new Metadata();
final ParseContext parseContext = new ParseContext();
parseContext.set(Parser.class, parser);
ContentHandler contentHandler = new BodyContentHandler(writer);
parser.parse(inputStream, contentHandler, metadata, parseContext);
}
finally {
inputStream.close();
}
return writer.toString();
}
示例4: buildParseContext
import org.apache.tika.parser.Parser; //导入依赖的package包/类
@Override
protected ParseContext buildParseContext(Metadata metadata,
String targetMimeType, TransformationOptions options) {
ParseContext context = super.buildParseContext(metadata, targetMimeType, options);
boolean recurse = includeContents;
if(options.getIncludeEmbedded() != null)
{
recurse = options.getIncludeEmbedded();
}
if(recurse)
{
// Use an auto detect parser to handle the contents
if(tikaConfig == null)
{
tikaConfig = TikaConfig.getDefaultConfig();
}
context.set(Parser.class, new AutoDetectParser(tikaConfig));
}
return context;
}
示例5: testSupports
import org.apache.tika.parser.Parser; //导入依赖的package包/类
public void testSupports() throws Exception
{
ArrayList<String> mimeTypes = new ArrayList<String>();
for (Parser p : new Parser[] {
new OfficeParser(), new OpenDocumentParser(),
new Mp3Parser(), new OOXMLParser()
}) {
Set<MediaType> mts = p.getSupportedTypes(new ParseContext());
for (MediaType mt : mts)
{
mimeTypes.add(mt.toString());
}
}
for (String mimetype : mimeTypes)
{
boolean supports = extracter.isSupported(mimetype);
assertTrue("Mimetype should be supported: " + mimetype, supports);
}
}
示例6: extractText
import org.apache.tika.parser.Parser; //导入依赖的package包/类
@Override
public void extractText(String mimeType, InputStream input, StringBuilder outputText, int maxSize)
throws IOException
{
WriteOutContentHandler wrapped = new WriteOutContentHandler(maxSize);
ContentHandler handler = new BodyContentHandler(wrapped);
try
{
Metadata meta = new Metadata();
Parser parser = new AutoDetectParser(new TikaConfig(getClass().getClassLoader()));
parser.parse(input, handler, meta, new ParseContext());
appendText(handler, outputText, maxSize);
}
catch( Exception t )
{
if( wrapped.isWriteLimitReached(t) )
{
// keep going
LOGGER.debug("PDF size limit reached. Indexing truncated text");
appendText(handler, outputText, maxSize);
return;
}
throw Throwables.propagate(t);
}
}
示例7: PDFExtract
import org.apache.tika.parser.Parser; //导入依赖的package包/类
public PDFExtract(){
parser = new AutoDetectParser();
TesseractOCRConfig config = new TesseractOCRConfig();
PDFParserConfig pdfConfig = new PDFParserConfig();
pdfConfig.setExtractInlineImages(true);
parseContext = new ParseContext();
parseContext.set(TesseractOCRConfig.class, config);
parseContext.set(PDFParserConfig.class, pdfConfig);
//need to add this to make sure recursive parsing happens!
parseContext.set(Parser.class, parser);
}
示例8: getMetadata
import org.apache.tika.parser.Parser; //导入依赖的package包/类
@Override
public Single<Map<String, String>> getMetadata(InputStream ins) {
return Single.create(sub -> {
Parser parser = new AutoDetectParser();
BodyContentHandler handler = new BodyContentHandler();
Metadata metadata = new Metadata();
ParseContext context = new ParseContext();
try {
parser.parse(ins, handler, metadata, context);
Map<String, String> map = new HashMap<>();
String[] metadataNames = metadata.names();
for (String name : metadataNames) {
map.put(name, metadata.get(name));
}
sub.onSuccess(map);
} catch (Exception e) {
sub.onError(e);
}
// ins.close();
});
}
示例9: extractMetaData
import org.apache.tika.parser.Parser; //导入依赖的package包/类
public static TreeMap<String, String> extractMetaData(InputStream input) throws IOException {
TreeMap<String, String> treeMap = new TreeMap<String, String>();
try {
ContentHandler handler = new DefaultHandler();
Metadata metadata = new Metadata();
Parser parser = new AutoDetectParser();
parser.parse(input, handler, metadata, new ParseContext());
for (int i = 0; i < metadata.names().length; i++) {
String name = metadata.names()[i];
treeMap.put(name, stripWhiteSpace(metadata.get(name)));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (input != null)
input.close();
}
return treeMap;
}
示例10: extractStringMetaData
import org.apache.tika.parser.Parser; //导入依赖的package包/类
public static String extractStringMetaData(InputStream input) throws IOException {
String result="";
try {
ContentHandler handler = new DefaultHandler();
Metadata metadata = new Metadata();
Parser parser = new AutoDetectParser();
parser.parse(input, handler, metadata, new ParseContext());
for (int i = 0; i < metadata.names().length; i++) {
String name = metadata.names()[i];
result+=name.toUpperCase()+" : "+stripWhiteSpace(metadata.get(name))+"\n";
}
} catch (Exception e) {
} finally {
if (input != null)
input.close();
}
return result;
}
示例11: Convert
import org.apache.tika.parser.Parser; //导入依赖的package包/类
protected String Convert(InputStream Bytes) throws PDException
{
try {
ContentHandler textHandler=new BodyContentHandler(-1);
Metadata metadata=new Metadata();
Parser parser=new AutoDetectParser();
ParseContext context=new ParseContext();
parser.parse(Bytes, textHandler, metadata, context);
FileMetadata="";
for (String key : metadata.names())
FileMetadata+=key+"="+metadata.get(key)+"\n";
FullText=textHandler.toString();
} catch (Exception ex)
{
PDException.GenPDException("Error_extracting_content_from_doc", ex.getLocalizedMessage());
}
return(FullText);
}
示例12: initSize
import org.apache.tika.parser.Parser; //导入依赖的package包/类
private static ProcessResult initSize(BaseFileItem f, File img, String mime) {
ProcessResult res = new ProcessResult();
res.setProcess("get image dimensions :: " + f.getId());
final Parser parser = new ImageParser();
try (InputStream is = new FileInputStream(img)) {
Metadata metadata = new Metadata();
metadata.set(CONTENT_TYPE, mime);
parser.parse(is, new DefaultHandler(), metadata, new ParseContext());
f.setWidth(Integer.valueOf(metadata.get(TIFF.IMAGE_WIDTH)));
f.setHeight(Integer.valueOf(metadata.get(TIFF.IMAGE_LENGTH)));
res.setExitCode(ZERO);
} catch (Exception e) {
log.error("Error while getting dimensions", e);
res.setError("Error while getting dimensions");
res.setException(e.getMessage());
res.setExitCode(-1);
}
return res;
}
示例13: main
import org.apache.tika.parser.Parser; //导入依赖的package包/类
public static void main(String[] args) {
String[] urls = {"http://t.co/hP5PM6fm", "http://t.co/xSFteG23"};
for (String url : urls)
{
try {
Parser parser = new AutoDetectParser();
Metadata metadata = new Metadata();
ParseContext parseContext = new ParseContext();
URL urlObject = new URL(url);
ContentHandler handler = new BodyContentHandler(10 *
1024 * 1024);
parser.parse((InputStream) urlObject.getContent(),
handler, metadata, parseContext);
String[] mimeDetails = metadata.get("Content-Type")
.split(";");
logger.info("execute: url = "+url+", mimeDetails = "+Arrays.asList(mimeDetails));
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
示例14: detectParser
import org.apache.tika.parser.Parser; //导入依赖的package包/类
private Parser detectParser(Record record) {
if (!hasAtLeastOneMimeType(record)) {
return null;
}
String mediaTypeStr = (String) record.getFirstValue(Fields.ATTACHMENT_MIME_TYPE); //ExtractingParams.STREAM_TYPE);
assert mediaTypeStr != null;
MediaType mediaType = parseMediaType(mediaTypeStr).getBaseType();
Parser parser = mediaTypeToParserMap.get(mediaType); // fast path
if (parser != null) {
return parser;
}
// wildcard matching
for (Map.Entry<MediaType, Parser> entry : mediaTypeToParserMap.entrySet()) {
if (isMediaTypeMatch(mediaType, entry.getKey())) {
return entry.getValue();
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("No supported MIME type parser found for " + Fields.ATTACHMENT_MIME_TYPE + "=" + mediaTypeStr);
}
return null;
}
示例15: imageParserShouldReturnMarkerInformationOfImage
import org.apache.tika.parser.Parser; //导入依赖的package包/类
@Test
public void imageParserShouldReturnMarkerInformationOfImage() throws Exception {
Parser parser = new ImageParser();
Metadata metadata = new Metadata();
metadata.set(Metadata.CONTENT_TYPE, "image/jpeg");
InputStream stream =getClass().getResourceAsStream("/test-documents/testJPEG.jpg");
parser.parse(stream, new DefaultHandler(), metadata, new ParseContext());
assertEquals("class=0, htableId=0", metadata.get("markerSequence dht dhtable"));
assertEquals("225", metadata.get("markerSequence unknown"));
assertEquals("componentSelector=1, dcHuffTable=0, acHuffTable=0", metadata.get("markerSequence sos scanComponentSpec"));
assertEquals("elementPrecision=0, qtableId=0", metadata.get("markerSequence dqt dqtable"));
assertEquals("numScanComponents=3, startSpectralSelection=0, endSpectralSelection=63, approxHigh=0, approxLow=0", metadata.get("markerSequence sos"));
assertEquals("componentId=1, HsamplingFactor=1, VsamplingFactor=1, QtableSelector=0", metadata.get("markerSequence sof componentSpec"));
assertEquals("process=0, samplePrecision=8, numLines=75, samplesPerLine=100, numFrameComponents=3", metadata.get("markerSequence sof"));
}