/**
   * Use an XSLT to alter the contents of this package. The output of the transformation must be
   * valid pck:package/pck:part format, as emitted by Word 2007.
   *
   * @param is
   * @param transformParameters
   * @throws Exception
   */
  public void transform(Templates xslt, Map<String, Object> transformParameters) throws Exception {

    // Prepare in the input document

    FlatOpcXmlCreator worker = new FlatOpcXmlCreator(this);
    org.docx4j.xmlPackage.Package pkg = worker.get();

    JAXBContext jc = Context.jcXmlPackage;
    Marshaller marshaller = jc.createMarshaller();
    org.w3c.dom.Document doc = org.docx4j.XmlUtils.neww3cDomDocument();
    marshaller.marshal(pkg, doc);

    //		javax.xml.bind.util.JAXBResult result = new javax.xml.bind.util.JAXBResult(jc );

    // Use constructor which takes Unmarshaller, rather than JAXBContext,
    // so we can set JaxbValidationEventHandler
    Unmarshaller u = jc.createUnmarshaller();
    u.setEventHandler(new org.docx4j.jaxb.JaxbValidationEventHandler());
    javax.xml.bind.util.JAXBResult result = new javax.xml.bind.util.JAXBResult(u);

    // Perform the transformation
    org.docx4j.XmlUtils.transform(doc, xslt, transformParameters, result);

    //		javax.xml.bind.JAXBElement je = (javax.xml.bind.JAXBElement)result.getResult();
    //		org.docx4j.xmlPackage.Package wmlPackageEl = (org.docx4j.xmlPackage.Package)je.getValue();
    org.docx4j.xmlPackage.Package wmlPackageEl =
        (org.docx4j.xmlPackage.Package) XmlUtils.unwrap(result.getResult());

    org.docx4j.convert.in.FlatOpcXmlImporter xmlPackage =
        new org.docx4j.convert.in.FlatOpcXmlImporter(wmlPackageEl);

    ContentTypeManager ctm = new ContentTypeManager();

    Part tmpDocPart = xmlPackage.getRawPart(ctm, "/word/document.xml", null);
    Part tmpStylesPart = xmlPackage.getRawPart(ctm, "/word/styles.xml", null);

    // This code assumes all the existing rels etc of
    // the existing main document part are still relevant.
    //		if (wmlDocument==null) {
    //			log.warn("Couldn't get main document part from package transform result!");
    //		} else {
    //			this.getMainDocumentPart().setJaxbElement(wmlDocument);
    //		}
    this.getMainDocumentPart()
        .setJaxbElement(((JaxbXmlPart<Document>) tmpDocPart).getJaxbElement());
    //
    //		if (wmlStyles==null) {
    //			log.warn("Couldn't get style definitions part from package transform result!");
    //		} else {
    //			this.getMainDocumentPart().getStyleDefinitionsPart().setJaxbElement(wmlStyles);
    //		}
    this.getMainDocumentPart()
        .getStyleDefinitionsPart(true)
        .setJaxbElement(((JaxbXmlPart<Styles>) tmpStylesPart).getJaxbElement());
  }
 public <T> T readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
   JAXBResult out = new JAXBResult(unmarshaller);
   // since the bridge only produces fragments, we need to fire start/end document.
   try {
     out.getHandler().startDocument();
     bridge.marshal(jaxbObject, out);
     out.getHandler().endDocument();
   } catch (SAXException e) {
     throw new JAXBException(e);
   }
   return (T) out.getResult();
 }
Beispiel #3
0
 public <T> T readAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
   try {
     JAXBResult r = new JAXBResult(unmarshaller);
     // bridge marshals a fragment, so we need to add start/endDocument by ourselves
     r.getHandler().startDocument();
     bridge.marshal(jaxbObject, r);
     r.getHandler().endDocument();
     return (T) r.getResult();
   } catch (SAXException e) {
     throw new JAXBException(e);
   }
 }
 /*     */ public <T> T readPayloadAsJAXB(Unmarshaller unmarshaller) throws JAXBException {
   /* 250 */ JAXBResult out = new JAXBResult(unmarshaller);
   /*     */ try
   /*     */ {
     /* 253 */ out.getHandler().startDocument();
     /* 254 */ this.bridge.marshal(this.jaxbObject, out);
     /* 255 */ out.getHandler().endDocument();
     /*     */ } catch (SAXException e) {
     /* 257 */ throw new JAXBException(e);
     /*     */ }
   /* 259 */ return out.getResult();
   /*     */ }
  @Override
  public void check(Document document, Section section) throws ValidatorException {
    long tsStart = System.currentTimeMillis();
    try {
      transformer.transform(new StreamSource(document.getInputStream()), jaxbResult);
      long tsEnd = System.currentTimeMillis();

      SchematronOutput output = (SchematronOutput) jaxbResult.getResult();

      section.setTitle(output.getTitle());
      section.setRuntime((tsEnd - tsStart) + "ms");

      for (Object o : output.getActivePatternAndFiredRuleAndFailedAssert())
        if (o instanceof FailedAssert) add(section, (FailedAssert) o);
    } catch (Exception e) {
      throw new ValidatorException(String.format("Unable to perform check: %s", e.getMessage()), e);
    }
  }