/**
  * @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;
 }
Example #3
0
 /**
  * Creates the administrative metadata section of a mets document.
  *
  * @param id the id of this amdSec
  * @param owner the rights owner of the item
  * @param logo the logo of the organization
  * @param url the url of the organization
  * @param reference the items url
  */
 public void createAmdSec(String id, String owner, String logo, String url, String reference) {
   this.amdSec = this.mets.addNewAmdSec();
   this.amdSec.setID(id);
   // DVRights
   MdSecType rightsMD = this.amdSec.addNewRightsMD();
   rightsMD.setID("rights" + id);
   MdWrap wrap = rightsMD.addNewMdWrap();
   wrap.setMIMETYPE("text/xml");
   wrap.setMDTYPE(MdWrap.MDTYPE.OTHER);
   wrap.setOTHERMDTYPE("DVRIGHTS");
   XmlData xml = wrap.addNewXmlData();
   XmlObject dvrights = XmlObject.Factory.newInstance();
   XmlCursor cur = dvrights.newCursor();
   cur.toNextToken();
   cur.beginElement("rights", "http://dfg-viewer.de/");
   cur.insertElementWithText("owner", "http://dfg-viewer.de/", owner);
   cur.insertElementWithText("ownerLogo", "http://dfg-viewer.de/", logo);
   cur.insertElementWithText("ownerSiteURL", "http://dfg-viewer.de/", url);
   cur.insertElementWithText("contact", "http://dfg-viewer.de/", "*****@*****.**");
   cur.dispose();
   xml.set(dvrights);
   wrap.setXmlData(xml);
   rightsMD.setMdWrap(wrap);
   this.amdSec.setRightsMDArray(0, rightsMD);
   // DVLinks
   MdSecType digiprovMD = this.amdSec.addNewDigiprovMD();
   digiprovMD.setID("digiprov" + id);
   MdWrap dpWrap = digiprovMD.addNewMdWrap();
   dpWrap.setMIMETYPE("text/xml");
   dpWrap.setMDTYPE(MdWrap.MDTYPE.OTHER);
   dpWrap.setOTHERMDTYPE("DVLINKS");
   XmlData dpXml = dpWrap.addNewXmlData();
   XmlObject dvdigiprov = XmlObject.Factory.newInstance();
   XmlCursor dpCur = dvdigiprov.newCursor();
   dpCur.toNextToken();
   dpCur.beginElement("links", "http://dfg-viewer.de/");
   dpCur.insertElementWithText("reference", "http://dfg-viewer.de/", reference);
   dpCur.insertElementWithText(
       "presentation", "http://dfg-viewer.de/", "http://vm38.mpdl.mpg.de:8080/dlib-journals/");
   dpCur.dispose();
   dpXml.set(dvdigiprov);
   dpWrap.setXmlData(dpXml);
   digiprovMD.setMdWrap(dpWrap);
   this.amdSec.setDigiprovMDArray(0, digiprovMD);
 }
