Page 1 of 1

Unmarshall exception using XmlUtils.unmarshallFromTemplate

PostPosted: Wed Nov 10, 2010 9:51 pm
by dagarfol
Hi all, I am working on creating a watermark for a document, and I planned to use the unmarshallFromTemplate utility to create the watermark based on markup captured from a working document. But when I try to use it I get the next exception (below). If i create all the components "by hand", everything goes ok and the document gets its watermark. Can anybody help me?

javax.xml.bind.UnmarshalException: unexpected element (uri:"http://schemas.openxmlformats.org/wordprocessingml/2006/main", local:"pict"). Expected elements are <{urn:schemas-microsoft-com:office:office}OLEObject>,<{urn:schemas-microsoft-com:vml}VMLFrame>,<{urn:schemas-microsoft-com:office:office}WordFieldCodes>,<{http://schemas.openxmlformats.org/wordprocessingml/2006/main}altChunk>,
:
com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:642)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:254)
:
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:104)
at org.docx4j.XmlUtils.unmarshalString(XmlUtils.java:260)
at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:308)
at org.docx4j.XmlUtils.unmarshallFromTemplate(XmlUtils.java:301)
at org.dave.tests.WatermarkGenerator.createWatermark(WatermarkGenerator.java:53)
at org.dave.tests.WatermarkGenerator.main(WatermarkGenerator.java:35)

Re: Unmarshall exception using XmlUtils.unmarshallFromTemplate

PostPosted: Thu Nov 11, 2010 7:15 am
by jason
Could you please post either the string you are trying to unmarshall, or at least an outline of the elements it contains (eg w:p/w:r/w:pict ) so I can advise.

Re: Unmarshall exception using XmlUtils.unmarshallFromTemplate

PostPosted: Thu Nov 11, 2010 8:32 pm
by dagarfol
Hi Jason, this is the code I am trying to unmarshall. It's watermark markup code taken from a working document, which includes w:pict + all the namespaces used, w:shapetipe and w:shape.

new StringBuilder("<w:pict xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\" xmlns:w10=\"urn:schemas-microsoft-com:office:word\" xmlns:r=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships\" xmlns:m=\"http://schemas.openxmlformats.org/officeDocument/2006/math\" xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\" xmlns:a=\"http://schemas.openxmlformats.org/drawingml/2006/main\" xmlns:pkg=\"http://schemas.microsoft.com/office/2006/xmlPackage\" xmlns:ns7=\"http://schemas.openxmlformats.org/schemaLibrary/2006/main\" xmlns:ns8=\"http://schemas.openxmlformats.org/drawingml/2006/chart\" xmlns:ns9=\"http://schemas.openxmlformats.org/drawingml/2006/chartDrawing\" xmlns:ns10=\"http://schemas.openxmlformats.org/drawingml/2006/diagram\" xmlns:pic=\"http://schemas.openxmlformats.org/drawingml/2006/picture\" xmlns:ns12=\"http://schemas.openxmlformats.org/drawingml/2006/spreadsheetDrawing\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:ns15=\"http://opendope.org/xpaths\" xmlns:ns16=\"http://opendope.org/conditions\" xmlns:ns17=\"http://opendope.org/questions\" xmlns:ns18=\"http://schemas.openxmlformats.org/drawingml/2006/compatibility\" xmlns:ns19=\"http://schemas.openxmlformats.org/drawingml/2006/lockedCanvas\">")

.append("<v:shapetype id=\"_x0000_t136\" coordsize=\"21600,21600\" path=\"m@7,l@8,m@5,21600l@6,21600e\"><v:formulas><v:f eqn=\"sum #0 0 10800\"/><v:f eqn=\"prod #0 2 1\"/><v:f eqn=\"sum 21600 0 @1\"/><v:f eqn=\"sum 0 0 @2\"/><v:f eqn=\"sum 21600 0 @3\"/><v:f eqn=\"if @0 @3 0\"/><v:f eqn=\"if @0 21600 @1\"/><v:f eqn=\"if @0 0 @2\"/><v:f eqn=\"if @0 @4 21600\"/><v:f eqn=\"mid @5 @6\"/><v:f eqn=\"mid @8 @5\"/><v:f eqn=\"mid @7 @8\"/><v:f eqn=\"mid @6 @7\"/><v:f eqn=\"sum @6 0 @5\"/></v:formulas><v:path textpathok=\"t\" o:connecttype=\"custom\" o:connectlocs=\"@9,0;@10,10800;@11,21600;@12,10800\" o:connectangles=\"270,180,90,0\"/><v:textpath on=\"t\" fitshape=\"t\"/><v:handles><v:h position=\"#0,bottomRight\" xrange=\"6629,14971\"/></v:handles><o:lock v:ext=\"edit\" text=\"t\" shapetype=\"t\"/></v:shapetype><v:shape id=\"PowerPlusWaterMarkObject357831064\" o:spid=\"_x0000_s2049\" type=\"#_x0000_t136\" style=\"position:absolute;margin-left:0;margin-top:0;width:412.4pt;height:247.45pt;rotation:315;z-index:-251656192;mso-position-horizontal:center;mso-position-horizontal-relative:margin;mso-position-vertical:center;mso-position-vertical-relative:margin\" fillcolor=\"${fillcolor}\" stroked=\"f\"><v:fill opacity=\".5\"/><v:textpath style=\"font-family:&quot;Calibri&quot;;font-size:1pt\" string=\"${watermark}\"/><w10:wrap anchorx=\"margin\" anchory=\"margin\"/></v:shape></w:pict>");

