View Javadoc

1   // ========================================================================
2   // Copyright (c) 2006-2010 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.webapp;
15  
16  import java.util.ArrayList;
17  import java.util.Collections;
18  import java.util.Iterator;
19  import java.util.List;
20  
21  import org.eclipse.jetty.util.resource.Resource;
22  import org.eclipse.jetty.xml.XmlParser;
23  
24  
25  /**
26   * Fragment
27   *
28   * A web-fragment.xml descriptor.
29   */
30  public class FragmentDescriptor extends WebDescriptor
31  {
32      public static final String NAMELESS = "@@-NAMELESS-@@"; //prefix for nameless Fragments
33      public enum OtherType {None, Before, After};
34      
35      protected int _counter = 0;
36      protected OtherType _otherType = OtherType.None;
37     
38      
39      protected List<String> _befores = new ArrayList<String>();
40      protected List<String> _afters = new ArrayList<String>();
41      protected String _name;
42      
43  
44      public FragmentDescriptor (Resource xml)
45          throws Exception
46      {
47          super (xml);
48      }       
49      
50      public String getName ()
51      {
52          return _name;
53      }
54      
55      @Override
56      public void parse () 
57          throws Exception
58      {
59          super.parse();
60          processName();
61      }
62      
63      public void processName ()
64      {
65          XmlParser.Node root = getRoot();
66          XmlParser.Node nameNode = root.get("name");
67          _name = NAMELESS+(_counter++);
68          if (nameNode != null)
69          {
70              String tmp = nameNode.toString(false,true);
71              if (tmp!=null && tmp.length()>0)
72                  _name = tmp;
73          }
74      }
75      @Override
76      public void processOrdering ()
77      {
78          //Process a fragment jar's web-fragment.xml<ordering> elements
79          XmlParser.Node root = getRoot();       
80          
81          XmlParser.Node ordering = root.get("ordering");
82          if (ordering == null)
83              return; //No ordering for this fragment
84          
85          _isOrdered = true;
86     
87          processBefores(ordering);
88          processAfters(ordering);
89      }
90      
91      
92      public void processBefores (XmlParser.Node ordering)
93      {
94          //Process the <before> elements, looking for an <others/> clause and all of the <name> clauses
95          XmlParser.Node before = ordering.get("before");
96          if (before == null)
97              return;
98  
99          Iterator iter = before.iterator();
100         XmlParser.Node node = null;
101         while (iter.hasNext())
102         {
103             Object o = iter.next();
104             if (!(o instanceof XmlParser.Node)) continue;
105             node = (XmlParser.Node) o;
106             if (node.getTag().equalsIgnoreCase("others"))
107             {
108                 if (_otherType != OtherType.None)
109                     throw new IllegalStateException("Duplicate <other> clause detected in "+_xml.getURI());
110 
111                 _otherType = OtherType.Before;
112             }
113             else if (node.getTag().equalsIgnoreCase("name"))
114                 _befores.add(node.toString(false,true));
115         }
116     }
117 
118     public void processAfters (XmlParser.Node ordering)
119     {
120         //Process the <after> elements, look for an <others/> clause and all of the <name/> clauses
121         XmlParser.Node after = ordering.get("after");
122         if (after == null)
123             return;
124         
125         Iterator iter = after.iterator();
126         XmlParser.Node node = null;
127         while (iter.hasNext())
128         {
129             Object o = iter.next();
130             if (!(o instanceof XmlParser.Node)) continue;
131             node = (XmlParser.Node) o;
132             if (node.getTag().equalsIgnoreCase("others"))
133             {
134                 if (_otherType != OtherType.After)
135                     throw new IllegalStateException("Duplicate <other> clause detected in "+_xml.getURI());
136 
137                 _otherType = OtherType.After;
138 
139             }
140             else if (node.getTag().equalsIgnoreCase("name"))
141                 _afters.add(node.toString(false,true));
142         }
143     }
144     
145     public List<String> getBefores()
146     {
147         return Collections.unmodifiableList(_befores);
148     }
149     
150     public List<String> getAfters()
151     {
152         return Collections.unmodifiableList(_afters);
153     }
154     
155     public OtherType getOtherType ()
156     {
157         return _otherType;
158     }
159     
160     public List<String> getOrdering()
161     {
162         return null; //only used for absolute-ordering in Descriptor
163     }
164 }