Exemplo n.º 1
0
  protected void setFormObject(Forms forms, XmlObject formObject) {
    // Create a cursor from the grants.gov form
    XmlCursor formCursor = formObject.newCursor();
    formCursor.toStartDoc();
    formCursor.toNextToken();

    // Create a cursor from the Forms object
    XmlCursor metaGrantCursor = forms.newCursor();
    metaGrantCursor.toNextToken();

    // Add the form to the Forms object.
    formCursor.moveXml(metaGrantCursor);
  }
Exemplo n.º 2
0
 public static void fixNamespaceForXsiType(XmlObject content, Map<?, ?> namespaces) {
   final XmlCursor cursor = content.newCursor();
   while (cursor.hasNextToken()) {
     if (cursor.toNextToken().isStart()) {
       final String xsiType = cursor.getAttributeText(W3CConstants.QN_XSI_TYPE);
       if (xsiType != null) {
         final String[] toks = xsiType.split(":");
         if (toks.length > 1) {
           String prefix = toks[0];
           String localName = toks[1];
           String namespace = (String) namespaces.get(prefix);
           if (Strings.isNullOrEmpty(namespace)) {
             namespace = CodingRepository.getInstance().getNamespaceFor(prefix);
           }
           if (StringHelper.isNotEmpty(namespace)) {
             cursor.setAttributeText(
                 W3CConstants.QN_XSI_TYPE,
                 Joiner.on(Constants.COLON_CHAR)
                     .join(
                         XmlHelper.getPrefixForNamespace(content, (String) namespaces.get(prefix)),
                         localName));
           }
         }
       }
     }
   }
   cursor.dispose();
 }
Exemplo n.º 3
0
  public static void fixNamespaceForXsiType(final XmlObject object, final QName value) {
    final XmlCursor cursor = object.newCursor();
    while (cursor.hasNextToken()) {
      if (cursor.toNextToken().isStart()) {
        final String xsiType = cursor.getAttributeText(W3CConstants.QN_XSI_TYPE);
        if (xsiType != null) {

          final String[] toks = xsiType.split(":");
          String localName;
          String prefix;
          if (toks.length > 1) {
            prefix = toks[0];
            localName = toks[1];
          } else {
            localName = toks[0];
          }
          if (localName.equals(value.getLocalPart())) {
            cursor.setAttributeText(
                W3CConstants.QN_XSI_TYPE,
                Joiner.on(Constants.COLON_CHAR)
                    .join(
                        XmlHelper.getPrefixForNamespace(object, value.getNamespaceURI()),
                        value.getLocalPart()));
          }
        }
      }
    }
    cursor.dispose();
  }
Exemplo n.º 4
0
  private void processElement(SchemaParticle sp, XmlCursor xmlc, boolean mixed) {
    // cast as schema local element
    SchemaLocalElement element = (SchemaLocalElement) sp;

    // Add comment about type
    addElementTypeAndRestricionsComment(element, xmlc);

    // / ^ -> <elemenname></elem>^
    if (_soapEnc) xmlc.insertElement(element.getName().getLocalPart()); // soap
    // encoded?
    // drop
    // namespaces.
    else xmlc.insertElement(element.getName().getLocalPart(), element.getName().getNamespaceURI());
    // / -> <elem>^</elem>
    // processAttributes( sp.getType(), xmlc );

    xmlc.toPrevToken();
    // -> <elem>stuff^</elem>

    String[] values = null;
    if (multiValues != null) values = multiValues.get(element.getName());
    if (values != null) xmlc.insertChars(StringUtils.join(values, ","));
    else if (sp.isDefault()) xmlc.insertChars(sp.getDefaultText());
    else createSampleForType(element.getType(), xmlc);
    // -> <elem>stuff</elem>^
    xmlc.toNextToken();
  }
Exemplo n.º 5
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);
 }
Exemplo n.º 6
0
 @SuppressWarnings("unused")
 private void moveToken(int numToMove, XmlCursor xmlc) {
   for (int i = 0; i < Math.abs(numToMove); i++) {
     if (numToMove < 0) {
       xmlc.toPrevToken();
     } else {
       xmlc.toNextToken();
     }
   }
 }
Exemplo n.º 7
0
 /**
  * Remove namespace declarations from an xml fragment (useful for moving all declarations to a
  * document root
  *
  * @param x The fragment to localize
  */
 public static void removeNamespaces(final XmlObject x) {
   final XmlCursor c = x.newCursor();
   while (c.hasNextToken()) {
     if (c.isNamespace()) {
       c.removeXml();
     } else {
       c.toNextToken();
     }
   }
   c.dispose();
 }
Exemplo n.º 8
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;
  }
Exemplo n.º 9
0
  public static String createSampleForType(SchemaType sType) {
    XmlObject object = XmlObject.Factory.newInstance();
    XmlCursor cursor = object.newCursor();
    // Skip the document node
    cursor.toNextToken();
    // Using the type and the cursor, call the utility method to get a
    // sample XML payload for that Schema element
    new SampleXmlUtil(false).createSampleForType(sType, cursor);
    // Cursor now contains the sample payload
    // Pretty print the result. Note that the cursor is positioned at the
    // end of the doc so we use the original xml object that the cursor was
    // created upon to do the xmlText() against.

    cursor.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 = object.xmlText(options);

    return result;
  }
Exemplo n.º 10
0
 static void addStylesheet(InterfaceDocument feed) {
   XmlCursor cursor = feed.newCursor();
   cursor.toNextToken();
   cursor.insertProcInst("xml-stylesheet", "type='text/xsl' href='feed.xsl'");
 }
  @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);
      }
    }
  }