1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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
81
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
97
98 private Document createDocument() {
99 return this.documentBuilder.newDocument();
100 }
101
102
103
104
105 public void setBeanName(String name) {
106 this.name = name;
107 }
108
109 }