Hi Gilles,

On Aug 4, 2006, at 9:10 AM, Gilles MATHURIN wrote:

Hi all,

Hope you've got a great summer, as for me it's always summer in the caribbeans.

Well, if you ever need a consultant you have my e-mail.  :-)


I 've got an issue with localizating an testApp. I am using the code in the Chuck Hill and Sacha Book, and i get an IllegalState Exception as a result.

I tried to implement their code in my Session.java file (i thought it was going there, but maybe i completly misunderstood th chapter).

Here is the missing exception (that Gilles sent to me in e-mail:

Original Exception:
java.lang.NullPointerException
at Session.browserLanguages(Session.java:116)
at Session.<init>(Session.java:31)

I don't know who wrote that code in that book. Must have been some kind of moron. :-P I don't know how that got through the reviews and tech reviews...

Anyway, there is no context() available in the session constructor so, when you call this

this.setLanguages(browserLanguages());

in the constructor and it calls
        protected NSArray browserLanguages() {
                NSMutableArray browserLanguages = new NSMutableArray();
                if (context().request() != null) {

context() returns null and a NPE results.

To fix this, move the line

this.setLanguages(browserLanguages())

to the awake() method:

public void awake() {
    super.awake();
    this.setLanguages(browserLanguages())
}

Chuck



Hope Someone could help me …

Regards.

GM

Here is my Session.java code :

//
// Session.java
// Project LocalizationApp
//
// Created by gillesmathurin on 03/08/06
//

import com.webobjects.foundation.*;
import com.webobjects.appserver.*;
import com.webobjects.eocontrol.*;

public class Session extends WOSession {
        
        public NSDictionary countryCodeToCountryName;
        public NSDictionary languageCodeToLanguageName;


    public Session() {
        super();

        /* ** Put your per-session initialization code here ** */
                String codes[] = new String[] {"FR","US","DE"};
                String countries[] = new String[] {"FRANCE", "USA", 
"ALLEMAGNE"};
NSDictionary countryCodeToCountryName = new NSDictionary (countries,codes);
                
                String lcodes[] = new String[] {"fr","en","de"};
                String lnames[] = new String[] {"French", "English", "German"};
NSDictionary languageCodeToLanguageName = new NSDictionary (lnames,lcodes);
                
                this.setLanguages(browserLanguages());

    }
        
                
        protected String removeTail(String aString) {
                String aCleanedString = aString;
                int index = aString.indexOf(";");
                if (index> 0) {
                        aCleanedString = aString.substring(0,index);
                }
                return aCleanedString.trim();
        }
        
        /** Return the browser prefs in a array */
        protected NSArray browserLanguagePreferences(String aBrowserString) {
                NSMutableArray aList = new NSMutableArray();
                if (aBrowserString != null) {
                        int index = 0;
                        String aBrowserCode = "";
                        String remainderString = aBrowserString;
                        while (remainderString.length() > 0) {
                                index = remainderString.indexOf(",",index);
                                if (index>0) {
                                        aBrowserCode = 
remainderString.substring(0,index);
                                        
aList.addObject(removeTail(aBrowserCode));
                                        remainderString = 
remainderString.substring(index + 1);
                                }
                                else {
                                        aBrowserCode = remainderString;
                                        
aList.addObject(removeTail(aBrowserCode));
                                        remainderString ="";
                                }
                        }
                } else { // Use the default
                        aList.addObject("fr-FR");
                }
                return aList;
        }
        
        public static String countryCode(String aName) {
                // the string is in the form xx-YY
                String aCode ="";
                if (aName.indexOf("-") > 0) {
                        aCode = aName.substring(aName.indexOf("-") + 
1).toUpperCase();
                } else {
                        aCode = "ZZ"; // Unable to determinde the country code
                }
// The standard say that there can be another part after the country so just trim it
                        if (aCode.indexOf("-") > 0) {
                                aCode = aCode.substring(0, 
aCode.indexOf("-")).toUpperCase();
                        }
                                return aCode;
        }

        public static String languageCode(String aName) {
                // the string is in the form xx-YY
                String aCode = "";
                if (aName.length() > 2) {
                        aCode = aName.substring(0,2).toLowerCase();
                } else if (aName.length() == 2) {
                        aCode = aName.toLowerCase();
                } else {
                        aCode = "ZZ"; // Unable to determinde the country code
                }
                return aCode;
        }

        protected NSArray browserLanguages() {
                NSMutableArray browserLanguages = new NSMutableArray();
                if (context().request() != null) {
                        // So many different way to specify the accepted 
languages !
                        // Each adaptor has their own…
String aHeader = context().request().headerForKey("accept- language");
                        if ((aHeader == null) || aHeader.equals(""))
                                aHeader = 
context().request().headerForKey("Accept-Language");
                        if ((aHeader == null) || aHeader.equals(""))
aHeader = context().request().headerForKey ("HTTP_ACCEPT_LANGUAGE");
                        
                        String aBrowserCode = "";
java.util.Enumeration anEnumaration = browserLanguagePreferences (aHeader).objectEnumerator();
                        while (anEnumaration.hasMoreElements()) {
                                aBrowserCode = 
(String)anEnumaration.nextElement();
String countryName = (String) countryCodeToCountryName.objectForKey(countryCode(aBrowserCode)); String languageName = (String) languageCodeToLanguageName.objectForKey(languageCode(aBrowserCode)); browserLanguages.addObject(languageName + " (" +countryName + ")");
                        }
                }
                return browserLanguages;
        }


}

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/chill% 40global-village.net

This email sent to [EMAIL PROTECTED]


--
Coming sometime... - an introduction to web applications using WebObjects and Xcode http://www.global-village.net/wointro

Practical WebObjects - for developers who want to increase their overall knowledge of WebObjects or who are trying to solve specific problems. http://www.global-village.net/products/practical_webobjects




_______________________________________________
Do not post admin requests to the list. They will be ignored.
Webobjects-dev mailing list      ([email protected])
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/webobjects-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to