View Javadoc

1   /*
2    * Copyright (c) 2005-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.extend.support.spring.deployment;
14  
15  import java.util.ArrayList;
16  import java.util.List;
17  
18  import org.springframework.beans.factory.BeanDefinitionStoreException;
19  import org.springframework.beans.factory.support.BeanDefinitionReader;
20  import org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader;
21  import org.w3c.dom.Element;
22  import org.w3c.dom.Node;
23  
24  /**
25   * This is xml parser that handles <depends-on> tag.
26   *
27   * @author Daniel Sendula
28   */
29  public class ApplicationContextModuleXmlParser extends DefaultBeanDefinitionDocumentReader {
30  
31      public static final String DEPENDS_ON_TAG = "depends-on";
32  
33      /** Context this parser is created for */
34      protected AbstractApplicationContextModule context;
35  
36      /**
37       * Empty constructor
38       */
39      public ApplicationContextModuleXmlParser() {
40      }
41  
42      /**
43       * This method extracts <depends-on> tags and calls modules
44       * {@link AbstractApplicationContextModule#processDependencies(List)} method with all
45       * tags (if any).
46       * @param root root
47       * @throws BeanDefinitionStoreException
48       */
49      protected void preProcessXml(Element root) throws BeanDefinitionStoreException {
50          List<Dependency> dependencies = new ArrayList<Dependency>();
51          List<Node> nodes = new ArrayList<Node>();
52          Node node = root.getFirstChild();
53          while (node != null) {
54              if (DEPENDS_ON_TAG.equals(node.getNodeName())) {
55                  int type = node.getNodeType();
56                  if (type == Node.ELEMENT_NODE) {
57                      Node c = node.getFirstChild();
58                      while ((c != null) && (c.getNodeType() != Node.TEXT_NODE)) {
59                          c = c.getNextSibling();
60                      }
61                      if (c != null) {
62                          // TODO - add processing for optional, provided and isURI attributes!
63                          String value = node.getFirstChild().getNodeValue();
64                          Dependency dependency = new Dependency();
65                          dependency.setValue(value);
66                          dependencies.add(dependency);
67                      }
68                  }
69                  nodes.add(node);
70              }
71              node = node.getNextSibling();
72          }
73          for (Node n : nodes) {
74              root.removeChild(n);
75          }
76          BeanDefinitionReader reader = getReaderContext().getReader();
77  
78          AbstractApplicationContextModule context = (AbstractApplicationContextModule)reader.getResourceLoader();
79          if (!context.processDependencies(dependencies)) {
80              // If we do not have deployed parent - we cannot proceed with any beans.
81              // We will remove them from definition and then wait to be 'created' again
82              ArrayList<Node> ns = new ArrayList<Node>();
83              Node nn = root.getFirstChild();
84              while (nn != null) {
85                  ns.add(nn);
86                  nn = nn.getNextSibling();
87              }
88              for (Node n : ns) {
89                  root.removeChild(n);
90              }
91          }
92      }
93  
94      /**
95       * Extracts value from node
96       * @param node text node
97       * @return string value
98       */
99      protected String getValue(Node node) {
100         Node c = node.getFirstChild();
101         while ((c != null) && (c.getNodeType() != Node.TEXT_NODE)) {
102             c = c.getNextSibling();
103         }
104         if (c != null) {
105             String value = node.getFirstChild().getNodeValue();
106             if (value.length() > 0) {
107                 return value;
108             }
109         }
110         return null;
111     }
112 
113 }