Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Enabling XML documents for globalization

A simple approach to organizing your translatable XML resources

Return to article


Listing 7: Processing translated XML subdocuments


  /**
   * Create a properties file using the specified XML subdocument based on the lookup
   * keys of the argument table.
   */
  public void processSubDocument(String uri, Vector translatedKeys)
  {
    setTranslatedKeys(translatedKeys);
    
    SAXParser parser = new SAXParser();
    parser.setContentHandler(this);
    parser.setErrorHandler(new SAXErrorHandler());
    try
    {
      parser.parse(uri);
    }
    catch (SAXException e)
    {
      MessageLogger.logMessage(MessageCodes.SAX_EXCEPTION, true, false, e);
      Exception embeddedException = e.getException();
      MessageLogger.logMessage(MessageCodes.JAVA_EXCEPTION, true, false, embeddedException);
    }
    catch (Exception e)
    {
      MessageLogger.logMessage(MessageCodes.JAVA_EXCEPTION, true, true, e);
    }
    return writePropertiesFileFromTable(getTranslatedStrings());
  }

  /**
   * Handle the start element event.
   * 
   * See if the element parsed was referenced in the main XML document.  If so,
   * set a flag to catch it in the characters event and remember the tag.
   */
  public void startElement(String uri, String localName, String rawName, Attributes attrs)
  {
    if (getTranslatedKeys().containsValue(localName))
    {
      isTargetKey = true;
      currentLookupKey = localName;
    }
  }

  /**
   * Handle the characters event.
   * 
   * Check the flag to see if the element for this data was referenced in the 
   * main XML document.  If so, grab the data and save it as a string 
   * with its element tag as the key.
   */
  public void characters(char ch[], int start, int length)
  {
    if (isTargetKey)
    {
      String translation = (new String(ch, start, length)).trim();
      getTranslatedStrings().put(currentLookupKey, translation);
      isTargetKey = false;
      currentLookupKey = null;
    }
  }

Return to article