Provide a common ole2 implementation of POITextExtractor, which gives access to the...
[poi.git] / src / scratchpad / src / org / apache / poi / hdgf / extractor / VisioTextExtractor.java
blob9b1307cee34ce9d2f952a0e2b66e685f17f6fa60
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
17 package org.apache.poi.hdgf.extractor;
19 import java.io.FileInputStream;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.util.ArrayList;
24 import org.apache.poi.POIOLE2TextExtractor;
25 import org.apache.poi.hdgf.HDGFDiagram;
26 import org.apache.poi.hdgf.chunks.Chunk;
27 import org.apache.poi.hdgf.chunks.Chunk.Command;
28 import org.apache.poi.hdgf.streams.ChunkStream;
29 import org.apache.poi.hdgf.streams.PointerContainingStream;
30 import org.apache.poi.hdgf.streams.Stream;
31 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
33 /**
34 * Class to find all the text in a Visio file, and return it.
35 * Can opperate on the command line (outputs to stdout), or
36 * can return the text for you (eg for use with Lucene).
38 public class VisioTextExtractor extends POIOLE2TextExtractor {
39 private HDGFDiagram hdgf;
40 private POIFSFileSystem fs;
42 public VisioTextExtractor(HDGFDiagram hdgf) {
43 super(hdgf);
44 this.hdgf = hdgf;
46 public VisioTextExtractor(POIFSFileSystem fs) throws IOException {
47 this(new HDGFDiagram(fs));
48 this.fs = fs;
50 public VisioTextExtractor(InputStream inp) throws IOException {
51 this(new POIFSFileSystem(inp));
54 /**
55 * Locates all the text entries in the file, and returns their
56 * contents.
58 public String[] getAllText() {
59 ArrayList text = new ArrayList();
60 for(int i=0; i<hdgf.getTopLevelStreams().length; i++) {
61 findText(hdgf.getTopLevelStreams()[i], text);
63 return (String[])text.toArray( new String[text.size()] );
65 private void findText(Stream stream, ArrayList text) {
66 if(stream instanceof PointerContainingStream) {
67 PointerContainingStream ps = (PointerContainingStream)stream;
68 for(int i=0; i<ps.getPointedToStreams().length; i++) {
69 findText(ps.getPointedToStreams()[i], text);
72 if(stream instanceof ChunkStream) {
73 ChunkStream cs = (ChunkStream)stream;
74 for(int i=0; i<cs.getChunks().length; i++) {
75 Chunk chunk = cs.getChunks()[i];
76 if(chunk != null &&
77 chunk.getName() != null &&
78 chunk.getName().equals("Text") &&
79 chunk.getCommands().length > 0) {
80 // First command
81 Command cmd = chunk.getCommands()[0];
82 if(cmd != null && cmd.getValue() != null) {
83 text.add( cmd.getValue().toString() );
90 /**
91 * Returns the textual contents of the file.
92 * Each textual object's text will be separated
93 * by a newline
95 public String getText() {
96 StringBuffer text = new StringBuffer();
97 String[] allText = getAllText();
98 for(int i=0; i<allText.length; i++) {
99 text.append(allText[i]);
100 if(!allText[i].endsWith("\r") &&
101 !allText[i].endsWith("\n")) {
102 text.append("\n");
105 return text.toString();
108 public static void main(String[] args) throws Exception {
109 if(args.length == 0) {
110 System.err.println("Use:");
111 System.err.println(" VisioTextExtractor <file.vsd>");
112 System.exit(1);
115 VisioTextExtractor extractor =
116 new VisioTextExtractor(new FileInputStream(args[0]));
118 // Print not PrintLn as already has \n added to it
119 System.out.print(extractor.getText());