View Javadoc

1   /*
2    * File:    DomModelBuilder.java
3    * Created: 29.05.2007 7:11: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 java.util.Map;
25  
26  import net.sf.webmancer.model.AbstractModelBuilder;
27  import net.sf.webmancer.util.ContractChecker;
28  import net.sf.webmancer.util.xml.dom.DOMUtils;
29  
30  import org.w3c.dom.Document;
31  import org.w3c.dom.Element;
32  import org.w3c.dom.Node;
33  import org.w3c.dom.NodeList;
34  import org.w3c.dom.Text;
35  
36  /**
37   * @author Michal Burda
38   *
39   */
40  class DOMModelBuilder extends AbstractModelBuilder {
41  
42      /**
43       * 
44       */
45      private Document document;
46      
47      /**
48       * 
49       */
50      private Element current;
51  
52      /**
53       * Constructs the DomModelBuilder.
54       * @param document 
55       *
56       */
57      public DOMModelBuilder(Document document) {
58          ContractChecker.mustNotBeNull(document, "document");
59          ContractChecker.mustNotBeNull(document.getDocumentElement(), "document.getDocumentElement()");
60          this.document = document;
61          this.current = document.getDocumentElement();
62      }
63  
64      /**
65       * @see net.sf.webmancer.model.AbstractModelBuilder#startElement(java.lang.String, java.lang.String, java.util.Map)
66       */
67      @Override
68      public void startElement(String uri, String elementName, Map<String, String> attributes) {
69          Element element = this.document.createElementNS(uri, elementName);
70          DOMUtils.addAttributes(element, attributes);
71          this.current.appendChild(element);
72          this.current = element;
73      }
74  
75      /**
76       * @see net.sf.webmancer.model.AbstractModelBuilder#text(java.lang.String)
77       */
78      @Override
79      public void text(String textString) {
80          Text textNode = null;
81          NodeList nodelist = this.current.getChildNodes();
82          for (int i = nodelist.getLength() - 1; i >= 0; i--) {
83              Node n = nodelist.item(i);
84              if (n instanceof Text) {
85                  textNode = (Text) n;
86                  break;
87              }
88          }
89          if (textNode == null) {
90              textNode = this.document.createTextNode(textString);
91              this.current.appendChild(textNode);
92          } else {
93              textNode.appendData(textString);
94          }
95      }
96  
97      /**
98       * @see net.sf.webmancer.model.AbstractModelBuilder#endElement()
99       */
100     @Override
101     public void endElement() {
102         this.current = (Element) this.current.getParentNode();
103     }
104 
105     /**
106      * @return
107      */
108     public Object getDocument() {
109         return document;
110     }
111 
112 }