/**
  * 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;
 }
 public QName getContentsQName(EDXLDistribution edxl) {
   QName qname = null;
   XmlCursor c =
       edxl.getContentObjectArray(0).getXmlContent().getEmbeddedXMLContentArray(0).newCursor();
   try {
     if (c.toFirstChild()) {
       qname = c.getObject().schemaType().getOuterType().getDocumentElementName();
     }
   } catch (Exception e) {
     log.error("Error finding EDXL-DE content type: " + e.getMessage());
     log.error("From EDXL-DE: " + edxl.toString());
   } finally {
     c.dispose();
   }
   return qname;
 }
 public void readHdrFtr() {
   bodyElements = new ArrayList<IBodyElement>();
   paragraphs = new ArrayList<XWPFParagraph>();
   tables = new ArrayList<XWPFTable>();
   // parse the document with cursor and add
   // the XmlObject to its lists
   XmlCursor cursor = headerFooter.newCursor();
   cursor.selectPath("./*");
   while (cursor.toNextSelection()) {
     XmlObject o = cursor.getObject();
     if (o instanceof CTP) {
       XWPFParagraph p = new XWPFParagraph((CTP) o, this);
       paragraphs.add(p);
       bodyElements.add(p);
     }
     if (o instanceof CTTbl) {
       XWPFTable t = new XWPFTable((CTTbl) o, this);
       tables.add(t);
       bodyElements.add(t);
     }
   }
   cursor.dispose();
   getAllPictures();
 }
  @Test
  public void shouldCreateValidNotification() throws XmlException, IOException, OXFException {
    String sesUrl = "http://ses.host";
    String dialect = "http://my-funny/dialect";
    ParameterContainer parameters = new ParameterContainer();
    parameters.addParameterShell(SESRequestBuilder_00.NOTIFY_SES_URL, sesUrl);
    parameters.addParameterShell(SESRequestBuilder_00.NOTIFY_TOPIC_DIALECT, dialect);
    parameters.addParameterShell(SESRequestBuilder_00.NOTIFY_TOPIC, "<start>topic</start>");
    parameters.addParameterShell(SESRequestBuilder_00.NOTIFY_XML_MESSAGE, readMessage());

    SESRequestBuilder_00 request = new SESRequestBuilder_00();
    String asText = request.buildNotifyRequest(parameters);
    EnvelopeDocument envelope = EnvelopeDocument.Factory.parse(asText);

    XMLBeansParser.registerLaxValidationCase(SASamplingPointCase.getInstance());
    Collection<XmlError> errors = XMLBeansParser.validate(envelope);
    Assert.assertTrue("Notification is not valid: " + errors, errors.isEmpty());

    XmlCursor bodyCur = envelope.getEnvelope().getBody().newCursor();
    bodyCur.toFirstChild();
    Assert.assertTrue(bodyCur.getObject() instanceof Notify);
    XmlCursor tmpCur =
        ((Notify) bodyCur.getObject()).getNotificationMessageArray()[0].getMessage().newCursor();
    tmpCur.toFirstChild();
    Assert.assertTrue(tmpCur.getObject() instanceof ObservationType);

    Assert.assertTrue(
        ((Notify) bodyCur.getObject())
            .getNotificationMessageArray()[0]
            .getTopic()
            .getDialect()
            .trim()
            .equals(dialect));

    tmpCur = ((Notify) bodyCur.getObject()).getNotificationMessageArray()[0].getTopic().newCursor();

    Assert.assertTrue(tmpCur.getObject().xmlText().contains("<start>"));
  }
  @Test
  public void shouldCreateValidLevel1SubscribeRequest() throws OXFException, XmlException {
    String consumer = "http://*****:*****@xlink:href='urn:ogc:object:procedure:CITE:WeatherService:LGA'";

    ParameterContainer parameter = new ParameterContainer();
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_SES_URL, host);
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_CONSUMER_REFERENCE_ADDRESS, consumer);
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_FILTER_TOPIC, topicString);
    parameter.addParameterShell(
        ISESRequestBuilder.SUBSCRIBE_FILTER_MESSAGE_CONTENT_DIALECT, dialect);
    parameter.addParameterShell(ISESRequestBuilder.SUBSCRIBE_FILTER_MESSAGE_CONTENT, xpath);

    SESRequestBuilder_00 request = new SESRequestBuilder_00();
    String asText = request.buildSubscribeRequest(parameter);
    EnvelopeDocument envelope = EnvelopeDocument.Factory.parse(asText);

    Collection<XmlError> errors = XMLBeansParser.validate(envelope);
    Assert.assertTrue("SubscribeRequest is not valid: " + errors, errors.isEmpty());

    Body body = envelope.getEnvelope().getBody();
    XmlCursor cur = body.newCursor();
    cur.toFirstChild();
    XmlObject subscribeRequestObject = cur.getObject();
    cur.dispose();

    if (!(subscribeRequestObject instanceof Subscribe))
      throw new IllegalStateException("Body does not contain a Subscribe request.");

    Subscribe subscribeRequest = (Subscribe) subscribeRequestObject;
    Assert.assertTrue(
        subscribeRequest
            .getConsumerReference()
            .getAddress()
            .getStringValue()
            .trim()
            .equals(consumer.trim()));

    cur = subscribeRequest.getFilter().newCursor();
    cur.toFirstChild();
    do {
      XmlObject filterElement = cur.getObject();
      if (filterElement instanceof QueryExpressionType) {
        QueryExpressionType qet = (QueryExpressionType) filterElement;
        Assert.assertTrue(qet.getDialect().trim().equals(dialect.trim()));
        XmlCursor tmpCur = qet.newCursor();
        Assert.assertTrue(tmpCur.toFirstContentToken() == TokenType.TEXT);
        Assert.assertTrue(tmpCur.getChars().trim().equals(xpath.trim()));
      } else if (filterElement instanceof TopicExpressionType) {
        TopicExpressionType tet = (TopicExpressionType) filterElement;
        Assert.assertTrue(
            tet.getDialect().trim().equals(SESRequestBuilder_00.DEFAULT_TOPIC_DIALECT));
        XmlCursor tmpCur = tet.newCursor();
        Assert.assertTrue(tmpCur.toFirstContentToken() == TokenType.TEXT);
        Assert.assertTrue(tmpCur.getChars().trim().equals(topicString.trim()));
      }
    } while (cur.toNextSibling());
  }