1 //$Id: MarcFactory.java,v 1.2 2006/07/28 16:29:01 bpeters Exp $
\r
3 * Copyright (C) 2004 Bas Peters
\r
5 * This file is part of MARC4J
\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
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
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
21 package org.marc4j.marc;
\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
32 * Factory for creating MARC record objects.
\r
34 * You can use <code>MarcFactory</code> to create records from scratch:
\r
38 * MarcFactory factory = MarcFactory.newInstance();
\r
39 * Record record = factory.newRecord();
\r
40 * ControlField cf = factory.newControlField("001");
\r
41 * record.addVariableField(cf);
\r
46 * @author Bas Peters
\r
47 * @version $Revision: 1.2 $
\r
49 public abstract class MarcFactory {
\r
51 protected MarcFactory() {
\r
55 * Creates a new factory instance. The implementation class to load is the
\r
56 * first found in the following locations:
\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
64 * <li>the default factory class</li>
\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
72 String className = null;
\r
75 className = getFactoryClassName(loader, count++);
\r
76 if (className != null) {
\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
84 } catch (Exception e) {
\r
87 } while (className == null && count < 3);
\r
88 return new org.marc4j.marc.impl.MarcFactoryImpl();
\r
91 private static String getFactoryClassName(ClassLoader loader, int attempt) {
\r
92 final String propertyName = "org.marc4j.marc.MarcFactory";
\r
95 return System.getProperty(propertyName);
\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
105 return props.getProperty(propertyName);
\r
106 } catch (IOException e) {
\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
116 BufferedReader r = new BufferedReader(new InputStreamReader(in));
\r
117 String ret = r.readLine();
\r
121 } catch (IOException e) {
\r
130 * Returns a new control field instance.
\r
132 * @return ControlField
\r
134 public abstract ControlField newControlField();
\r
137 * Creates a new control field with the given tag and returns the instance.
\r
139 * @return ControlField
\r
141 public abstract ControlField newControlField(String tag);
\r
144 * Creates a new control field with the given tag and data and returns the
\r
147 * @return ControlField
\r
149 public abstract ControlField newControlField(String tag, String data);
\r
152 * Returns a new data field instance.
\r
154 * @return DataField
\r
156 public abstract DataField newDataField();
\r
159 * Creates a new data field with the given tag and indicators and returns the
\r
162 * @return DataField
\r
164 public abstract DataField newDataField(String tag, char ind1, char ind2);
\r
167 * Returns a new leader instance.
\r
171 public abstract Leader newLeader();
\r
174 * Creates a new leader with the given <code>String</code> object.
\r
178 public abstract Leader newLeader(String ldr);
\r
181 * Returns a new record instance.
\r
185 public abstract Record newRecord();
\r
188 * Returns a new record instance.
\r
192 public abstract Record newRecord(Leader leader);
\r
195 * Returns a new record instance.
\r
199 public abstract Record newRecord(String leader);
\r
202 * Returns a new subfield instance.
\r
206 public abstract Subfield newSubfield();
\r
209 * Creates a new subfield with the given identifier.
\r
213 public abstract Subfield newSubfield(char code);
\r
216 * Creates a new subfield with the given identifier and data.
\r
220 public abstract Subfield newSubfield(char code, String data);
\r