/**
   * @param sourceFile File to read from
   * @return List of String objects with the shas
   */
  public static FileRequestFileContent readRequestFile(final File sourceFile) {
    if (!sourceFile.isFile() || !(sourceFile.length() > 0)) {
      return null;
    }
    Document d = null;
    try {
      d = XMLTools.parseXmlFile(sourceFile.getPath());
    } catch (final Throwable t) {
      logger.log(Level.SEVERE, "Exception in readRequestFile, during XML parsing", t);
      return null;
    }

    if (d == null) {
      logger.log(Level.SEVERE, "Could'nt parse the request file");
      return null;
    }

    final Element rootNode = d.getDocumentElement();

    if (rootNode.getTagName().equals(TAG_FrostFileRequestFile) == false) {
      logger.severe(
          "Error: xml request file does not contain the root tag '"
              + TAG_FrostFileRequestFile
              + "'");
      return null;
    }

    final String timeStampStr = XMLTools.getChildElementsTextValue(rootNode, TAG_timestamp);
    if (timeStampStr == null) {
      logger.severe("Error: xml file does not contain the tag '" + TAG_timestamp + "'");
      return null;
    }
    final long timestamp = Long.parseLong(timeStampStr);

    final List<Element> nodelist = XMLTools.getChildElementsByTagName(rootNode, TAG_shaList);
    if (nodelist.size() != 1) {
      logger.severe("Error: xml request files must contain only one element '" + TAG_shaList + "'");
      return null;
    }

    final Element rootShaNode = nodelist.get(0);

    final List<String> shaList = new LinkedList<String>();
    final List<Element> xmlKeys = XMLTools.getChildElementsByTagName(rootShaNode, TAG_sha);
    for (final Element el : xmlKeys) {

      final Text txtname = (Text) el.getFirstChild();
      if (txtname == null) {
        continue;
      }

      final String sha = txtname.getData();
      shaList.add(sha);
    }

    final FileRequestFileContent content = new FileRequestFileContent(timestamp, shaList);
    return content;
  }
Example #2
0
  // -----------------------------------------------------
  // Description: Determine if a template exists for the
  // given file and return true on success.
  // -----------------------------------------------------
  boolean templateExistFor(String fileName, String destinationDirectory) {

    templateIdentified = false;

    // from template
    int intOriginX = 0;
    int intOriginY = 0;

    // from template
    int intX = 0;
    int intY = 0;
    int intWidth = 0;
    int intHeight = 0;

    // calculated
    int viaTemplateX = 0;
    int viaTemplateY = 0;
    int viaTemplatePlusWidth = 0;
    int viaTemplatePlusHeight = 0;

    String fileName_woext = fileName.substring(0, fileName.lastIndexOf('.'));
    String pathPlusFileName_woext =
        destinationDirectory + File.separator + fileName_woext + File.separator + fileName_woext;

    try {

      File folder = new File(templateLocation);
      File[] listOfFiles = folder.listFiles();

      long startTime = System.currentTimeMillis();

      for (File file : listOfFiles) {

        if (file.isFile()) {

          templateName = "";

          DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
          DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
          Document doc = dBuilder.parse(file);
          doc.getDocumentElement().normalize();

          System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
          NodeList nList = doc.getElementsByTagName("condition");

          for (int temp = 0; temp < nList.getLength(); temp++) {

            Node nNode = nList.item(temp);
            if (nNode.getNodeType() == Node.ELEMENT_NODE) {

              Element eElement = (Element) nNode;

              String myX = getTagValue("x", eElement);
              String myY = getTagValue("y", eElement);
              String myWidth = getTagValue("width", eElement);
              String myHeight = getTagValue("height", eElement);
              String mySuffix = getTagValue("suffix", eElement);
              String myRequirements = getTagValue("required", eElement);

              intX = Integer.parseInt(myX);
              intY = Integer.parseInt(myY);
              intWidth = Integer.parseInt(myWidth);
              intHeight = Integer.parseInt(myHeight);

              viaTemplateX = intX - (intOriginX - pageX);
              viaTemplateY = intY - (intOriginY - pageY);
              viaTemplatePlusWidth = viaTemplateX + intWidth;
              viaTemplatePlusHeight = viaTemplateY + intHeight;

              Spawn.execute(
                  Settings.Programs.CONVERT.path(),
                  pathPlusFileName_woext + "_trim.png",
                  "-crop",
                  String.format("%dx%d+%d+%d", intWidth, intHeight, viaTemplateX, viaTemplateY),
                  "+repage",
                  pathPlusFileName_woext + "_" + mySuffix + ".png");
              Spawn.execute(
                  Settings.Programs.CONVERT.path(),
                  pathPlusFileName_woext + "_trim.png",
                  "-fill",
                  "none",
                  "-stroke",
                  "red",
                  "-strokewidth",
                  "3",
                  "-draw",
                  String.format(
                      "rectangle %d,%d %d,%d",
                      viaTemplateX, viaTemplateY, viaTemplatePlusWidth, viaTemplatePlusHeight),
                  "+repage",
                  pathPlusFileName_woext + "_draw.png");
              Spawn.execute(
                  Settings.Programs.TESSERACT.path(),
                  pathPlusFileName_woext + "_" + mySuffix + ".png",
                  pathPlusFileName_woext + "_" + mySuffix);

              String line = ""; // String that holds current file line
              String accumulate = "";

              try {
                FileReader input = new FileReader(pathPlusFileName_woext + "_" + mySuffix + ".txt");
                BufferedReader bufRead = new BufferedReader(input);

                line = bufRead.readLine();

                while (line != null) {
                  accumulate += line;
                  line = bufRead.readLine();
                }

                bufRead.close();
                input.close();

              } catch (IOException e) {
                e.printStackTrace();
              }

              String[] requirements = myRequirements.split("#");
              for (String requirement : requirements) {
                if (requirement.equals(accumulate.trim())) {
                  templateName = file.getName();
                  templateIdentified = true;
                  return templateIdentified;
                }
              }
            }
          }
        }
      }

      // record end time
      long endTime = System.currentTimeMillis();
      System.out.println(
          "Template Search [" + fileName + "] " + (endTime - startTime) / 1000 + " seconds");

    } catch (Exception e) {
      e.printStackTrace();
    }

    return templateIdentified;
  }