View Javadoc

1   /***
2   * Copyright 2004 The Apache Software Foundation.
3   * 
4   * Licensed under the Apache License, Version 2.0 (the "License");
5   * you may not use this file except in compliance with the License.
6   * You may obtain a copy of the License at
7   * 
8   *      http://www.apache.org/licenses/LICENSE-2.0
9   * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16  package org.apache.commons.jelly.tags.jaxme;
17  
18  import java.io.File;
19  import java.io.IOException;
20  
21  import javax.xml.bind.UnmarshallerHandler;
22  import javax.xml.bind.Unmarshaller;
23  import javax.xml.bind.JAXBException;
24  import javax.xml.bind.JAXBContext;
25  
26  import org.apache.commons.jelly.TagSupport;
27  import org.apache.commons.jelly.XMLOutput;
28  import org.apache.commons.jelly.MissingAttributeException;
29  import org.apache.commons.jelly.JellyTagException;
30  
31  import org.apache.ws.jaxme.generator.Generator;
32  import org.apache.ws.jaxme.generator.SchemaReader;
33  import org.apache.ws.jaxme.generator.sg.impl.JAXBSchemaReader;
34  import org.apache.ws.jaxme.generator.sg.SchemaSG;
35  import org.apache.ws.jaxme.generator.impl.GeneratorImpl;
36  
37  import org.xml.sax.SAXException;
38  
39  /*** 
40   * <p>Unmarshalls xml documents into java objects.</p>
41   * <p>
42   * This tag unmarshalls the xml content contained 
43   * into the JaxMe generated java objects in the packages specified.
44   * </p>
45   *
46   * @author <a href="mailto:joe@ispsoft.de">Jochen Wiedmann</a>
47   * @author <a href="mailto:commons-dev at jakarta.apache.org">Apache Commons Development Team</a>
48   * @version $Revision: 155420 $
49   */
50  public class UnmarshallTag extends TagSupport {
51         
52      private String packages;
53      private String var;
54      
55      public String getPackages() {
56          return packages;
57      }
58      
59      /***
60       * Defines the generated objects to which the xml should be unmarshalled. 
61       */
62      public void setPackages(String packages) {
63          this.packages = packages;
64      }
65      
66      public String getVar() {
67          return var;
68      }
69      
70      /***
71       * Sets the name of the jelly variable to which 
72       * the unmarshalled java object should be bound.
73       */
74      public void setVar(String var) {
75          this.var = var;
76      }
77      
78      public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
79          if (packages == null) {
80              throw new MissingAttributeException( "packages" );
81          }
82          if ( var == null ) {
83              throw new MissingAttributeException( "var" );
84          }
85          try {   
86  
87              JAXBContext jaxbContext = JAXBContext.newInstance(packages);
88              Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
89              UnmarshallerHandler handler = unmarshaller.getUnmarshallerHandler();
90              
91              handler.startDocument();
92              
93              XMLOutput newOutput = new XMLOutput( handler );
94              
95              invokeBody(newOutput);
96              handler.endDocument();
97              
98              Object result = handler.getResult();
99              context.setVariable( var, result );
100             
101         } catch (JAXBException ex)  {
102             throw new JellyTagException(ex);
103         } catch (SAXException ex) {
104             throw new JellyTagException(ex);
105         }
106         
107     }
108 }