KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > Menu


1 /*
2   Copyright (C) 2003-2005 Know Gate S.L. All rights reserved.
3                            C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipergate;
34
35 import java.util.ArrayList JavaDoc;
36
37 import java.io.IOException JavaDoc;
38 import java.io.FileNotFoundException JavaDoc;
39 import java.io.UnsupportedEncodingException JavaDoc;
40 import java.io.FileInputStream JavaDoc;
41 import java.io.BufferedInputStream JavaDoc;
42
43 import org.jibx.runtime.IBindingFactory;
44 import org.jibx.runtime.IUnmarshallingContext;
45 import org.jibx.runtime.BindingDirectory;
46 import org.jibx.runtime.JiBXException;
47
48 import com.knowgate.debug.DebugFile;
49
50 /**
51  * <p>hipergate top menu definition JiBX handler</p>
52  * This class needs to be processed with JiBX after being compiled.
53  * If you get the error org.jibx.runtime.JiBXException:
54  * Unable to access binding information for class com.knowgate.hipergate.Menu
55  * whilst trying to execute hipergate then
56  * execute from the command line:
57  * C:\JRE\bin\java -cp C:\JAR\bcel.jar;C:\JAR\jibx-1beta3.jar;C:\JAR\jibx-extras.jar;C:\JAR\xpp3.jar org.jibx.binding.Compile C:\knowgate\storage\xslt\schemas\menu-def-jixb.xml
58  * @author Sergio Montoro Ten
59  * @version 1.0
60  */

61 public class Menu {
62   private ArrayList JavaDoc options;
63
64   // ---------------------------------------------------------------------------
65

66   /**
67    * Default constructor
68    */

69   public Menu() {
70     options = new ArrayList JavaDoc();
71   }
72
73   // ---------------------------------------------------------------------------
74

75   /**
76    * Number of top level options
77    * @return int
78    */

79   public int countSubOptions() {
80     return options.size();
81   }
82
83   // ---------------------------------------------------------------------------
84

85   /**
86    * Get option by index
87    * @param nIndex int Option Index
88    * @return MenuOption
89    * @throws ArrayIndexOutOfBoundsException if n<0 or n>=countSubOptions()
90    */

91   public MenuOption getOption(int nIndex)
92     throws ArrayIndexOutOfBoundsException JavaDoc {
93     return (MenuOption) options.get(nIndex);
94   }
95
96   // ---------------------------------------------------------------------------
97

98   /**
99    * <p>Get option by index</p>
100    * Perform linear search on top level options
101    * @param sName String Option name
102    * @return MenuOption
103    * @throws ArrayIndexOutOfBoundsException if no option with such name was found
104    */

105   public MenuOption getOption(String JavaDoc sName)
106     throws ArrayIndexOutOfBoundsException JavaDoc {
107     final int nOptCount = countSubOptions();
108     for (int o=0;o<nOptCount;o++) {
109       if (((MenuOption)options.get(o)).getName().equalsIgnoreCase(sName))
110         return (MenuOption) options.get(o);
111     } // next
112
throw new ArrayIndexOutOfBoundsException JavaDoc(sName + " menu option not found");
113   } // getOption
114

115   // ---------------------------------------------------------------------------
116

117   /**
118    * Create a Menu object by parsing its definition from an XML file
119    * @param sXMLDocPath String Full path to menu XML definition file
120    * @param sEnc String Character encoding, if <b>null</b> then UTF-8 is assumed.
121    * @return Menu object
122    * @throws JiBXException
123    * @throws FileNotFoundException
124    * @throws UnsupportedEncodingException
125    * @throws IOException
126    */

127   public static Menu parse(String JavaDoc sXMLDocPath, String JavaDoc sEnc)
128     throws JiBXException, FileNotFoundException JavaDoc, UnsupportedEncodingException JavaDoc,
129            IOException JavaDoc {
130
131     if (DebugFile.trace) {
132       DebugFile.writeln("Begin Menu.parse("+sXMLDocPath+","+sEnc+")");
133       DebugFile.incIdent();
134     }
135
136     if (sEnc==null) sEnc="UTF-8";
137
138     IBindingFactory bfact = BindingDirectory.getFactory(Menu.class);
139     IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
140
141     final int BUFFER_SIZE = 28000;
142     FileInputStream JavaDoc oFileStream = new FileInputStream JavaDoc(sXMLDocPath);
143     BufferedInputStream JavaDoc oXMLStream = new BufferedInputStream JavaDoc(oFileStream, BUFFER_SIZE);
144
145     Object JavaDoc obj = uctx.unmarshalDocument (oXMLStream, sEnc);
146
147     oXMLStream.close();
148     oFileStream.close();
149
150     if (DebugFile.trace) {
151       DebugFile.decIdent();
152       DebugFile.writeln("End Menu.parse()");
153     }
154
155     return (Menu) obj;
156   } // parse
157
}
158
Popular Tags