|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| I18nViewLookupSequence.java | 100% | 82.4% | 60% | 80.8% |
|
||||||||||||||
| 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 | package org.objectledge.web.mvc.finders; | |
| 29 | ||
| 30 | import java.util.ArrayList; | |
| 31 | import java.util.Iterator; | |
| 32 | import java.util.List; | |
| 33 | import java.util.NoSuchElementException; | |
| 34 | ||
| 35 | import org.objectledge.i18n.I18n; | |
| 36 | import org.objectledge.i18n.I18nContext; | |
| 37 | ||
| 38 | /** | |
| 39 | * A view sequence that appends localization dependent suffixes. | |
| 40 | * | |
| 41 | * @author <a href="mailto:rafal@caltha.pl">Rafal Krzewski</a> | |
| 42 | * @version $Id: I18nViewLookupSequence.java,v 1.1 2005/02/21 17:47:49 rafal Exp $ | |
| 43 | */ | |
| 44 | public class I18nViewLookupSequence | |
| 45 | implements Sequence | |
| 46 | { | |
| 47 | private final Sequence viewLookupSequence; | |
| 48 | private final List<String> suffices = new ArrayList<String>(3); | |
| 49 | private Iterator<String> iterator; | |
| 50 | private String lastPrefix; | |
| 51 | private final StringBuilder buff = new StringBuilder(); | |
| 52 | ||
| 53 | /** | |
| 54 | * Creates new LocalizedViewLookupSequence instance. | |
| 55 | * | |
| 56 | * @param viewLookupSequence the view lookup sequence. | |
| 57 | * @param i18n the I18n component, for determining default locale. | |
| 58 | * @param i18nContext the I18n context for determining requested locale. | |
| 59 | */ | |
| 60 | 46 | public I18nViewLookupSequence(Sequence viewLookupSequence, I18n i18n, |
| 61 | I18nContext i18nContext) | |
| 62 | { | |
| 63 | 46 | this.viewLookupSequence = viewLookupSequence; |
| 64 | 46 | suffices.add(i18nContext.getLocale().toString()); |
| 65 | 46 | suffices.add(i18n.getDefaultLocale().toString()); |
| 66 | 46 | suffices.add(""); |
| 67 | } | |
| 68 | ||
| 69 | /** | |
| 70 | * {@inheritDoc} | |
| 71 | */ | |
| 72 | 0 | public boolean hasNext() |
| 73 | { | |
| 74 | 0 | return viewLookupSequence.hasNext() || iterator.hasNext(); |
| 75 | } | |
| 76 | ||
| 77 | /** | |
| 78 | * {@inheritDoc} | |
| 79 | */ | |
| 80 | 552 | public String next() |
| 81 | throws NoSuchElementException | |
| 82 | { | |
| 83 | 552 | if(lastPrefix == null || !iterator.hasNext()) |
| 84 | { | |
| 85 | 184 | lastPrefix = viewLookupSequence.next(); |
| 86 | 184 | iterator = suffices.iterator(); |
| 87 | } | |
| 88 | 552 | String suffix = iterator.next(); |
| 89 | 552 | buff.setLength(0); |
| 90 | 552 | buff.append(lastPrefix); |
| 91 | 552 | if(suffix.length() > 0) |
| 92 | { | |
| 93 | 368 | buff.append('.').append(suffix); |
| 94 | } | |
| 95 | 552 | return buff.toString(); |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * {@inheritDoc} | |
| 100 | */ | |
| 101 | 0 | public void reset() |
| 102 | { | |
| 103 | 0 | viewLookupSequence.reset(); |
| 104 | 0 | iterator = suffices.iterator(); |
| 105 | } | |
| 106 | ||
| 107 | /** | |
| 108 | * {@inheritDoc} | |
| 109 | */ | |
| 110 | 552 | public String currentView() |
| 111 | { | |
| 112 | 552 | return viewLookupSequence.currentView(); |
| 113 | } | |
| 114 | } |
|
||||||||||