Make List type safe. Remove/suppress all warnings.
[marc4j.git] / src / org / marc4j / marc / MarcFactory.java
1 //$Id: MarcFactory.java,v 1.2 2006/07/28 16:29:01 bpeters Exp $\r
2 /**\r
3  * Copyright (C) 2004 Bas Peters\r
4  *\r
5  * This file is part of MARC4J\r
6  *\r
7  * MARC4J is free software; you can redistribute it and/or\r
8  * modify it under the terms of the GNU Lesser General Public \r
9  * License as published by the Free Software Foundation; either \r
10  * version 2.1 of the License, or (at your option) any later version.\r
11  *\r
12  * MARC4J is distributed in the hope that it will be useful,\r
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
15  * Lesser General Public License for more details.\r
16  *\r
17  * You should have received a copy of the GNU Lesser General Public \r
18  * License along with MARC4J; if not, write to the Free Software\r
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
20  */\r
21 package org.marc4j.marc;\r
22 \r
23 import java.io.BufferedReader;\r
24 import java.io.File;\r
25 import java.io.FileInputStream;\r
26 import java.io.IOException;\r
27 import java.io.InputStream;\r
28 import java.io.InputStreamReader;\r
29 import java.util.Properties;\r
30 \r
31 /**\r
32  * Factory for creating MARC record objects.\r
33  * <p>\r
34  * You can use <code>MarcFactory</code> to create records from scratch:\r
35  * \r
36  * <pre>\r
37  * \r
38  *  MarcFactory factory = MarcFactory.newInstance();\r
39  *  Record record = factory.newRecord();\r
40  *  ControlField cf = factory.newControlField(&quot;001&quot;);\r
41  *  record.addVariableField(cf);\r
42  *  etc...\r
43  *  \r
44  * </pre>\r
45  * \r
46  * @author Bas Peters\r
47  * @version $Revision: 1.2 $\r
48  */\r
49 public abstract class MarcFactory {\r
50 \r
51   protected MarcFactory() {\r
52   }\r
53 \r
54   /**\r
55    * Creates a new factory instance. The implementation class to load is the\r
56    * first found in the following locations:\r
57    * <ol>\r
58    * <li>the <code>org.marc4j.marc.MarcFactory</code> system property</li>\r
59    * <li>the above named property value in the\r
60    * <code><i>$JAVA_HOME</i>/lib/marc4j.properties</code> file</li>\r
61    * <li>the class name specified in the\r
62    * <code>META-INF/services/org.marc4j.marc.MarcFactory</code> system\r
63    * resource</li>\r
64    * <li>the default factory class</li>\r
65    * </ol>\r
66    */\r
67   public static MarcFactory newInstance() {\r
68     ClassLoader loader = Thread.currentThread().getContextClassLoader();\r
69     if (loader == null) {\r
70       loader = MarcFactory.class.getClassLoader();\r
71     }\r
72     String className = null;\r
73     int count = 0;\r
74     do {\r
75       className = getFactoryClassName(loader, count++);\r
76       if (className != null) {\r
77         try {\r
78           @SuppressWarnings("rawtypes")\r
79           Class t = (loader != null) ? loader.loadClass(className) : Class\r
80               .forName(className);\r
81           return (MarcFactory) t.newInstance();\r
82         } catch (ClassNotFoundException e) {\r
83           className = null;\r
84         } catch (Exception e) {\r
85         }\r
86       }\r
87     } while (className == null && count < 3);\r
88     return new org.marc4j.marc.impl.MarcFactoryImpl();\r
89   }\r
90 \r
91   private static String getFactoryClassName(ClassLoader loader, int attempt) {\r
92     final String propertyName = "org.marc4j.marc.MarcFactory";\r
93     switch (attempt) {\r
94     case 0:\r
95       return System.getProperty(propertyName);\r
96     case 1:\r
97       try {\r
98         File file = new File(System.getProperty("java.home"));\r
99         file = new File(file, "lib");\r
100         file = new File(file, "marc4j.properties");\r
101         InputStream in = new FileInputStream(file);\r
102         Properties props = new Properties();\r
103         props.load(in);\r
104         in.close();\r
105         return props.getProperty(propertyName);\r
106       } catch (IOException e) {\r
107         return null;\r
108       }\r
109     case 2:\r
110       try {\r
111         String serviceKey = "/META-INF/services/" + propertyName;\r
112         InputStream in = (loader != null) ? loader\r
113             .getResourceAsStream(serviceKey) : MarcFactory.class\r
114             .getResourceAsStream(serviceKey);\r
115         if (in != null) {\r
116           BufferedReader r = new BufferedReader(new InputStreamReader(in));\r
117           String ret = r.readLine();\r
118           r.close();\r
119           return ret;\r
120         }\r
121       } catch (IOException e) {\r
122       }\r
123       return null;\r
124     default:\r
125       return null;\r
126     }\r
127   }\r
128 \r
129   /**\r
130    * Returns a new control field instance.\r
131    * \r
132    * @return ControlField\r
133    */\r
134   public abstract ControlField newControlField();\r
135 \r
136   /**\r
137    * Creates a new control field with the given tag and returns the instance.\r
138    * \r
139    * @return ControlField\r
140    */\r
141   public abstract ControlField newControlField(String tag);\r
142 \r
143   /**\r
144    * Creates a new control field with the given tag and data and returns the\r
145    * instance.\r
146    * \r
147    * @return ControlField\r
148    */\r
149   public abstract ControlField newControlField(String tag, String data);\r
150 \r
151   /**\r
152    * Returns a new data field instance.\r
153    * \r
154    * @return DataField\r
155    */\r
156   public abstract DataField newDataField();\r
157 \r
158   /**\r
159    * Creates a new data field with the given tag and indicators and returns the\r
160    * instance.\r
161    * \r
162    * @return DataField\r
163    */\r
164   public abstract DataField newDataField(String tag, char ind1, char ind2);\r
165 \r
166   /**\r
167    * Returns a new leader instance.\r
168    * \r
169    * @return Leader\r
170    */\r
171   public abstract Leader newLeader();\r
172 \r
173   /**\r
174    * Creates a new leader with the given <code>String</code> object.\r
175    * \r
176    * @return Leader\r
177    */\r
178   public abstract Leader newLeader(String ldr);\r
179 \r
180   /**\r
181    * Returns a new record instance.\r
182    * \r
183    * @return Record\r
184    */\r
185   public abstract Record newRecord();\r
186 \r
187   /**\r
188    * Returns a new record instance.\r
189    * \r
190    * @return Record\r
191    */\r
192   public abstract Record newRecord(Leader leader);\r
193     \r
194   /**\r
195    * Returns a new record instance.\r
196    * \r
197    * @return Record\r
198    */\r
199   public abstract Record newRecord(String leader);\r
200   \r
201   /**\r
202    * Returns a new subfield instance.\r
203    * \r
204    * @return Leader\r
205    */\r
206   public abstract Subfield newSubfield();\r
207 \r
208   /**\r
209    * Creates a new subfield with the given identifier.\r
210    * \r
211    * @return Subfield\r
212    */\r
213   public abstract Subfield newSubfield(char code);\r
214 \r
215   /**\r
216    * Creates a new subfield with the given identifier and data.\r
217    * \r
218    * @return Subfield\r
219    */\r
220   public abstract Subfield newSubfield(char code, String data);\r
221 \r
222 }