|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| AbstractEncoder.java | 75% | 83.3% | 100% | 82.1% |
|
||||||||||||||
| 1 | // | |
| 2 | //Copyright (c) 2003, 2004 Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 3 | //All rights reserved. | |
| 4 | // | |
| 5 | //Redistribution and use in source and binary forms, with or without modification, | |
| 6 | //are permitted provided that the following conditions are met: | |
| 7 | // | |
| 8 | //* Redistributions of source code must retain the above copyright notice, | |
| 9 | //this list of conditions and the following disclaimer. | |
| 10 | //* Redistributions in binary form must reproduce the above copyright notice, | |
| 11 | //this list of conditions and the following disclaimer in the documentation | |
| 12 | //and/or other materials provided with the distribution. | |
| 13 | //* Neither the name of the Caltha - Gajda, Krzewski, Mach, Potempski Sp.J. | |
| 14 | //nor the names of its contributors may be used to endorse or promote products | |
| 15 | //derived from this software without specific prior written permission. | |
| 16 | // | |
| 17 | //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | |
| 18 | //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
| 19 | //WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. | |
| 20 | //IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
| 21 | //INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 22 | //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, | |
| 23 | //OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, | |
| 24 | //WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
| 25 | //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
| 26 | //POSSIBILITY OF SUCH DAMAGE. | |
| 27 | // | |
| 28 | ||
| 29 | package org.objectledge.encodings; | |
| 30 | ||
| 31 | import org.objectledge.ComponentInitializationError; | |
| 32 | import org.objectledge.encodings.encoders.CharEncoder; | |
| 33 | import org.picocontainer.MutablePicoContainer; | |
| 34 | import org.picocontainer.defaults.ConstructorInjectionComponentAdapterFactory; | |
| 35 | import org.picocontainer.defaults.DefaultPicoContainer; | |
| 36 | ||
| 37 | /** | |
| 38 | * Base encoder using CharacterEncoders. | |
| 39 | * | |
| 40 | * @author <a href="mailto:dgajda@caltha.pl">Damian Gajda</a> | |
| 41 | * @version $Id: AbstractEncoder.java,v 1.2 2004/12/22 08:35:13 rafal Exp $ | |
| 42 | */ | |
| 43 | public abstract class AbstractEncoder | |
| 44 | { | |
| 45 | private static final String ENCODER_CLASS_PREFIX = | |
| 46 | "org.objectledge.encodings.encoders.CharEncoder"; | |
| 47 | private MutablePicoContainer container; | |
| 48 | ||
| 49 | /** | |
| 50 | * Constructs the base encoder component. | |
| 51 | */ | |
| 52 | 92 | public AbstractEncoder() |
| 53 | { | |
| 54 | // non caching container | |
| 55 | 92 | this.container = |
| 56 | new DefaultPicoContainer(new ConstructorInjectionComponentAdapterFactory()); | |
| 57 | 92 | CharEncoder ref1 = getCharsetEncoder("UTF-16"); |
| 58 | 92 | CharEncoder ref2 = getCharsetEncoder("UTF-16"); |
| 59 | 92 | if(ref1 == null || ref2 == null) |
| 60 | { | |
| 61 | 0 | throw new ComponentInitializationError("cannot get basic UTF-16 encoder"); |
| 62 | } | |
| 63 | 92 | if(ref1 == ref2) |
| 64 | { | |
| 65 | 0 | throw new ComponentInitializationError( |
| 66 | "container configured for component instance caching"); | |
| 67 | } | |
| 68 | } | |
| 69 | ||
| 70 | // implementation ---------------------------------------------------------------------------- | |
| 71 | ||
| 72 | /** | |
| 73 | * Returns an encoder instance for the specific encoding. | |
| 74 | * | |
| 75 | * @param encodingName the name of character encoding. | |
| 76 | * @return the encoder object. | |
| 77 | */ | |
| 78 | 460 | protected CharEncoder getCharsetEncoder(String encodingName) |
| 79 | { | |
| 80 | 460 | if(encodingName == null) |
| 81 | { | |
| 82 | 92 | return null; |
| 83 | } | |
| 84 | ||
| 85 | 368 | try |
| 86 | { | |
| 87 | 368 | encodingName = EncodingMap.getIANA2JavaMapping(encodingName); |
| 88 | 368 | Object encoderInstance = container.getComponentInstance(encodingName); |
| 89 | 368 | if(encoderInstance == null) |
| 90 | { | |
| 91 | 184 | Class clazz = Class.forName(ENCODER_CLASS_PREFIX + encodingName); |
| 92 | 184 | container.registerComponentImplementation(encodingName, clazz); |
| 93 | 184 | encoderInstance = container.getComponentInstance(encodingName); |
| 94 | } | |
| 95 | 368 | return (CharEncoder) encoderInstance; |
| 96 | } | |
| 97 | catch (ClassNotFoundException e) | |
| 98 | { | |
| 99 | 0 | throw new IllegalArgumentException( |
| 100 | "unknown or unsupported encoding '"+encodingName+"'"); | |
| 101 | } | |
| 102 | } | |
| 103 | } |
|
||||||||||