Re: Unmarshall exception using XmlUtils.unmarshallFromTemplate

PostPosted: Thu Nov 11, 2010 9:15 pm
by jason
You can use:

Code: Select all
XmlUtils.unmarshalString(sb.toString(), Context.jc, Pict.class);


The error message is misleading.

Re: Unmarshall exception using XmlUtils.unmarshallFromTemplate

PostPosted: Thu Nov 11, 2010 10:18 pm
by dagarfol
Yes, but I wanted to set a pair of attributes, like the text of the watermark and the text color. That's why I tried to use the unmarshallFromTemplate facility.

Re: Unmarshall exception using XmlUtils.unmarshallFromTemplate

PostPosted: Thu Nov 11, 2010 10:50 pm
by jason
OK. Well, that's much the same thing:

Code: Select all
   public static Object unmarshallFromTemplate(String wmlTemplateString,
         java.util.HashMap<String, String> mappings, JAXBContext jc) throws JAXBException {
       String wmlString = replace(wmlTemplateString, 0, new StringBuilder(), mappings).toString();
       log.debug("Results of substitution: " + wmlString);
       return unmarshalString(wmlString, jc);
    }
   
    private static StringBuilder replace(String s, int offset, StringBuilder b, java.util.HashMap<String, String> mappings) {
       int startKey = s.indexOf("${", offset);
       if (startKey == -1)
          return b.append(s.substring(offset));
       else {
          b.append(s.substring(offset, startKey));
          int keyEnd = s.indexOf('}', startKey);
          String key = s.substring(startKey + 2, keyEnd);
          b.append( mappings.get(key) );
          return replace(s, keyEnd + 1, b, mappings);
       }
    }


Just replace return unmarshalString(wmlString, jc) with what I posted earlier.

Re: Unmarshall exception using XmlUtils.unmarshallFromTemplate

PostPosted: Fri Nov 12, 2010 7:49 pm
by dagarfol
Hi Jason, I have tried what you describe, but it doesn't work either. What I have done is to extend XmlUtils, and add a new overload for unmarshallFromTemplate which includes the defined class, so that I could pass it to unmarshallString. Whit this in place, everything is working fine. The resulting code, if it may be useful, is this:
Code: Select all
   public static Object unmarshallFromTemplate(String wmlTemplateString, HashMap<String, String> mappings, JAXBContext jc, Class<?> declaredType) throws JAXBException {
      String wmlString = replace(wmlTemplateString, 0, new StringBuilder(), mappings).toString();
      return unmarshalString(wmlString, jc, declaredType);
   }

   private static StringBuilder replace(String s, int offset, StringBuilder b, java.util.HashMap<String, String> mappings) {
      int startKey = s.indexOf("${", offset);
      if (startKey == -1)
         return b.append(s.substring(offset));
      else {
         b.append(s.substring(offset, startKey));
         int keyEnd = s.indexOf('}', startKey);
         String key = s.substring(startKey + 2, keyEnd);
         b.append(mappings.get(key));
         return replace(s, keyEnd + 1, b, mappings);
      }
   }


Thank you very much for your help.

Re: Unmarshall exception using XmlUtils.unmarshallFromTemplate

PostPosted: Sat Nov 13, 2010 10:29 am
by jason
That does look like what I suggested, subject to:

- of course you had to pass the Class<?> arg, sorry I wasn't explicit about that.

- at a glance, I can't see why you'd need to extend XmlUtils for this to work though.

Anyway, glad you solved your problem.

Re: Unmarshall exception using XmlUtils.unmarshallFromTemplate

PostPosted: Mon Nov 15, 2010 8:14 pm
by dagarfol
jason wrote:- at a glance, I can't see why you'd need to extend XmlUtils for this to work though.

There is not a real need to extend XmlUtils for it to work, I just did it to be able to use the "old" XmlUtils functions along with the new one without having to import two classes.

Cheers ;)