Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * *********************************************************************************************************************
   *
   * NorthernWind - lightweight CMS
   * http://northernwind.tidalwave.it - hg clone https://bitbucket.org/tidalwave/northernwind-src
   * %%
   * Copyright (C) 2011 - 2013 Tidalwave s.a.s. (http://tidalwave.it)
   * %%
  * *********************************************************************************************************************
  *
  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  * the License. You may obtain a copy of the License at
  *
  *     http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  * specific language governing permissions and limitations under the License.
  *
  * *********************************************************************************************************************
  *
  * $Id: TextResourcePropertyResolver.java,v 2a03aa9acdde 2013/07/13 10:34:52 fabrizio $
  *
  * *********************************************************************************************************************
  * #L%
  */
 package it.tidalwave.northernwind.core.impl.model;
 
 import  javax.annotation.Nonnull;
 import  it.tidalwave.util.Id;
 import  it.tidalwave.util.Key;
 import  it.tidalwave.util.NotFoundException;
This class tries to resolve a property by loading it from a text file. The file is searched in the given folder with the same name of the property, localized.If the searched property is fullText, and the managed locales are en and it, then the following files are searched for:
  • fullText.xhtml
  • fullText.html
  • fullText.xml
  • fullText.txt
  • fullText_en.xhtml
  • fullText_en.html
  • fullText_en.xml
  • fullText_en.txt
  • fullText_it.xhtml
  • fullText_it.html
  • fullText_it.xml
  • fullText_it.txt
Files are pre-processed through the FilterSetExpander. If the file MIME type is XHTML, UTF-8 is used for the encoding, otherwise the default charset is used. HTML support is a legacy and will be removed in a future version.

Author(s):
Fabrizio Giudici
Version:
$Id: TextResourcePropertyResolver.java,v 2a03aa9acdde 2013/07/13 10:34:52 fabrizio $ /
 
 public class TextResourcePropertyResolver implements ResourceProperties.PropertyResolver
   {
     private static final ImmutableList<StringEXTENSIONS = ImmutableList.of(".xhtml"".html"".xml"".txt");
 
     @Inject @Nonnull
 
     @Inject @Nonnull
 
     @Nonnull
     private final ResourceFile folder;

    
/
 
     public TextResourcePropertyResolver (final @Nonnull ResourceFile folder)
       {
         this. = folder;
       }

    
/
    @Override @SuppressWarnings("unchecked")
    public <Type> Type resolveProperty (final @Nonnull Id propertyGroupIdfinal @Nonnull Key<Type> propertyName)
      throws NotFoundException, IOException
      {
        log.trace("resolveProperty({})"propertyName);
        final ResourceFile propertyFile = findLocalizedFile(propertyName.stringValue());
        log.trace(">>>> reading from {}"propertyFile.getPath());
        final String mimeType = propertyFile.getMimeType();
        final String charset = mimeType.equals("application/xhtml+xml") ? "UTF-8" : Charset.defaultCharset().name();
        try
          {
            return (Type).get().filter(propertyFile.asText(charset), mimeType);
          }
        catch (RuntimeException e// FIXME: introduce a FilterException
          {
            throw new IOException(e);
          }
      }

    
/
    @Nonnull
    private ResourceFile findLocalizedFile (final @Nonnull String fileName)
      throws NotFoundException
      {
        log.trace("findLocalizedFile({})"fileName);
        final StringBuilder fileNamesNotFound = new StringBuilder();
        String separator = "";
        for (final String localeSuffix : .getLocaleSuffixes())
          {
            for (final String extension : )
              {
                final String localizedFileName = fileName + localeSuffix + extension;
                try
                  {
                    return .findChildren().withName(localizedFileName).result();
                  }
                catch (NotFoundException e)
                  {
                    // continue
                  }
                fileNamesNotFound.append(separator);
                fileNamesNotFound.append(localizedFileName);
                separator = ",";
              }
          }
        throw new NotFoundException(String.format("%s/{%s}".getPath().asString(), fileNamesNotFound));
      }
  }
New to GrepCode? Check out our FAQ X