Skip to main content
summaryrefslogtreecommitdiffstats
blob: 8ae5b626c58e3d833fdfa28f7c5b321df234a944 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
 * Created on Apr 13, 2011
 *
 * PLACE_YOUR_DISTRIBUTION_STATEMENT_RIGHT_HERE
 */
package org.eclipse.osee.framework.ui.skynet.render.imageDetection;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Collection;
import javax.xml.bind.DatatypeConverter;
import javax.xml.xpath.XPath;
import org.eclipse.osee.framework.jdk.core.util.Lib;
import org.eclipse.osee.framework.jdk.core.util.xml.Jaxp;
import org.eclipse.osee.framework.jdk.core.util.xml.SimpleNamespaceContext;
import org.eclipse.osee.framework.jdk.core.util.xml.Xml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;

public class ImageChecker {

   private final File basePath;

   public ImageChecker(File basePath) {
      this.basePath = basePath;
   }

   /**
    * @param args
    */
   public static void main(String[] args) throws Exception {
      File basePath = new File("C:\\Documents and Settings\\b1565043\\Desktop\\extracted");

      Lib.deleteDir(basePath);

      InputStream inputStream = null;
      try {
         inputStream =
            new BufferedInputStream(new FileInputStream(
               "C:\\Documents and Settings\\b1565043\\Desktop\\_MPD_VAM_CONTROL__1177719__20110414_093657-41.xml"));
         ImageChecker checker = new ImageChecker(basePath);
         checker.extractImages(inputStream);
      } finally {
         Lib.close(inputStream);
      }
   }

   private void extractImages(InputStream inputStream) throws Exception {
      Document document = Jaxp.readXmlDocumentNamespaceAware(inputStream);
      Element rootElement = document.getDocumentElement();

      XPath xPath = Jaxp.createXPath();
      SimpleNamespaceContext context = new SimpleNamespaceContext();
      Xml.addNamespacesForWordMarkupLanguage(xPath, context);
      Collection<Node> nodes = Jaxp.selectNodesViaXPath(xPath, rootElement, "//w:binData");

      for (Node node : nodes) {
         Element element = (Element) node;
         String fileName = element.getAttribute("w:name");
         String binData = Jaxp.getElementCharacterData(element);
         process(fileName, binData);
      }
   }

   private void process(String fileName, String binData) throws Exception {
      String extension = Lib.getExtension(fileName);
      if (extension.equalsIgnoreCase("wmz")) {
         String name = fileName.replace("wordml://", "");
         name = Lib.removeExtension(name);

         OutputStream outputStream = null;
         InputStream inputStream = null;
         try {
            basePath.mkdirs();
            byte[] data = DatatypeConverter.parseBase64Binary(binData);
            inputStream = new ByteArrayInputStream(data);
            outputStream = new FileOutputStream(new File(basePath, name + ".gzip"));
            Lib.inputStreamToOutputStream(inputStream, outputStream);
            convert(inputStream, outputStream);
         } finally {
            Lib.close(outputStream);
            Lib.close(inputStream);
         }
      } else {
         System.out.println(extension);
      }
   }

   private void convert(InputStream inputStream, OutputStream outputStream) throws Exception {
      EMZHtmlImageHandler emzHtmlImageHandler = new EMZHtmlImageHandler();
      if (emzHtmlImageHandler.isValid(inputStream)) {
         emzHtmlImageHandler.convert(inputStream, outputStream);
      } else {
         System.out.println("Not valid");
         Lib.inputStreamToOutputStream(inputStream, outputStream);
      }
   }
}

Back to the top