Original 2.4
[marc4j.git] / src / org / marc4j / converter / CharConverter.java
1 //$Id: CharConverter.java,v 1.3 2008/10/17 06:47:06 haschart Exp $\r
2 /**\r
3  * Copyright (C) 2005 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  */\r
22 package org.marc4j.converter;\r
23 \r
24 /**\r
25  * Extend this class to create a character converter.\r
26  * \r
27  * @author Bas Peters\r
28  * @version $Revision: 1.3 $\r
29  */\r
30 public abstract class CharConverter {\r
31 \r
32   /**\r
33    * The method that needs to be implemented in a subclass to create a CharConverter.\r
34    * Receives a data element extracted from a record as a array of characters, and \r
35    * converts that data and returns the result as a <code>String</code> object.\r
36    * \r
37    * @param dataElement the data to convert\r
38    * @return String the conversion result\r
39    */\r
40     public abstract String convert(char[] dataElement);\r
41     \r
42    /**\r
43     * Alternate method for performing a character conversion.  Receives the incoming\r
44     * as a byte array, converts the bytes to characters, and calls the above convert method\r
45     * which must be implemented in the subclass.\r
46     * \r
47     * @param dataElement the data to convert\r
48     * @return String the conversion result\r
49     */\r
50     final public String convert(byte[] dataElement) \r
51     {\r
52         char cData[] = new char[dataElement.length];\r
53         for (int i = 0; i < dataElement.length; i++)\r
54         {\r
55             byte b = dataElement[i];\r
56             cData[i] =  (char)(b >= 0 ? b : 256 + b);\r
57         }\r
58         return convert(cData);\r
59     }\r
60 \r
61    /**\r
62     * Alternate method for performing a character conversion.  Receives the incoming\r
63     * as a String, converts the String to a character array, and calls the above convert \r
64     * method which must be implemented in the subclass.\r
65     * \r
66     * @param dataElement the data to convert\r
67     * @return String the conversion result\r
68     */\r
69     final public String convert(String dataElement) \r
70     {\r
71         char[] data = null;\r
72         data = dataElement.toCharArray();\r
73         return (convert(data));\r
74     }\r
75     \r
76 \r
77 }