/**
  * get the TableCell which belongs to the TableCell
  *
  * @param cell
  */
 public XWPFTableCell getTableCell(CTTc cell) {
   XmlCursor cursor = cell.newCursor();
   cursor.toParent();
   XmlObject o = cursor.getObject();
   if (!(o instanceof CTRow)) {
     return null;
   }
   CTRow row = (CTRow) o;
   cursor.toParent();
   o = cursor.getObject();
   cursor.dispose();
   if (!(o instanceof CTTbl)) {
     return null;
   }
   CTTbl tbl = (CTTbl) o;
   XWPFTable table = getTable(tbl);
   if (table == null) {
     return null;
   }
   XWPFTableRow tableRow = table.getRow(row);
   if (row == null) {
     return null;
   }
   return tableRow.getTableCell(cell);
 }
 /**
  * @param cursor
  * @return the inserted table
  */
 public XWPFTable insertNewTbl(XmlCursor cursor) {
   if (isCursorInHdrF(cursor)) {
     String uri = CTTbl.type.getName().getNamespaceURI();
     String localPart = "tbl";
     cursor.beginElement(localPart, uri);
     cursor.toParent();
     CTTbl t = (CTTbl) cursor.getObject();
     XWPFTable newT = new XWPFTable(t, this);
     cursor.removeXmlContents();
     XmlObject o = null;
     while (!(o instanceof CTTbl) && (cursor.toPrevSibling())) {
       o = cursor.getObject();
     }
     if (!(o instanceof CTTbl)) {
       tables.add(0, newT);
     } else {
       int pos = tables.indexOf(getTable((CTTbl) o)) + 1;
       tables.add(pos, newT);
     }
     int i = 0;
     cursor = t.newCursor();
     while (cursor.toPrevSibling()) {
       o = cursor.getObject();
       if (o instanceof CTP || o instanceof CTTbl) i++;
     }
     bodyElements.add(i, newT);
     cursor = t.newCursor();
     cursor.toEndToken();
     return newT;
   }
   return null;
 }
 /**
  * add a new paragraph at position of the cursor
  *
  * @param cursor
  * @return the inserted paragraph
  */
 public XWPFParagraph insertNewParagraph(XmlCursor cursor) {
   if (isCursorInHdrF(cursor)) {
     String uri = CTP.type.getName().getNamespaceURI();
     String localPart = "p";
     cursor.beginElement(localPart, uri);
     cursor.toParent();
     CTP p = (CTP) cursor.getObject();
     XWPFParagraph newP = new XWPFParagraph(p, this);
     XmlObject o = null;
     while (!(o instanceof CTP) && (cursor.toPrevSibling())) {
       o = cursor.getObject();
     }
     if ((!(o instanceof CTP)) || (CTP) o == p) {
       paragraphs.add(0, newP);
     } else {
       int pos = paragraphs.indexOf(getParagraph((CTP) o)) + 1;
       paragraphs.add(pos, newP);
     }
     int i = 0;
     cursor.toCursor(p.newCursor());
     while (cursor.toPrevSibling()) {
       o = cursor.getObject();
       if (o instanceof CTP || o instanceof CTTbl) i++;
     }
     bodyElements.add(i, newP);
     cursor.toCursor(p.newCursor());
     cursor.toEndToken();
     return newP;
   }
   return null;
 }
 /**
  * verifies that cursor is on the right position
  *
  * @param cursor
  */
 private boolean isCursorInHdrF(XmlCursor cursor) {
   XmlCursor verify = cursor.newCursor();
   verify.toParent();
   if (verify.getObject() == this.headerFooter) {
     return true;
   }
   return false;
 }
Esempio n. 5
0
 private static final String formatQName(XmlCursor xmlc, QName qName) {
   XmlCursor parent = xmlc.newCursor();
   parent.toParent();
   String prefix = parent.prefixForNamespace(qName.getNamespaceURI());
   parent.dispose();
   String name;
   if (prefix == null || prefix.length() == 0) name = qName.getLocalPart();
   else name = prefix + ":" + qName.getLocalPart();
   return name;
 }
  /**
   * Check the segments cardinalities
   *
   * @param segObj the Segment object
   */
  private void checkSegmentCardinalities(XmlObject segObj) {

    XmlCursor cur = segObj.newCursor();
    String usage = cur.getAttributeText(QName.valueOf("Usage"));
    String segName = cur.getAttributeText(QName.valueOf("Name"));
    if (!usage.equals("X")) {
      String min = cur.getAttributeText(QName.valueOf("Min"));
      String max = cur.getAttributeText(QName.valueOf("Max"));
      if (cur.toParent()) {
        if (cur.getName().toString().equals("SegGroup")) {
          ArrayList<Integer> list = profileMapping.get(segObj);
          if (list != null) {
            Iterator<Integer> it = list.iterator();
            ArrayList<ArrayList<Integer>> groups = new ArrayList<ArrayList<Integer>>();
            ArrayList<Integer> currentGroup = null;
            int previous = 0;
            while (it.hasNext()) {
              int lineNr = it.next();
              /* init */
              if (currentGroup == null) {
                ArrayList<Integer> group = new ArrayList<Integer>();
                currentGroup = group;
                currentGroup.add(lineNr);
              } else {
                /* does the line belong to the current group ? */
                boolean b = true;
                int i = previous + 1;
                while (i < lineNr && b) {
                  String line = er7Mapping.get(i);
                  if (!line.matches("\\s*")) {
                    b = false;
                  }
                }
                if (b) {
                  currentGroup.add(lineNr);
                } else {
                  groups.add(currentGroup);
                  ArrayList<Integer> group = new ArrayList<Integer>();
                  currentGroup = group;
                  currentGroup.add(lineNr);
                }
              }
              previous = lineNr;
            }
          }
        } else {
          ArrayList<Integer> list = profileMapping.get(segObj);
          if (list != null) {
            int occurrences = list.size();
            if (occurrences < Integer.parseInt(min)) {
              MessageFailureV2 mf = new MessageFailureV2(message.getEncoding());
              mf.setFailureType(AssertionTypeV2Constants.CARDINALITY);
              mf.setFailureSeverity(ErrorSeverityConstants.NORMAL);
              mf.setDescription(
                  segName
                      + " is present "
                      + occurrences
                      + " times whereas it must be present at least "
                      + min
                      + " times");
              mf.setColumn(((Er7Message) inputMessage).getColumn(getCurrentLocation()));
              mf.setLine(lineNumber);
              mf.setPath(getCurrentLocation().toString());
              messageFailures.add(mf);
            }
            if (!max.equals("*") && occurrences > Integer.parseInt(max)) {
              MessageFailureV2 mf = new MessageFailureV2(message.getEncoding());
              mf.setFailureType(AssertionTypeV2Constants.CARDINALITY);
              mf.setFailureSeverity(ErrorSeverityConstants.NORMAL);
              mf.setDescription(
                  segName
                      + " is present "
                      + occurrences
                      + " times whereas it is only allowed "
                      + max
                      + " times");
              mf.setColumn(((Er7Message) inputMessage).getColumn(getCurrentLocation()));
              mf.setLine(lineNumber);
              mf.setPath(getCurrentLocation().toString());

              messageFailures.add(mf);
            }
          }
        }
      }
    }
  }