KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ungoverned > oscar > LibraryInfo


1 /*
2  * Oscar - An implementation of the OSGi framework.
3  * Copyright (c) 2004, Richard S. Hall
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * * Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  * * Neither the name of the ungoverned.org nor the names of its
17  * contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * Contact: Richard S. Hall (heavy@ungoverned.org)
33  * Contributor(s):
34  *
35 **/

36 package org.ungoverned.oscar;
37
38 import java.util.ArrayList JavaDoc;
39 import java.util.List JavaDoc;
40 import java.util.StringTokenizer JavaDoc;
41
42 import org.osgi.framework.Constants;
43
44 public class LibraryInfo
45 {
46     private String JavaDoc m_name = null;
47     private String JavaDoc[] m_osnames = null;
48     private String JavaDoc[] m_osversions = null;
49     private String JavaDoc[] m_processors = null;
50     private String JavaDoc[] m_languages = null;
51
52     public LibraryInfo(String JavaDoc name, String JavaDoc[] osnames, String JavaDoc[] osversions,
53         String JavaDoc[] processors, String JavaDoc[] languages)
54     {
55         m_name = name;
56         m_osnames = osnames;
57         m_osversions = osversions;
58         m_processors = processors;
59         m_languages = languages;
60     }
61
62     public LibraryInfo(LibraryInfo library)
63     {
64         m_name = library.m_name;
65         m_osnames = library.m_osnames;
66         m_osversions = library.m_osversions;
67         m_processors = library.m_processors;
68         m_languages = library.m_languages;
69     }
70
71     public String JavaDoc getName()
72     {
73         return m_name;
74     }
75
76     public String JavaDoc[] getOSNames()
77     {
78         return m_osnames;
79     }
80
81     public String JavaDoc[] getOSVersions()
82     {
83         return m_osversions;
84     }
85
86     public String JavaDoc[] getProcessors()
87     {
88         return m_processors;
89     }
90
91     public static LibraryInfo[] parse(String JavaDoc s)
92     {
93         try
94         {
95             if ((s == null) || (s.length() == 0))
96             {
97                 return null;
98             }
99
100             // The tokens are separated by semicolons and may include
101
// any number of libraries (whose name starts with a "/")
102
// along with one set of associated properties.
103
StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(s, ";");
104             String JavaDoc[] libs = new String JavaDoc[st.countTokens()];
105             List JavaDoc osNameList = new ArrayList JavaDoc();
106             List JavaDoc osVersionList = new ArrayList JavaDoc();
107             List JavaDoc processorList = new ArrayList JavaDoc();
108             List JavaDoc languageList = new ArrayList JavaDoc();
109             int libCount = 0;
110             while (st.hasMoreTokens())
111             {
112                 String JavaDoc token = st.nextToken().trim();
113                 if (token.indexOf('=') < 0)
114                 {
115                     // Remove the slash, if necessary.
116
libs[libCount] = (token.charAt(0) == '/')
117                         ? token.substring(1)
118                         : token;
119                     libCount++;
120                 }
121                 else
122                 {
123                     // Check for valid native library properties; defined as
124
// a property name, an equal sign, and a value.
125
StringTokenizer JavaDoc stProp = new StringTokenizer JavaDoc(token, "=");
126                     if (stProp.countTokens() != 2)
127                     {
128                         throw new IllegalArgumentException JavaDoc(
129                             "Bundle manifest native library entry malformed: " + token);
130                     }
131                     String JavaDoc property = stProp.nextToken().trim().toLowerCase();
132                     String JavaDoc value = stProp.nextToken().trim();
133                     
134                     // Values may be quoted, so remove quotes if present.
135
if (value.charAt(0) == '"')
136                     {
137                         // This should always be true, otherwise the
138
// value wouldn't be properly quoted, but we
139
// will check for safety.
140
if (value.charAt(value.length() - 1) == '"')
141                         {
142                             value = value.substring(1, value.length() - 1);
143                         }
144                         else
145                         {
146                             value = value.substring(1);
147                         }
148                     }
149                     // Add the value to its corresponding property list.
150
if (property.equals(Constants.BUNDLE_NATIVECODE_OSNAME))
151                     {
152                         osNameList.add(value);
153                     }
154                     else if (property.equals(Constants.BUNDLE_NATIVECODE_OSVERSION))
155                     {
156                         osVersionList.add(value);
157                     }
158                     else if (property.equals(Constants.BUNDLE_NATIVECODE_PROCESSOR))
159                     {
160                         processorList.add(value);
161                     }
162                     else if (property.equals(Constants.BUNDLE_NATIVECODE_LANGUAGE))
163                     {
164                         languageList.add(value);
165                     }
166                 }
167             }
168
169             if (libCount == 0)
170             {
171                 return null;
172             }
173
174             LibraryInfo[] libraries = new LibraryInfo[libCount];
175             for (int i = 0; i < libCount; i++)
176             {
177                 libraries[i] =
178                     new LibraryInfo(
179                         libs[i],
180                         (String JavaDoc[]) osNameList.toArray(new String JavaDoc[0]),
181                         (String JavaDoc[]) osVersionList.toArray(new String JavaDoc[0]),
182                         (String JavaDoc[]) processorList.toArray(new String JavaDoc[0]),
183                         (String JavaDoc[]) languageList.toArray(new String JavaDoc[0]));
184             }
185
186             return libraries;
187
188         }
189         catch (RuntimeException JavaDoc ex)
190         {
191             ex.printStackTrace();
192             throw ex;
193         }
194     }
195
196     public String JavaDoc toString()
197     {
198         return m_name;
199     }
200 }
201
Popular Tags