Example #4
0
  public static String createSampleForElement(SchemaGlobalElement element) {
    XmlObject xml = XmlObject.Factory.newInstance();

    XmlCursor c = xml.newCursor();
    c.toNextToken();
    c.beginElement(element.getName());

    new SampleXmlUtil(false).createSampleForType(element.getType(), c);

    c.dispose();

    XmlOptions options = new XmlOptions();
    options.put(XmlOptions.SAVE_PRETTY_PRINT);
    options.put(XmlOptions.SAVE_PRETTY_PRINT_INDENT, 3);
    options.put(XmlOptions.SAVE_AGGRESSIVE_NAMESPACES);
    options.setSaveOuter();
    String result = xml.xmlText(options);

    return result;
  }
  @Override
  protected void checkMessageStructure() throws MessageValidationException {
    boolean messageStructureFailure = false;
    try {
      // Create a pseudo XML message
      XmlObject pseudoMessage = XmlObject.Factory.newInstance();
      XmlCursor pmCursor = pseudoMessage.newCursor();
      pmCursor.toNextToken();
      pmCursor.beginElement(profile.getMessageStructureID(), "urn:hl7-org:v2xml");
      BufferedReader br = new BufferedReader(new StringReader(message.getMessageAsString()));
      String line = null;
      while ((line = br.readLine()) != null) {
        if (!line.matches("\\s*")) {
          String fieldSep = ((Er7Message) message).getFieldSeparatorChar();
          try {
            int idx = line.indexOf(fieldSep);
            if (idx == -1 && line.length() <= 3) {
              idx = 3;
            }
            line = line.substring(0, idx);
            if (!line.startsWith("Z")) {
              pmCursor.beginElement(line, "urn:hl7-org:v2xml");
              pmCursor.toNextToken();
            }
          } catch (StringIndexOutOfBoundsException e) {
            if (line.length() > 3) {
              System.out.println(line);
            }
          }
        }
      }
      pmCursor.dispose();

      // Create a schema
      StreamSource xsltStream =
          new StreamSource(
              MessageStructureValidationV2Er7.class
                  .getClassLoader()
                  .getResourceAsStream(MessageValidationConstants.XSLT_CHECK_STRUCTURE));
      Transformer t = TransformerFactory.newInstance().newTransformer(xsltStream);
      t.setParameter("groups", "false");
      t.setParameter("xml", "false");

      StreamSource src = new StreamSource(profile.getDocument().newInputStream());
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      t.transform(src, new StreamResult(out));
      XmlObject schemaDoc =
          XmlObject.Factory.parse(
              new ByteArrayInputStream(out.toByteArray()), (new XmlOptions()).setLoadLineNumbers());
      // pseudoMessage.save(new File("tmp/mu/PseudoMessage.xml"));
      // schemaDoc.save(new File("tmp/mu/Schema.xsd"));
      // Load the schema
      SchemaTypeLoader sLoader = null;
      Collection<Object> compErrors = new ArrayList<Object>();
      XmlOptions schemaOptions = new XmlOptions();
      schemaOptions.setErrorListener(compErrors);
      XmlObject[] schemas = new XmlObject[1];

      schemas[0] = schemaDoc;
      sLoader = XmlBeans.compileXsd(schemas, sLoader, schemaOptions);

      // Load the Message
      XmlObject xobj =
          sLoader.parse(pseudoMessage.toString(), null, (new XmlOptions()).setLoadLineNumbers());

      // Validate the Message against the schema
      Collection<XmlValidationError> errors = new ArrayList<XmlValidationError>();
      xobj.validate(new XmlOptions().setErrorListener(errors));
      Iterator<XmlValidationError> it = errors.iterator();
      while (it.hasNext()) {
        XmlValidationError xve = it.next();
        messageFailures.add(interpretSchemaError(xve));
        messageStructureFailure = true;
      }
    } catch (XmlException xmle) {
      // This type of exception is thrown when the generated schema is
      // ambiguous
      MessageFailureV2 mf = new MessageFailureV2(message.getEncoding());
      mf.setDescription(
          "The message validation can't be performed because the profile is ambiguous."
              + " Possible reasons for this problem include an ambiguous message definition"
              + " specified in the standard or an ambiguous message definition caused by the"
              + " user changing the Usage settings for segments during profile creation."
              + " Remember that a segment with the same name MUST be separated by at least one"
              + " non-optional segment with a different name.");
      mf.setFailureSeverity(ErrorSeverityConstants.FATAL);
      mf.setFailureType(AssertionTypeV2Constants.AMBIGUOUS_PROFILE);
      messageFailures.add(mf);
    } catch (Exception e) {
      throw new MessageValidationException(e.getMessage());
    } finally {
      if (!messageStructureFailure) {
        MessageFailureV2 mf = new MessageFailureV2(message.getEncoding());
        mf.setDescription("The message structure at the segment level is correct.");
        mf.setFailureSeverity(ErrorSeverityConstants.NORMAL);
        mf.setFailureType(AssertionTypeV2Constants.CHECKED);
        messageFailures.add(mf);
      }
    }
  }