Example usage for org.apache.commons.lang LocaleUtils toLocale

List of usage examples for org.apache.commons.lang LocaleUtils toLocale

Introduction

In this page you can find the example usage for org.apache.commons.lang LocaleUtils toLocale.

Prototype

public static Locale toLocale(String str) 

Source Link

Document

Converts a String to a Locale.

This method takes the string format of a locale and creates the locale object from it.

 LocaleUtils.toLocale("en")         = new Locale("en", "") LocaleUtils.toLocale("en_GB")      = new Locale("en", "GB") LocaleUtils.toLocale("en_GB_xxx")  = new Locale("en", "GB", "xxx")   (#) 

(#) The behaviour of the JDK variant constructor changed between JDK1.3 and JDK1.4.

Usage

From source file:org.atomserver.core.dbstore.DBSTestCase.java

protected void modifyEntry(String workspace, String collection, String entryId, String locale,
        String xmlFileString, boolean creating, String revision, boolean checkContent, boolean checkCount,
        String diffContentValue) throws Exception {

    log.debug("\n%%%%%%%%%%%%%% CREATING:: [" + workspace + ", " + collection + ", " + entryId + ", " + locale);

    BaseServiceDescriptor serviceDescriptor = new BaseServiceDescriptor(workspace);

    int startCount = 0;
    if (checkCount) {
        startCount = entriesDao.getTotalCount(serviceDescriptor);
        log.debug("startCount = " + startCount);
    }/*from  w  w w . j a v  a  2s . c o m*/

    AbderaClient client = new AbderaClient();
    RequestOptions options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");

    Entry entry = AtomServerTestCase.getFactory().newEntry();
    entry.setId(getURLPath(workspace, collection, entryId, locale, revision));
    entry.setContentAsXhtml(xmlFileString.replaceFirst("<\\?[^\\?]*\\?>", ""));

    String putUrl = getServerURL() + getURLPath(workspace, collection, entryId, locale, revision);
    log.debug("PUTting to URL : " + putUrl);

    ClientResponse response = client.put(putUrl, entry, options);

    Document<Entry> doc = response.getDocument();
    Entry entryOut;
    try {
        entryOut = doc.getRoot();
    } catch (Exception e) {
        Document<org.apache.abdera.protocol.error.Error> errorDoc = response.getDocument();
        log.error(errorDoc.getRoot().getMessage());
        throw e;
    }

    IRI editLink = entryOut.getEditLinkResolvedHref();
    assertNotNull("link rel='edit' must not be null", editLink);

    assertEquals(("entry [" + workspace + "," + collection + "," + locale + "," + entryId + "]"),
            creating ? 201 : 200, response.getStatus());
    response.release();

    // file system needs to catch up
    if (checkContent) {
        int rev = extractRevisionFromURI(editLink.toString());
        Thread.sleep(300);
        assertNotNull(contentStorage.getContent(new BaseEntryDescriptor(workspace, collection, entryId,
                LocaleUtils.toLocale(locale), (rev - 1))));
    }

    // COUNT
    if (checkCount) {
        int exitCount = entriesDao.getTotalCount(serviceDescriptor);
        assertEquals((creating ? startCount + 1 : startCount), exitCount);
    }

    if (diffContentValue != null && ("true".equals(diffContentValue) || "false".equals(diffContentValue))) {
        String contentHashValue = entryOut.getSimpleExtension(AtomServerConstants.CONTENT_HASH);
        String contentUpdatedTag = entryOut.getSimpleExtension(AtomServerConstants.ENTRY_UPDATED);
        assertNotNull(contentHashValue);
        assertNotNull(contentUpdatedTag);
        assertEquals(contentUpdatedTag, diffContentValue);
    }

    LatencyUtil.updateLastWrote();
}

From source file:org.atomserver.core.dbstore.DBSTestCase.java

protected void deleteEntry(String workspace, String collection, String entryId, String locale)
        throws Exception {
    URIHandler handler = widgetURIHelper;
    IRI iri = IRI.create("http://localhost:8080/"
            + handler.constructURIString(workspace, collection, entryId, LocaleUtils.toLocale(locale)));

    log.debug("deleting IRI : " + iri);

    EntryTarget entryTarget = handler// w  ww . ja va  2s  .  co m
            .getEntryTarget(new MockRequestContext(serviceContext, "GET", iri.toString()), true);
    contentStorage.deleteContent(null, entryTarget);
    entriesDao.obliterateEntry(entryTarget);

    Thread.sleep(DB_CATCHUP_SLEEP);
}

From source file:org.atomserver.core.dbstore.TagsBasicsDBSTest.java

public void testEntryWithCategories() throws Exception {
    /*  PUT::/*from   ww  w  .  jav  a2 s .  c  o m*/
    <entry xmlns="http://www.w3.org/2005/Atom">
     <id>tags:widgets/acme/4.en.xml</id>
     <content type="xhtml">
       <div xmlns="http://www.w3.org/1999/xhtml">
     <categories xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom">
       <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="testutils:0" />
       <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="testutils:1" />
       <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="testutils:2" />
       <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="testutils:3" />
       <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="testutils:4" />
       <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="testutils:5" />
       <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="testutils:6" />
       <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="testutils:7" />
     </categories>
       </div>
     </content>
    </entry>
            
      THE CORRESPONDING ENTRY NOW RETURNS ::
    <?xml version='1.0' encoding='UTF-8'?>
     <entry xmlns="http://www.w3.org/2005/Atom">
     <id>/atomserver/v1/widgets/acme/4.en.xml</id>
     <title type="text"> Entry: acme 4.en</title>
     <author><name>atomserver Atom Service</name></author>
     <link href="/atomserver/v1/widgets/acme/4.en.xml" rel="self" />
     <link href="/atomserver/v1/widgets/acme/4.en.xml/1" rel="edit" />
     <updated>2008-01-04T19:03:21.000Z</updated>
     <published>2007-12-16T03:33:33.000Z</published>
     <category scheme="urn:widgets/foo" term="testutils:0" />
     <category scheme="urn:widgets/foo" term="testutils:1" />
     <category scheme="urn:widgets/foo" term="testutils:2" />
     <category scheme="urn:widgets/foo" term="testutils:3" />
     <category scheme="urn:widgets/foo" term="testutils:4" />
     <category scheme="urn:widgets/foo" term="testutils:5" />
     <category scheme="urn:widgets/foo" term="testutils:6" />
     <category scheme="urn:widgets/foo" term="testutils:7" />
     <content type="application/xml">
       <property xmlns="http://wvrgroup.com/propertyom" systemId="acme" id="4" homeAwayNetwork="false">[\n]">
      .....
    */

    // First let's add a bunch of Categories for the Entry
    // Create a standard APP Categories doc
    //  which is the Content for this "tags:widgets" Entry
    String urlPath = "tags:widgets/acme/4.en.xml/*";
    String fullURL = getServerURL() + urlPath;
    String id = urlPath;

    Categories categories = getFactory().newCategories();

    int numCats = 8;
    for (int ii = 0; ii < numCats; ii++) {
        Category category = getFactory().newCategory();
        category.setScheme("urn:widgets/foo");
        category.setTerm("testutils:" + ii);
        categories.addCategory(category);
    }

    String categoriesXML = getCategoriesXML(categories);

    //INSERT
    String editURI = update(id, fullURL, categoriesXML);

    // Now let's GET the corresponding "widgets" Entry
    urlPath = "widgets/acme/4.en.xml";
    fullURL = getServerURL() + urlPath;

    ClientResponse response = clientGetWithFullURL(fullURL, 200);
    assertEquals(200, response.getStatus());

    Document<Entry> doc = response.getDocument();
    Entry entryOut = doc.getRoot();

    String xmlContent = entryOut.getContent();
    assertTrue(xmlContent.indexOf("id=\"4\"") != -1);

    // verify that the Categories appear in the Entry
    List<Category> entryCategories = entryOut.getCategories();
    assertNotNull(entryCategories);
    assertEquals(numCats, entryCategories.size());

    response.release();

    // Let's delete the Categories -- these are actually deleted -- not just "marked deleted"
    editURI = delete(editURI);

    // Let's verify that the Categories are gone and that the Entry content has not been deleted
    response = clientGetWithFullURL(fullURL, 200);
    assertEquals(200, response.getStatus());

    doc = response.getDocument();
    entryOut = doc.getRoot();

    xmlContent = entryOut.getContent();
    assertTrue(xmlContent.indexOf("id=\"4\"") != -1);

    // verify that the Categories appear in the Entry
    entryCategories = entryOut.getCategories();
    assertNotNull(entryCategories);
    assertEquals(0, entryCategories.size());

    response.release();

    // Let's verify that the actual Entry is NOT marked "Deleted"
    IRI entryIRI = IRI.create("http://localhost:8080/"
            + widgetURIHelper.constructURIString("widgets", "acme", "4", LocaleUtils.toLocale("en")));
    EntryTarget entryTarget = widgetURIHelper
            .getEntryTarget(new MockRequestContext(serviceContext, "GET", entryIRI.toString()), true);
    EntryMetaData metaData = entriesDAO.selectEntry(entryTarget);
    assertFalse(metaData.getDeleted());
}

From source file:org.atomserver.core.dbstore.TagsBasicsDBSTest.java

public void testGetServiceDocument() throws Exception {

    // First let's add some Categories for some Entries
    // Create a standard APP Categories doc
    //  which is the Content for this "tags:widgets" Entry
    String urlPath = "tags:widgets/acme/4.en.xml/*";
    String fullURL = getServerURL() + urlPath;
    String id = urlPath;/*from  w w w  .  ja  v  a 2s. c o  m*/

    Categories categories = getFactory().newCategories();
    int numCats4 = 3;
    String[] schemes4 = { "urn:widgets/foo", "urn:widgets/foo", "urn:widgets/bar" };
    String[] terms4 = { "term1", "term2", "term1" };

    for (int ii = 0; ii < numCats4; ii++) {
        Category category = getFactory().newCategory();
        category.setScheme(schemes4[ii]);
        category.setTerm(terms4[ii]);
        categories.addCategory(category);
    }
    String categoriesXML4 = getCategoriesXML(categories);

    //INSERT
    String editURI4 = insert(id, fullURL, categoriesXML4, false);

    //-------------
    urlPath = "tags:widgets/acme/2797.en.xml/*";
    fullURL = getServerURL() + urlPath;
    id = urlPath;

    categories = getFactory().newCategories();
    int numCats2797 = 3;
    String[] schemes2797 = { "urn:widgets/foo", "urn:widgets/foo", "urn:widgets/bar" };
    String[] terms2797 = { "term3", "term1", "term1" };

    for (int ii = 0; ii < numCats2797; ii++) {
        Category category = getFactory().newCategory();
        category.setScheme(schemes2797[ii]);
        category.setTerm(terms2797[ii]);
        categories.addCategory(category);
    }
    String categoriesXML2797 = getCategoriesXML(categories);

    //INSERT
    String editURI2797 = insert(id, fullURL, categoriesXML2797, false);

    //=====================
    // Now get the Service Document
    /*
      <?xml version='1.0' encoding='UTF-8'?>
      <service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom">
    <workspace>
      <atom:title type="text">widgets</atom:title>
      <collection href="widgets/dummy/">
         <atom:title type="text">dummy</atom:title>
         <accept>application/atom+xml;type=entry</accept>
         <categories />
      </collection>
      <collection href="widgets/acme/">
        <atom:title type="text">acme</atom:title>
        <accept>application/atom+xml;type=entry</accept>
        <categories>
           <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/bar" term="term1" />
           <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="term1" />
           <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="term2" />
           <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="term3" />
        </categories>
      </collection>
    </workspace>
    ......
     </service>
    */

    AbderaClient client = new AbderaClient();
    RequestOptions options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");

    // do the introspection step
    ClientResponse response = client.get(getServerURL(), options);
    assertEquals(200, response.getStatus());

    Document<Service> service_doc = response.getDocument();
    assertNotNull(service_doc);

    // there are several workspaces configured for the base URL
    //int numWorkspaces = store.getNumberOfWorkspaces();
    int numWorkspaces = store.getNumberOfVisibleWorkspaces();

    assertEquals(numWorkspaces, service_doc.getRoot().getWorkspaces().size());

    Workspace workspace1 = service_doc.getRoot().getWorkspace("tags:widgets");
    assertNull(workspace1);
    Workspace workspace = service_doc.getRoot().getWorkspace("widgets");
    assertNotNull(workspace);

    for (Collection c : workspace.getCollections()) {
        assertNotNull(c.getTitle());
        assertNotNull(c.getHref());
        assertTrue(c.getHref().toString().startsWith(workspace.getTitle() + '/'));
    }
    response.release();

    //===============
    // Now get the Service Document
    /*
      <?xml version='1.0' encoding='UTF-8'?>
      <service xmlns="http://www.w3.org/2007/app" xmlns:atom="http://www.w3.org/2005/Atom">
    <workspace>
      <atom:title type="text">widgets</atom:title>
      <collection href="widgets/dummy/">
        <atom:title type="text">dummy</atom:title>
        <accept>application/atom+xml;type=entry</accept>
        <categories />
      </collection>
      <collection href="widgets/acme/">
        <atom:title type="text">acme</atom:title>
        <accept>application/atom+xml;type=entry</accept>
        <categories>
           <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/bar" term="term1" />
           <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="term1" />
           <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="term2" />
           <category xmlns="http://www.w3.org/2005/Atom" scheme="urn:widgets/foo" term="term3" />
        </categories>
      </collection>
    </workspace>
      </service>
    */
    client = new AbderaClient();
    options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");

    // do the introspection step
    response = client.get(getServerURL() + "widgets", options);
    assertEquals(200, response.getStatus());

    service_doc = response.getDocument();
    assertNotNull(service_doc);

    workspace = service_doc.getRoot().getWorkspace("widgets");
    assertNotNull(workspace);

    for (Collection c : workspace.getCollections()) {
        assertNotNull(c.getTitle());
        assertNotNull(c.getHref());
        assertTrue(c.getHref().toString().startsWith(workspace.getTitle() + '/'));
    }
    response.release();

    //===============
    // Now get the Service Document
    // NOTE: should return the same service docs a "widgets"
    client = new AbderaClient();
    options = client.getDefaultRequestOptions();
    options.setHeader("Connection", "close");

    // do the introspection step
    response = client.get(getServerURL() + "tags:widgets", options);
    assertEquals(200, response.getStatus());

    service_doc = response.getDocument();
    assertNotNull(service_doc);

    workspace = service_doc.getRoot().getWorkspace("widgets");
    assertNotNull(workspace);

    for (Collection c : workspace.getCollections()) {
        assertNotNull(c.getTitle());
        assertNotNull(c.getHref());
        assertTrue(c.getHref().toString().startsWith(workspace.getTitle() + '/'));
    }
    response.release();

    //===============
    // Let's delete the Categories -- these are actually deleted -- not just "marked deleted"
    delete(editURI4);
    delete(editURI2797);

    // Delete the Categories we created (permanently)
    IRI entryIRI4 = IRI.create("http://localhost:8080/"
            + widgetURIHelper.constructURIString("tags:widgets", "acme", "4", LocaleUtils.toLocale("en")));
    EntryTarget entryTarget4 = widgetURIHelper
            .getEntryTarget(new MockRequestContext(serviceContext, "GET", entryIRI4.toString()), false);
    entriesDAO.obliterateEntry(entryTarget4);

    // Let's verify that the Categories are gone and that the Entry content has not been deleted
    urlPath = "widgets/acme/4.en.xml";
    fullURL = getServerURL() + urlPath;

    response = clientGetWithFullURL(fullURL, 200);
    assertEquals(200, response.getStatus());

    Document<Entry> doc = response.getDocument();
    Entry entryOut = doc.getRoot();

    IRI entryIRI2797 = IRI.create("http://localhost:8080/"
            + widgetURIHelper.constructURIString("tags:widgets", "acme", "2797", LocaleUtils.toLocale("en")));
    EntryTarget entryTarget2797 = widgetURIHelper
            .getEntryTarget(new MockRequestContext(serviceContext, "GET", entryIRI2797.toString()), false);
    entriesDAO.obliterateEntry(entryTarget2797);

    String xmlContent = entryOut.getContent();
    assertTrue(xmlContent.indexOf("id=\"4\"") != -1);

    List<Category> entryCategories = entryOut.getCategories();
    assertNotNull(entryCategories);
    assertEquals(0, entryCategories.size());

    response.release();

    //---------
    urlPath = "widgets/acme/2797.en.xml";
    fullURL = getServerURL() + urlPath;
    response = clientGetWithFullURL(fullURL, 200);
    assertEquals(200, response.getStatus());

    doc = response.getDocument();
    entryOut = doc.getRoot();

    xmlContent = entryOut.getContent();
    assertTrue(xmlContent.indexOf("id=\"2797\"") != -1);

    entryCategories = entryOut.getCategories();
    assertNotNull(entryCategories);
    assertEquals(0, entryCategories.size());

    response.release();
}

From source file:org.atomserver.core.filestore.FileBasedContentStorage.java

private EntryDescriptor getMetaDataFromFilePath(File file) {
    String filePath = file.getAbsolutePath();
    if (log.isDebugEnabled()) {
        log.debug("file path = " + filePath);
    }/*from  w  ww . j  a v a 2s  .c o  m*/
    String rootPath = getRootDir().getAbsolutePath();
    if (log.isDebugEnabled()) {
        log.debug("root path = " + rootPath);
    }
    String relativePath = filePath.replace(rootPath, "");
    if (log.isDebugEnabled()) {
        log.debug("relative path = " + relativePath);
    }
    Matcher matcher = FILE_PATH_WORKSPACE_COLLECTION_PATTERN.matcher(relativePath);
    if (matcher.matches()) {
        String workspace = matcher.group(1);
        String collection = matcher.group(2);
        File collectionRoot = pathFromRoot(workspace, collection);

        for (PartitionPathGenerator pathGenerator : partitionPathGenerators) {
            PartitionPathGenerator.ReverseMatch match = pathGenerator.reverseMatch(collectionRoot, file);
            if (match != null) {
                String entryId = match.getSeed();
                Matcher localeRevMatcher = FILE_PATH_LOCALE_REV_PATTERN.matcher(match.getRest());
                Locale locale;
                int revision;
                if (localeRevMatcher.matches()) {
                    locale = StringUtils.isEmpty(localeRevMatcher.group(1)) ? null
                            : LocaleUtils.toLocale(localeRevMatcher.group(1).replace("/", "_"));
                    try {
                        revision = Integer.parseInt(localeRevMatcher.group(2));
                    } catch (NumberFormatException ee) {
                        String msg = "Could not parse revision from file= " + file;
                        log.error(msg);
                        throw new AtomServerException(msg);
                    }
                    return new BaseEntryDescriptor(workspace, collection, entryId, locale, revision);
                }
            }
        }
    }
    return null;
}

From source file:org.atomserver.testutils.ibatis.LocaleTypeHandler.java

public Object getResult(ResultGetter getter) throws SQLException {
    String val = getter.getString();
    if (val == null) {
        throw new SQLException("NULL Result supplied in LocaleTypeHandler");
    } else {//from w  ww  . ja  va 2s. co  m
        return LocaleUtils.toLocale(val);
    }
}

From source file:org.atomserver.testutils.ibatis.LocaleTypeHandler.java

public Object valueOf(String val) {
    return LocaleUtils.toLocale(val);
}

From source file:org.atomsphere.management.web.properties.WebSystemPropertiesService.java

@Override
public Locale getLocale() {
    Locale locale = null;//  www.j a  v a  2s . c o  m

    String localeStr = getProperty(
            EnvParametersKeys.class.getName() + "." + EnvParametersKeys.DEFAULT_LOCALE.name());

    if (StringUtils.isNotEmpty(localeStr)) {
        locale = LocaleUtils.toLocale(localeStr);
    }
    return locale == null ? DEFAULT_LOCALE : locale;
}

From source file:org.datacleaner.monitor.server.WizardServiceImpl.java

private Locale getLocale(String localeString) {
    if (localeString == null) {
        return Locale.ENGLISH;
    }//  ww w . j a v a 2 s .  com
    if ("".equals(localeString) || "default".equals(localeString)) {
        return Locale.ENGLISH;
    }
    return LocaleUtils.toLocale(localeString);
}

From source file:org.drools.planner.benchmark.core.measurement.ScoreDifferencePercentage.java

public String toString(String locale) {
    return toString(LocaleUtils.toLocale(locale));
}