public Integer getImageNr() {
    Integer url = -1;
    try {
      ElementIterator it = new ElementIterator(this.htmlDoc);
      Element element;
      while ((element = it.next()) != null) {
        // <div id="picture">
        Element elementIdAnswers = getChildElementByTagName(element, Tag.IMG, null);
        if (elementIdAnswers != null) {
          Map<String, String> foundAtrib = this.getAtributes(elementIdAnswers, questionReadAtrib);
          String href = foundAtrib.get("a");
          if (href != null) {
            href = clearHref(href);
            url = CommonS.strToInteger(href);
            break;
          }
        }
      }

    } catch (Exception ex) {
      logger.error("Save page: " + CommonS.saveStringList(buff));
    }

    // if (url < 1){
    //	HtmlPrinter hp = new HtmlPrinter(this.htmlDoc);
    //	CommonS.out("Save page: "+ CommonS.saveStringList(buff));
    //	CommonS.stopThread(" Picture Href not found ! ");
    // }

    return url;
  }
Exemple #2
0
  public JSONArray convertHTMLToFlag(HTMLDocument htmlDoc) {
    boolean isExistFace = false;
    ElementIterator it = new ElementIterator(htmlDoc);
    Element element;
    while ((element = it.next()) != null) {
      if (element.getName().equals(HTML.Tag.IMG.toString())) {
        isExistFace = true;
        try {
          String name = element.getAttributes().getAttribute(HTML.Attribute.NAME).toString();
          // String src = element.getAttributes().getAttribute(HTML.Attribute.SRC).toString();

          int offset = element.getStartOffset();
          htmlDoc.replace(offset, element.getEndOffset() - offset, "~face:" + name + "~", null);

        } catch (BadLocationException ex) {
          Logger.getLogger(QQImageUtil.class.getName()).log(Level.SEVERE, null, ex);
        }
      }
    }
    String text = null;
    try {
      text = htmlDoc.getText(0, htmlDoc.getLength());
      htmlDoc.remove(0, htmlDoc.getLength());
    } catch (BadLocationException ex) {
      Logger.getLogger(QQImageUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    if (isExistFace) {
      text = text.replaceFirst("\\n", "");
    }
    // Log.println(text);
    JSONArray msg = new JSONArray();
    String[] arr = text.split("~");
    for (int i = 0; i < arr.length; i++) {
      String temp = arr[i];
      // Log.println(temp);
      if (temp.startsWith("face:")) {
        String[] tempArray = temp.split(":");
        JSONArray face = new JSONArray();
        face.add(tempArray[0]);
        String regex = ",([0-9]*):" + tempArray[1] + ",";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(faceFlag);
        String result = null;
        if (m.find()) {
          result = m.group(1);
        } else {
          result = tempArray[1];
        }
        int faceNumber = Integer.parseInt(result);
        face.add(faceNumber);
        msg.add(face);
      } else {
        msg.add(temp);
      }
    }

    // Log.println(msg);
    return msg;
  }
  /**
   * Clones the ElementIterator.
   *
   * @return a cloned ElementIterator Object.
   */
  public synchronized Object clone() {

    try {
      ElementIterator it = new ElementIterator(root);
      if (elementStack != null) {
        it.elementStack = new Stack();
        for (int i = 0; i < elementStack.size(); i++) {
          StackItem item = (StackItem) elementStack.elementAt(i);
          StackItem clonee = (StackItem) item.clone();
          it.elementStack.push(clonee);
        }
      }
      return it;
    } catch (CloneNotSupportedException e) {
      throw new InternalError();
    }
  }
  public Question getQuestion() throws Exception {
    Question question = new Question();
    try {
      ElementIterator it = new ElementIterator(this.htmlDoc);
      Element element;
      while ((element = it.next()) != null) {

        // <div id="question">
        Element elementQuestion = getChildElementByTagName(element, Tag.DIV, srcIdQuest);
        if (elementQuestion != null) {
          // <div class="question"> text . . .
          Element elementQuestionText =
              getChildElementByTagName(elementQuestion, Tag.DIV, srcClassQuest);
          if (elementQuestionText != null)
            question.setQuestionText(getElementText(elementQuestionText));
          // <input id="question" type="hidden" value="32384"
          // name="question">.
          Element elementQuestionInput =
              getChildElementByTagName(elementQuestion, Tag.IMPLIED, null /* searchQusetion */);
          if (elementQuestionInput != null) {
            Element elementQuestionInput2 =
                getChildElementByTagName(elementQuestionInput, Tag.INPUT, srcIdQuest);
            if (elementQuestionInput2 != null) {
              Map<String, String> foundAtrib =
                  this.getAtributes(elementQuestionInput2, questionReadAtrib);
              String str = foundAtrib.get("value");
              Integer sk = CommonS.strToInteger(str);
              question.setQuestionCsddId(sk);
            }
          }
        }
      }
    } catch (Exception e) {
      logger.error("", e);
      CommonS.stopThread(e);
    }
    if (!question.isQuestionOk()) {
      logger.error("question not found ! ");
      logger.error("Save page: " + CommonS.saveStringList(buff));
      throw new MyCustException("Ending thread");
      // CommonS.stopThread();
    }

    return question;
  }
  public Integer getCorrectAnswer() throws MyCustException {
    Integer csddIdInt = 0;
    try {
      ElementIterator it = new ElementIterator(this.htmlDoc);
      Element element;
      while ((element = it.next()) != null) {
        // <div id="answers">
        Element elementIdAnswers = getChildElementByTagName(element, Tag.DIV, srcIdAnswers);
        if (elementIdAnswers != null) {
          // <div>
          Element elementDiv = getChildElementByTagName(elementIdAnswers, Tag.DIV, null);
          if (elementDiv != null) {
            // <div class= 'answer right'>
            Element elementAnswerRight =
                getChildElementByTagName(elementDiv, Tag.DIV, srcAnswerRight);
            if (elementAnswerRight != null) {
              Element elementClassAnswerInp =
                  getChildElementByTagName(elementAnswerRight, Tag.IMPLIED, null);
              if (elementClassAnswerInp != null) {
                Element elementClassAnswerInp2 =
                    getChildElementByTagName(elementClassAnswerInp, Tag.INPUT, srcNameAnswer);
                if (elementClassAnswerInp2 != null) {
                  Map<String, String> foundAtrib =
                      this.getAtributes(elementClassAnswerInp2, questionReadAtrib);
                  String csddId = foundAtrib.get("id");
                  csddId = clearAnswer(csddId);
                  csddIdInt = CommonS.strToInteger(csddId);
                }
              }
            }
          }
        }
      }

    } catch (Exception ex) {
      logger.error("Save page: " + CommonS.saveStringList(buff));
    }
    if (csddIdInt == 0) {
      logger.info("Save page: " + CommonS.saveStringList(buff));
      CommonS.stopThread("CorrectAnswer not found !");
    }

    return csddIdInt;
  }
  public Map<Integer, Integer> getAnswerIds() throws MyCustException {
    Map<Integer, Integer> answers = new HashMap<Integer, Integer>();
    try {
      ElementIterator it = new ElementIterator(this.htmlDoc);
      Element element;
      while ((element = it.next()) != null) {
        // <div id="navi">
        Element elementIdNav = getChildElementByTagName(element, Tag.DIV, srcIdNavi);
        if (elementIdNav != null) {
          // <div id ='change1'>
          Element elementIdChange1 = getChildElementByTagName(elementIdNav, Tag.DIV, srcIdChange1);
          if (elementIdChange1 != null) {
            // <div id ='change2'>
            Element elementIdChange2 =
                getChildElementByTagName(elementIdChange1, Tag.DIV, srcIdChange2);
            if (elementIdChange2 != null) {
              // <div id ='question_xxxxx'>
              List<Element> elementIdQuestions =
                  getChildElementsByTagName(elementIdChange2, Tag.DIV, null);
              if (elementIdQuestions.size() != 0) {
                for (int index = 1; index < elementIdQuestions.size(); index++) {
                  Element elemntid = elementIdQuestions.get(index);
                  Map<String, String> rez = getAtributes(elemntid, allAnswers);
                  String str = clearQuestion(rez.get("id"));
                  Integer sk = CommonS.strToInteger(str);
                  answers.put(index, sk);
                }
              }
            }
          }
        }
      } // end while
    } catch (Exception e) {
      logger.error("Save page: " + CommonS.saveStringList(buff));
      CommonS.stopThread(e);
    }

    if ((answers.size() == 0) || (answers.size() != TCaseCont.QUESTION_COUNT)) {
      CommonS.stopThread(" short answerList not corect ! ");
    }

    return answers;
  }
  /**
   * @return List<Answer> - order is important!
   * @throws MyCustException
   */
  public List<Answer> getAnswers() throws MyCustException {
    List<Answer> answers = new ArrayList<Answer>();

    try {
      ElementIterator it = new ElementIterator(this.htmlDoc);
      Element element;
      while ((element = it.next()) != null) {
        // <div class="answer"
        List<Element> elementAnswers = getChildElementsByTagName(element, Tag.DIV, srcClassAnswer);
        if (elementAnswers.size() != 0)
          for (Element elementAnswer : elementAnswers) {
            // <input id="answer55667" type="radio" value="55667"
            // name="answer">
            Element elementAnswerInput =
                getChildElementByTagName(elementAnswer, Tag.IMPLIED, null /* searchQusetion */);
            if (elementAnswerInput != null) {
              Element elementAnswerInput2 =
                  getChildElementByTagName(elementAnswerInput, Tag.INPUT, srcNameAnswer);
              if (elementAnswerInput2 != null) {
                Map<String, String> foundAtrib =
                    getAtributes(elementAnswerInput2, questionReadAtrib);
                String csddIdText = foundAtrib.get("value");
                Integer csddId = CommonS.strToInteger(csddIdText);
                String questionText = getElementText(elementAnswer);
                answers.add(new Answer(csddId, questionText));
              }
            }
          }
      }
    } catch (Exception e) {
      logger.error("", e);
      CommonS.stopThread(e);
    }
    if (answers.size() == 0) {
      logger.info("Save page: " + CommonS.saveStringList(buff));
      CommonS.stopThread("Answers not found ! ");
    }
    return answers;
  }
  /**
   * Iterates over the Element tree and controls the writing out of all the tags and its attributes.
   *
   * @exception IOException on any I/O error
   * @exception BadLocationException if pos represents an invalid location within the document.
   */
  public void write() throws IOException, BadLocationException {
    ElementIterator it = getElementIterator();
    Element current = null;
    Element next = null;

    wroteHead = false;
    setCurrentLineLength(0);
    replaceEntities = false;
    setCanWrapLines(false);
    if (segment == null) {
      segment = new Segment();
    }
    inPre = false;
    boolean forcedBody = false;
    while ((next = it.next()) != null) {
      if (!inRange(next)) {
        if (completeDoc
            && next.getAttributes().getAttribute(StyleConstants.NameAttribute) == HTML.Tag.BODY) {
          forcedBody = true;
        } else {
          continue;
        }
      }
      if (current != null) {

        /*
          if next is child of current increment indent
        */

        if (indentNeedsIncrementing(current, next)) {
          incrIndent();
        } else if (current.getParentElement() != next.getParentElement()) {
          /*
             next and current are not siblings
             so emit end tags for items on the stack until the
             item on top of the stack, is the parent of the
             next.
          */
          Element top = (Element) blockElementStack.peek();
          while (top != next.getParentElement()) {
            /*
               pop() will return top.
            */
            blockElementStack.pop();
            if (!synthesizedElement(top)) {
              AttributeSet attrs = top.getAttributes();
              if (!matchNameAttribute(attrs, HTML.Tag.PRE) && !isFormElementWithContent(attrs)) {
                decrIndent();
              }
              endTag(top);
            }
            top = (Element) blockElementStack.peek();
          }
        } else if (current.getParentElement() == next.getParentElement()) {
          /*
             if next and current are siblings the indent level
             is correct.  But, we need to make sure that if current is
             on the stack, we pop it off, and put out its end tag.
          */
          Element top = (Element) blockElementStack.peek();
          if (top == current) {
            blockElementStack.pop();
            endTag(top);
          }
        }
      }
      if (!next.isLeaf() || isFormElementWithContent(next.getAttributes())) {
        blockElementStack.push(next);
        startTag(next);
      } else {
        emptyTag(next);
      }
      current = next;
    }
    /* Emit all remaining end tags */

    /* A null parameter ensures that all embedded tags
       currently in the tags vector have their
       corresponding end tags written out.
    */
    closeOutUnwantedEmbeddedTags(null);

    if (forcedBody) {
      blockElementStack.pop();
      endTag(current);
    }
    while (!blockElementStack.empty()) {
      current = (Element) blockElementStack.pop();
      if (!synthesizedElement(current)) {
        AttributeSet attrs = current.getAttributes();
        if (!matchNameAttribute(attrs, HTML.Tag.PRE) && !isFormElementWithContent(attrs)) {
          decrIndent();
        }
        endTag(current);
      }
    }

    if (completeDoc) {
      writeAdditionalComments();
    }

    segment.array = null;
  }