View Javadoc

1   /*
2    * File:    DomModeler.java
3    * Created: 29.05.2007 7:09:19 
4    *
5    * Copyright 2007 Michal Burda.
6    *
7    * This program is free software; you can redistribute it and/or modify
8    * it under the terms of the GNU General Public License as published by
9    * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.
11   *
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.
16   *
17   * You should have received a copy of the GNU General Public License
18   * along with this program; if not, write to the Free Software
19   * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20   */
21  
22  package net.sf.webmancer.model.dom;
23  
24  import javax.xml.parsers.DocumentBuilder;
25  import javax.xml.parsers.DocumentBuilderFactory;
26  
27  import net.sf.webmancer.model.IModelable;
28  import net.sf.webmancer.model.IModeler;
29  import net.sf.webmancer.model.ModelingException;
30  
31  import org.springframework.beans.factory.BeanNameAware;
32  import org.springframework.web.servlet.ModelAndView;
33  import org.w3c.dom.Document;
34  import org.w3c.dom.Element;
35  
36  /**
37   * @author Michal Burda
38   *
39   */
40  public class DOMModeler implements IModeler, BeanNameAware {
41      /**
42       * 
43       */
44      public static final String MODEL_ATTRIBUTE_NAME = "model";
45  
46      /**
47       * 
48       */
49      public static final String ROOT_ELEMENT_NAME = "DocRoot";
50  
51      /**
52       * 
53       */
54      public  static final String MODEL_OBJECT_NAME = "document";
55      
56      /**
57       * 
58       */
59      private String name;
60  
61      /**
62       * 
63       */
64      private DocumentBuilder documentBuilder;
65  
66      /**
67       * Constructs the DomModeler.
68       */
69      public DOMModeler() throws ModelingException {
70          super();
71          try {
72              DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
73              documentBuilder = factory.newDocumentBuilder();
74          } catch (Exception e) {
75              throw new ModelingException(e);
76          }
77      }
78  
79      /**
80       * @throws ModelingException 
81       * @see net.sf.webmancer.model.IModeler#createModel(net.sf.webmancer.model.IModelable)
82       */
83      public ModelAndView createModel(IModelable modelable) throws ModelingException {
84          ModelAndView mv = new ModelAndView(this.name);
85          Document document = createDocument();
86          Element rootElement = document.createElementNS(IModeler.WEBMANCER_NAMESPACE, ROOT_ELEMENT_NAME);
87          document.appendChild(rootElement);
88          rootElement.setAttribute(MODEL_ATTRIBUTE_NAME, this.name);
89          DOMModelBuilder builder = new DOMModelBuilder(document);
90          modelable.build(builder);
91          mv.addObject(MODEL_OBJECT_NAME, builder.getDocument());
92          return mv;
93      }
94  
95      /**
96       * @return
97       */
98      private Document createDocument() {
99          return this.documentBuilder.newDocument();
100     }
101 
102     /**
103      * @see org.springframework.beans.factory.BeanNameAware#setBeanName(java.lang.String)
104      */
105     public void setBeanName(String name) {
106         this.name = name;
107     }
108 
109 }