All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.shindig.gadgets.spec.LocaleSpec Maven / Gradle / Ivy

Go to download

Renders gadgets, provides the gadget metadata service, and serves all javascript required by the OpenSocial specification.

There is a newer version: 3.0.0-beta4
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */
package org.apache.shindig.gadgets.spec;
import org.apache.shindig.common.uri.Uri;
import org.apache.shindig.common.xml.XmlUtil;

import org.w3c.dom.Element;

import java.util.Locale;
import java.util.Map;

/**
 * Represents a Locale tag.
 * Generally compatible with java.util.Locale, but with some extra
 * localization data from the spec.
 * Named "LocaleSpec" so as to not conflict with java.util.Locale
 *
 * No localization.
 * No user pref substitution.
 */
public class LocaleSpec {
  private final Locale locale;
  private final String languageDirection;
  private final Uri messages;
  private final MessageBundle messageBundle;

  /**
   * @param specUrl The url that the spec is loaded from. messages is assumed
   *     to be relative to this path.
   * @throws SpecParserException If language_direction is not valid
   */
  public LocaleSpec(Element element, Uri specUrl) throws SpecParserException {
    String language = XmlUtil.getAttribute(element, "lang", "all").toLowerCase();
    String country = XmlUtil.getAttribute(element, "country", "ALL").toUpperCase();
    this.locale = new Locale(language, country);

    languageDirection = XmlUtil.getAttribute(element, "language_direction", "ltr");
    if (!("ltr".equals(languageDirection) || "rtl".equals(languageDirection))) {
      throw new SpecParserException("Locale/@language_direction must be ltr or rtl");
    }
    String messagesString = XmlUtil.getAttribute(element, "messages");
    if (messagesString == null) {
      this.messages = Uri.parse("");
    } else {
      try {
        this.messages = specUrl.resolve(Uri.parse(messagesString));
      } catch (IllegalArgumentException e) {
        throw new SpecParserException("Locale@messages url is invalid.");
      }
    }
    messageBundle = new MessageBundle(element);
  }

  public Locale getLocale() {
    return locale;
  }

  /**
   * Locale@lang
   */
  public String getLanguage() {
    return locale.getLanguage();
  }

  /**
   * Locale@country
   */
  public String getCountry() {
    return locale.getCountry();
  }

  /**
   * Locale@language_direction
   */
  public String getLanguageDirection() {
    return languageDirection;
  }

  /**
   * Locale@messages
   */
  public Uri getMessages() {
    return messages;
  }

  /**
   * Locale/msg
   */
  public MessageBundle getMessageBundle() {
    return messageBundle;
  }

  @Override
  public String toString() {
    StringBuilder buf = new StringBuilder();
    buf.append("\n");
    for (Map.Entry entry : messageBundle.getMessages().entrySet()) {
      buf.append("")
         .append(entry.getValue())
         .append("\n");
    }
    buf.append("");
    return buf.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy