View Javadoc

1   package edu.asu.cri.MirkE.menu; 
2   
3   import java.io.File;
4   import java.io.FileInputStream;
5   import java.io.InputStream;
6   import java.net.URL;
7   
8   import javax.xml.parsers.DocumentBuilder;
9   import javax.xml.parsers.DocumentBuilderFactory;
10  
11  import org.w3c.dom.Document;
12  import org.w3c.dom.Element;
13  import org.w3c.dom.NodeList;
14  
15  import edu.asu.cri.MirkE.exceptions.MenuException;
16  import edu.asu.cri.MirkE.util.MirkeMessages;
17  import edu.asu.cri.MirkE.util.MirkeProperties;
18  
19  /***
20   * This class parses the menu.xml file of mirke
21   * 
22   * @author Siva B.S.D.S
23   * @version $Id: MenuXMLParser.java,v 1.6 2005/02/22 16:35:18 smenor Exp $
24   */
25  public class MenuXMLParser
26  {
27      /***
28       * Returns the menu list by parsing the menu xml file that is configured in
29       * mirke.properties file
30       *  
31       * @return Menu list
32       * @throws MenuException
33       */
34      public static MenuList getMenuList() throws MenuException
35      {
36          MenuList menuList = null;
37          String menuFileName = null;
38          URL menuFileUrl = null;
39          try
40          {
41              menuFileName = MirkeProperties.getProperty("menu.filename");
42              menuFileUrl =
43                  (new Thread()).getContextClassLoader().getResource(
44                          menuFileName);
45              InputStream menuFileInputStream = menuFileUrl.openStream();
46              
47              menuList = getMenuList(menuFileInputStream);
48          }
49          catch (MenuException exception)
50          {
51              throw exception;
52          }
53          catch (Throwable exception)
54          {
55              throw new MenuException(exception);
56          }
57          return menuList;
58      }
59      
60      /***
61       * Returns menu list by parsing the given file
62       * 
63       * @param menuXmlFile Menu xml file
64       * 
65       * @return Menu list
66       * @throws MenuException
67       */
68      private static MenuList getMenuList(File menuXmlFile)
69      throws MenuException
70      {
71          MenuList menuList = null;
72          try 
73          {
74              InputStream xmlInputStream = new FileInputStream(menuXmlFile);
75              menuList = getMenuList(xmlInputStream);
76          } 
77          
78          catch (Throwable exception)
79          {
80              throw new MenuException(exception);
81          }
82          
83          return menuList;
84      }
85      
86      private static MenuList getMenuList(InputStream menuXmlInputStream) 
87      throws MenuException 
88      {
89  
90          DocumentBuilder builder = null;
91          MenuList menuList = null;
92          
93          try
94          {
95              //Creating the xml document object
96              builder =
97                  DocumentBuilderFactory.newInstance().newDocumentBuilder();
98              //Parsing the menu xml document
99              Document document = builder.parse(menuXmlInputStream);
100             menuList = getMenuList(document.getDocumentElement());
101         }
102         
103         catch (MenuException exception)
104         {
105             throw exception;
106         }
107         catch (Throwable exception)
108         {
109             throw new MenuException(exception);
110         }
111         
112         return menuList;
113     }
114     
115     /***
116      * Returns menu list by parsing the given xml element recursively
117      * 
118      * @param menuElement Menu element
119      * @return Menu list
120      * @throws MenuException
121      */
122     private static MenuList getMenuList(Element menuElement)
123     throws MenuException
124     {
125         MenuList menuList = null;
126         NodeList nodeList = null;
127         int nodeLength = 0;
128         int nodeIndex = 0;
129         Element childElement = null;
130         MenuItem menuItem = null;
131         String nameKey = null;
132         try
133         {
134             menuList = new MenuList();
135             
136             //Setting the attributes of menu list
137             nameKey = menuElement.getAttribute("name-key");
138             menuList.setAccessKeyStroke(
139                     menuElement.getAttribute("access-key-stroke"));
140             if (nameKey != null && !nameKey.equals(""))
141             {
142                 menuList.setNameKey(nameKey);
143                 menuList.setName(MirkeMessages.getMessage(nameKey));
144             }
145             
146             //Parsing the child elements
147             nodeList = menuElement.getChildNodes();
148             if (nodeList != null)
149             {
150                 nodeLength = nodeList.getLength();
151                 //Navigating through each menu item and creating the vo
152                 for (nodeIndex = 0; nodeIndex < nodeLength; nodeIndex++)
153                 {
154                     if (nodeList.item(nodeIndex) instanceof Element)
155                     {
156                         childElement = (Element) nodeList.item(nodeIndex);
157                         //Calling the same method recursively if the element is
158                         //menu list item
159                         if (childElement.getNodeName().equals("menu-list"))
160                         {
161                             menuList.addChildItem(getMenuList(childElement));
162                         }
163                         //Creating the menu item object if the element is menu 
164                         //item
165                         else if (
166                                 childElement.getNodeName().equals("menu-item"))
167                         {
168                             menuItem = getMenuItem(childElement);
169                             menuList.addChildItem(menuItem);
170                         }
171                         else
172                         {
173                             throw new MenuException(
174                                     MirkeMessages.getMessage("invalid.menuxml")
175                                     + childElement.getNodeName());
176                         }
177                     }
178                 }
179             }
180         }
181         catch (MenuException exception)
182         {
183             throw exception;
184         }
185         catch (Throwable exception)
186         {
187             throw new MenuException(exception);
188         }
189         return menuList;
190     }
191     
192     /***
193      * Returns menu Item by parsing the given menu item xml element
194      *  
195      * @param itemElement Menu item xml element
196      * @return Menu item
197      * @throws MenuException
198      */
199     private static MenuItem getMenuItem(Element itemElement)
200     throws MenuException
201     {
202         MenuItem menuItem = null;
203         Element actionElement = null;
204         String nameKey = null;
205         try
206         {
207             menuItem = new MenuItem();
208             //Setting the attributes of menu list
209             nameKey = itemElement.getAttribute("name-key");
210             if (nameKey != null && !nameKey.equals(""))
211             {
212                 menuItem.setNameKey(nameKey);
213                 menuItem.setName(MirkeMessages.getMessage(nameKey));
214             }
215             menuItem.setAccessKeyStroke(
216                     itemElement.getAttribute("access-key-stroke"));
217             
218             //Getting action element
219             actionElement =
220                 (Element) itemElement.getElementsByTagName("action").item(0);
221             if (actionElement != null)
222             {
223                 menuItem.setClassName(
224                         actionElement.getAttribute("class-name"));
225                 menuItem.setMethodName(
226                         actionElement.getAttribute("method-name"));
227             }
228         }
229         catch (Throwable exception)
230         {
231             throw new MenuException(exception);
232         }
233         return menuItem;
234     }
235 }