20 June 2014

Using Resource Bundle in Java

How to read resource bundle in java?

BundleTest.java

package org.metadata.core.xml;

import java.text.MessageFormat;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

public class BundleTest {
      private ResourceBundle bundle;

      public BundleTest() {
            bundle = ResourceBundle.getBundle("messages");
      }

      public String getMessage(String id, Object... params) {
            String text = null;
            try {
                  text = bundle.getString(id);
            } catch (MissingResourceException e) {
                  text = "!! key " + id + " not found !!";
            }
            if (params != null) {
                  MessageFormat mf = new MessageFormat(text);
                  text = mf.format(params, new StringBuffer(), null).toString();
            }
            return text;
      }

      public static void main(String[] args) {
            BundleTest test = new BundleTest();
            String message_1 = test.getMessage("MESSAGE_ONE");
            System.out.println(message_1);
            String message_2 = test.getMessage("MESSAGE_TWO", "ZTO", "Myanmar");
            System.out.println(message_2);
      }
}


  1. Create a folder name with “resources” and set the folder into the classpath. 
  2. Put the “messages.properties” file into the “resources” folder.

messages.properties
MESSAGE_ONE = Hello World!
MESSAGE_TWO = Hi {0}! Welcome from {1}!

Output
Hello World!
Hi ZTO! Welcome from Myanmar!

XML Validation Using XSD File


The example will show how to check the xml format validation by using XSD file.

XMLValidator.java
package org.metadata.core.xml;

import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;

/**
 * A sample application which shows how to perform a
 * XML document validation.
 */
public class XMLValidator {
    public static void main(String[] args) {
        try {
            // define the type of schema - we use W3C:
            String schemaLang = "http://www.w3.org/2001/XMLSchema";

            // get validation driver:
            SchemaFactory factory = SchemaFactory.newInstance(schemaLang);

            // create schema by reading it from an XSD file:
            Schema schema = factory.newSchema(new StreamSource("resources/sample.xsd"));
            Validator validator = schema.newValidator();

            // at last perform validation:
            validator.validate(new StreamSource("resources/sample.xml"));
            System.out.println("Valid XML Format....");
        } catch (SAXException ex) {
            System.out.println("Invalid XML Format....");
            ex.printStackTrace();
        } catch (Exception ex) {
            System.out.println("Invalid XML Format....");
            ex.printStackTrace();
        }
    }
}

  1. Create a folder name with “resources” and set the folder into the classpath.
  2. Put the “sample.xml” and “sample.xsd” file into the “resources” folder.
  3. Make sure schema location in “sample.xml” file.

sample.xml
<?xml version="1.0" encoding="UTF-8"?>
<noties xsi:noNamespaceSchemaLocation="sample.xsd"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <notic>
      <line>
          <content>Mutu</content>
          <color>#0052415</color>
      </line>
      <line>
          <content>Zaw Than Oo</content>
          <color>red</color>
      </line>
      <line>
          <content>John Smith</content>
          <color>blue</color>
      </line>
      <line>
          <content>CycDemo</content>
          <color></color>
      </line>
  </notic>
</noties>

sample.xsd

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="noties">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="notic" minOccurs="1" maxOccurs="1" nillable="false">
          <xs:complexType>
            <xs:sequence>
            <xs:element name="line" minOccurs="1" maxOccurs="unbounded" nillable="false">
            <xs:complexType>
              <xs:sequence>
                <xs:element name="content" minOccurs="1" maxOccurs="1" nillable="false">
                  <xs:simpleType>
                    <xs:restriction base="xs:string">
                      <xs:minLength value="1"/>
                    </xs:restriction>
                  </xs:simpleType>
                </xs:element>
                <xs:element name="color" type="xs:string"/>
              </xs:sequence>
            </xs:complexType>
            </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Complete Package Structure



Like us on Facebook