tag:blogger.com,1999:blog-23173670346426120472024-03-13T18:22:02.923-07:00Joy of GroovyYiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.comBlogger12125tag:blogger.com,1999:blog-2317367034642612047.post-32910292641845557412016-05-28T05:24:00.000-07:002016-05-28T05:32:21.015-07:00耶鲁大学的格言 Light and truth 光明与真理 אורים ותמים<h2>
אורים ותמים </h2>
<h3>
耶鲁大学的格言 </h3>
耶鲁大学的格言是希伯来文 אורים ותמים<br />
<br />
אורים ותמים (Hebrew) (读音Urim V'Thummim)<br />
Lux et veritas (Latin)<br />
Light and truth(English)<br />
光明与真理(中文)
<br />
<br />
下面是网站(https://en.wikipedia.org/wiki/Yale_University)上的官方解释。
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGLW1V6QSGzn-8SnENnLlsF-cixgNGBsxRvlTAsT647Cj7eANLNfxBCqn9kk30xs_uc2pjB40omsKNIyi1VnHrldI0Escokm0kyL9tgFp-QHyjsWl8BQG6uljnSwjWnJjYrD4RygBaTprJ/s1600/Yale_University_Shield_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgGLW1V6QSGzn-8SnENnLlsF-cixgNGBsxRvlTAsT647Cj7eANLNfxBCqn9kk30xs_uc2pjB40omsKNIyi1VnHrldI0Escokm0kyL9tgFp-QHyjsWl8BQG6uljnSwjWnJjYrD4RygBaTprJ/s320/Yale_University_Shield_1.png" /></a></div>
The arms of Yale University, often referred to as the shield of the university. The device on the seal, a book bearing the Hebrew phrase אורים ותמים (English: Urim V'Tamim), originates from the early eighteenth century, and its designer is unknown.<br />
大意是:耶鲁大学的膀臂常称为大学的盾牌。<br />
印上是一本写有希伯来文אורים ותמים (英文Urim V’Tamim,中文乌陵和土明)的书,
说是来自18世纪早期。设计者未知。<br />
<br />
我认为这个理解太肤浅。容我来解释一下。<br />
<br />
אורים ותמים那个希伯来文的翻译没有多少问题。<br />
<br />
תמים: Whole, entire, intact, blameless, sincerely, honestly, perfect in knowledge 整全,完整,无愧,真诚,诚实,知识的完备<br />
אורים:Light 光明<br />
所以译成中文为“光明与真理”是恰当的。但תמים本身的意思比译文丰富得多。
<br />
<br />
我认为这个词不是来自18世纪,而是直接来自圣经。<br />
<br />
首先这个词(אורים ותמים)多次完整出现在圣经中,意思是“从神那儿得旨意的装备或程序”。<br />
<br />
下面是这个词在圣经中出现的一些地方:<br />
<br />
Ex 28:30 (出埃及记28:30)
30ונתת אל חשן המשפט את האורים ואת התמים והיו על לב אהרן בבאו לפני יהוה ונשא אהרן את משפט בני ישראל על לבו לפני יהוה—תמיד {ס}
30<br />
你要把乌陵和土明放在作决断用的胸牌里。亚伦进入耶和华面前的时候,它们要带在胸前;这样,亚伦就在耶和华面前常常把以色列众子的决断带在胸前。
<br />
<br />
乌陵和土明是从神那儿得旨意的装备或程序。这个装备放在祭司胸牌的口袋里。祭司到神面前去一定要带上这个装备。<br />
<br />
Lv 8:8 (利未记8:8)
8וישם עליו את החשן ויתן אל החשן את האורים ואת התמים
8<br />
又给他佩上胸牌,把乌陵和土明放在胸牌里面
同上<br />
<br />
Dt 33:8 (申命记33:8)
8וללוי אמר תמיך ואוריך לאיש חסידך אשר נסיתו במסה תריבהו על מי מריבה׃
8<br />
<br />
论到利未,他说:“耶和华啊,愿你的土明和乌陵属于倚靠你的利未人,就是你在玛撒试验过的,在米利巴水边与他们争论过的。
<br />
<br />
这儿“愿你的土明和乌陵属于倚靠你的利未人”,意思是愿神的旨意属于倚靠神的利未人(祭司)。<br />
<br />
Ear 2:63
63ויאמר התרשתא להם אשר לא יאכלו מקדש הקדשים עד עמד כהן לאורים ולתמים׃
63省长告诉他们不可吃至圣之物,直等到有能用乌陵和土明来决疑的祭司兴起来。<br />
<br />
Ne 7:65
65ויאמר התרשתא להם אשר לא יאכלו מקדש הקדשים עד עמד הכהן לאורים ותומים׃
65省长告诉他们不可吃至圣之物,直等到有能用乌陵和土明来决疑的祭司兴起来。
<br />
<br />
这儿“直等到有能用乌陵和土明来决疑的祭司兴起来”意思是直到真正明白神旨意的祭司出现。<br />
<br />
圣经中的光有很多意思,下面是几个例子:
<br />
<br />
光是圣经中记载神的第一个创造。光将黑暗分明。<br />
<br />
光是在黑暗中为我们引路的。<br />
<br />
圣经也用光描述神的话语本身。神的话语是我们脚前的灯,路上的光。<br />
<br />
约翰福音描述神就是光。耶稣是世界的真光。
。。。。<br />
<br />
所以אורים ותמים一词来自圣经。
<br />
<br />
耶鲁大学当初成立的目的是培养牧师。牧师相当于圣经中古时的祭司的角色。所以图中的书应该指圣经。旧约圣经主要是用希伯来文写成。也就是说耶鲁大学的当初的目的是紧紧围绕圣经,培养能明白圣经真理,时常来到神面前求智慧,在神人之间代祷的牧者。Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com0tag:blogger.com,1999:blog-2317367034642612047.post-41105728134878909282009-10-28T11:06:00.000-07:002009-10-28T11:13:26.715-07:00Do not give your email id and password to any web site unless you know for sure what you are doingI and my friends have received invitation from seemly friendly email from friend email to join them such as the "Desk Top Dating".<br /><br />When you receive the email, delete it immediately and do not follow through it. This is a trap!<br /><br />Generally, for any email/web site asking for login/password for another email account, don't follow through that and delete that email unless you know you are giving information to a well trusted email service such as google. Otherwise, you are in danger of exposing your complete email contact list to an unknown user/web site.<br /><br />For example the mentioned "Desktop Dating" email, this is what the hacker site was doing:<br /><br />1. in the email it ask you to input your email id (for example <span style="border-bottom: 1px dashed rgb(0, 102, 204); cursor: pointer;" class="yshortcuts" id="lw_1256753122_0">yahoo</span>) and password,<br />2. the "desktop dating" site could use your yahoo email id and password and login to your email account and (don't be fooled by the encryption of the password that shows on the page)<br />3. read your emails and<br />4. fetch all your email contact and send the chain email to all your contacts.<br />5. The cycle continues back to step 1 for all your contacts and their contacts again and again unless you stop it from step 1.<br /><br />If you already input your email id /password to site like that, change your password now, inform your friends do not open that email from you etc.Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com0tag:blogger.com,1999:blog-2317367034642612047.post-25404706947729101132009-10-25T12:30:00.000-07:002009-10-25T12:56:52.106-07:00Use GSON to access json response in grails actionCommunicate with external service through gson<br />1. download gson package and extract the jar into lib directory<br /> http://code.google.com/p/google-gson/<br />2. grails action<br /><br />def fetch={<br />def data<br /> def reader<br /> try {<br /> URL url = new URL("http://localhost:8080/controller/action");<br /> reader = new BufferedReader(new InputStreamReader(url.openStream()));<br /> JsonParser jp = new JsonParser()<br /> JsonElement je = jp.parse(reader)<br /> data = je.getAt("data");<br /> reader.close()<br /><br /> } catch (MalformedURLException e) {<br /> // ...<br /> } catch (IOException e) {<br /> // ...<br /> } finally {<br /> reader.close()<br /> }<br /> render data;<br /> }Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com0tag:blogger.com,1999:blog-2317367034642612047.post-84826017159351597672009-10-24T20:28:00.000-07:002009-10-24T21:03:42.442-07:00Create google wave robot from grails1. use grails 1.1.1 (1.2.M3 does not work yet!!!)<br />2. create a grail project gswordwave<br /> grails create-app gswordwave<br />3. uninstall hiberante<br />grails uninstall-plugin hibernate<br /><br />4. install app engine plugin, selected jdo ( default)<br />grails install-plugin app-engine<br />5. run the app with this command:<br />grails app-engine<br />6. kill it<br />7. do initial deployment and make sure it works without wave stuff<br /><ul><li> Register your application on appengine site. gswordwave</li><li>grails set-version 1</li><li> grails app-engine package</li><li> %APP-ENGINE-HOME%/bin/appcfg.cmd update ./target/war</li></ul><br /><br />8. download the google wave client jars from<br /><ul><li> http://code.google.com/p/wave-robot-java-client/downloads/list and copy all the wave client jar to the lib directory in the grails root dir, also download servlet.api.2.5.jar into lib</li><li><span style="display: block;" id="formatbar_Buttons"><span class="down" style="display: block;" id="formatbar_InsertUnorderedList" title="Bulleted List" onmouseover="ButtonHoverOn(this);" onmouseout="ButtonHoverOff(this);" onmouseup="" onmousedown="CheckFormatting(event);FormatbarButton('richeditorframe', this, 16);ButtonMouseDown(this);"><img src="http://www.blogger.com/img/blank.gif" alt="Bulleted List" class="gl_list_bullet" border="0" /></span></span> mkdir src/java/parroty/server</li><li>copy http://code.google.com/apis/wave/extensions/robots/java-tutorial.html src/parroty/ParrotyServlet.java into the above directory src/java/parroty/server and name it ParrotyServlet.java and update the package to parroty.server.</li><li>Make a web-app/_wave dir. Create in it a capabilties.xml file and copy paste the code</li></ul><pre><br /><?xml version="1.0" encoding="utf-8"?><br /><br /><w:robot w="http://wave.google.com/extensions/robots/1.0"><br /><w:capabilities><br /><w:capability name="WAVELET_PARTICIPANTS_CHANGED" content="true"><br /></w:capability><br /><w:version>1</w:version><br /></w:capabilities><br /></w:robot></pre><br /><br />9. In your appengine-web.xml (mine is in<br />c:\users\yiguang\.grails\1.1.1\projects\gswordbible\stage)file<br />you will find your application id is there already(mine, "gswordwave").<br /><br /><br /><appengine-web-app xmlns="http://appengine.google.com/ns/1.0"><br /><application>gswordwave</application><br /><version>0.1</version><br /><sessions-enabled>true</sessions-enabled><br /><ssl-enabled>true</ssl-enabled><br /><system-properties><br /><br /></system-properties><br /><br /></appengine-web-app><br /><br /><br />10. add your ParrotyServlet config into src\templates\war\web.xml :<br /><br /><servlet><br /><servlet-name>Parroty</servlet-name><br /><servlet-class>parroty.server.ParrotyServlet</servlet-class><br /></servlet><br /><servlet-mapping><br /><servlet-name>Parroty</servlet-name><br /><url-pattern>/_wave/robot/jsonrpc</url-pattern><br /></servlet-mapping><br /><br /><br />11. do redeployment<br /><br /></pre><ul><li> grails app-engine package</li><li> %APP-ENGINE-HOME%/bin/appcfg.cmd update ./target/war</li></ul><pre>12. check it out<br /><br />http://gswordwave.appspot.com/_wave/capabilities.xml<br />13. goto your wave account and add gswordwave@appsport.com onto<br />your contact list and start to communicate with it<br /><br /></pre>Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com1tag:blogger.com,1999:blog-2317367034642612047.post-83787295218972696952009-10-21T14:57:00.000-07:002009-10-21T15:00:57.097-07:00Pintout Unicode of a StringI don't use this often but I run into the problem occasionally. For example an special character in a text from database broken XML when it is put into xml. The problem is I don't know what character is doing it. So I need to find the unicode and replace it.<br /><br />This is how to do it:<br />def thatString="sdsdsd sd fsd fsd fsd fsd fsd s ->s sd fsdf sdf "<br />that.toCharArray().each{ch-><br />printf("%04X%n", (int)ch);<br />}<br /><br /><br />This will printout all the unicode of the stringYiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com0tag:blogger.com,1999:blog-2317367034642612047.post-47935545799200649232009-09-14T07:59:00.001-07:002009-09-16T20:33:57.523-07:00GWT on Google Appengine<p style="margin-bottom: 0in;">The first part list the steps to create the first google app engine GWT applicaiton(Mostly copied from Google Instruction). The second part is more detail explanation.<br /></p><p style="margin-bottom: 0in;">1. Download Eclipse and install google app engine plugin</p> <p style="margin-bottom: 0in;"> </p> <h4 style="font-weight: normal;"> Eclipse 3.5 (Galileo) </h4><p><code> http://dl.google.com/eclipse/plugin/3.5</code></p><p style="margin-bottom: 0in;">2. Create applicaiton (areyouup)</p> <p style="margin-bottom: 0in;"> <span style="font-size:85%;">File > New > Web Application Project (areyouup, com.asianwondersnet.areyouup)</span></p> <p style="margin-bottom: 0in;"><span style="font-size:85%;"> (select workbench if not show up)</span></p> <p style="margin-bottom: 0in;">3. Running your Web Application locally</p> <p style="margin-bottom: 0in; text-align: justify;"><span style="font-size:85%;">Right-click on your web application project and select Debug As > Web Application from the popup menu.</span></p><div style="text-align: justify;"> </div><p style="margin-bottom: 0in; text-align: justify;"><span style="font-size:85%;">This action creates an Eclipse Web Application launch configuration for you and launches it. The web application launch configuration will start a server and the GWT hosted browser.</span></p><div style="text-align: justify;"> </div><p style="margin-bottom: 0in; text-align: justify;"><span style="font-size:85%;">At this point, you can set breakpoints, inspect variables and modify code as you would normally expect from a Java Eclipse debugging session.</span></p> <p style="margin-bottom: 0in;">4. Deploying your Web Application</p> <p style="margin-bottom: 0in; text-align: justify;"><span style="font-size:85%;">To deploy your web application, you will need to create an application from the App Engine Administration Console, at the following URL: https://appengine.google.com/. If you already have one, then you can skip this step.</span></p><div style="text-align: justify;"> </div><p style="margin-bottom: 0in; text-align: justify;"><span style="font-size:85%;">Once you have an application ID, just right-click on your project, and select Google > App Engine Settings... from the context menu. Enter your application ID into the Application ID text box. Click OK.</span></p><div style="text-align: justify;"> </div><p style="margin-bottom: 0in; text-align: justify;"><span style="font-size:85%;">Right-click on your project and select Google > Deploy to App Engine. In the resulting Deploy Project to Google App Engine dialog, enter your Google Account email and password.</span></p><div style="text-align: justify;"> </div><p style="margin-bottom: 0in; text-align: justify;"><span style="font-size:85%;">Note: Don't worry - the plugin doesn't store your password anywhere.</span></p><div style="text-align: justify;"> </div><p style="margin-bottom: 0in; text-align: justify;"><span style="font-size:85%;">Click Deploy.</span></p><div style="text-align: justify;"> </div><p style="margin-bottom: 0in; text-align: justify;">Go to http://application-id.appspot.com/ to see your application. </p> <p style="margin-bottom: 0in;">The following is a little bit more detail. Here are the steps to add a new service. Using TestService as example.</p><p style="margin-bottom: 0in;">1. Define TestService.java </p><p style="margin-bottom: 0in;"><span style="font-size:85%;">package com.asianwondersnet.areyouup.client;<br /><br />import com.google.gwt.user.client.rpc.RemoteService;<br />import com.google.gwt.user.client.rpc.RemoteServiceRelativePath;<br />@RemoteServiceRelativePath("test")<br />public interface TestService extends RemoteService{<br />String testServer(String name);<br />}<br /></span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">Please notice the relative path annotation above. We will use that information in web.xml</span><br /></p><p style="margin-bottom: 0in;">2. Define the TestServiceAsyn.java </p><p style="margin-bottom: 0in;"><span style="font-size:85%;">package com.asianwondersnet.areyouup.client;<br /><br />import com.google.gwt.user.client.rpc.AsyncCallback;<br /><br />public interface TestServiceAsync {<br />void testServer(String input, AsyncCallback<string> callback);<br />}</string></span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">Notice the above two service interface both has testServer. But there is a little bit difference. The second one has a AsyncCallback argument which we will show it in entrypoint code.</span></p><p style="margin-bottom: 0in;">3. Define the TestServiceImpl.java which implement the TestService interface. </p><p style="margin-bottom: 0in;"><span style="font-size:85%;">package com.asianwondersnet.areyouup.server;<br /><br />import com.asianwondersnet.areyouup.client.TestService;<br />import com.google.gwt.user.server.rpc.RemoteServiceServlet;<br /><br />@SuppressWarnings("serial")<br />public class TestServiceImpl extends RemoteServiceServlet implements TestService{<br /><br />public String testServer(String input) {<br /> StringBuffer sb=new StringBuffer();<br /> try {<br /> URL url = new URL(input);<br /> BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));<br /> String line;<br /> while ((line = reader.readLine()) != null) {<br /> sb.append(line+"\\n");<br /> }<br /> reader.close();<br /><br /> }finally{<br /> if (sb.length()==0){<br /> sb.append("Please input the full URL for fetching data!");<br /> }<br /> }<br />return sb.toString();<br /><br />}<br />}</span></p><p style="margin-bottom: 0in;">4. Add the servlet configuration in web.xml </p><p style="margin-bottom: 0in;"><span style="font-size:85%;">The name: testServlet</span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">The class: com.asianwondersnet.areyouup.server.TestServiceImpl</span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">Add the servlet mapping to web.xml<br /></span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">map /areyouup/test to testServlet<br /></span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">The relative path test is annotated in the TestService.java interface.</span></p><span style="font-size:85%;">Also define the hosting page as welcome-file-list: Areyouup.html</span><p style="margin-bottom: 0in;"><span style="font-size:85%;">The hosting page was generated automatically but you can change it from the web.xml file.</span></p><p style="margin-bottom: 0in;">5. Now the entrypoing code: Areyouup.java (This was generated automatically when the application is setup on Eclipse).</p><p style="margin-bottom: 0in;"><span style="font-size:85%;">package com.asianwondersnet.areyouup.client;<br /><br />import com.google.gwt.core.client.EntryPoint;<br /></span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">public class Areyouup implements EntryPoint{</span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">private final TestServiceAsync testService = GWT.create(TestService.class);</span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">//The entrypoint<br />public void onModuleLoad() {</span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">//Define the buttons/textfields specify style as necessary<br />final Button sendButton = new Button("Send");<br />final TextBox nameField = new TextBox();<br />nameField.setText("Input URL");<br /><br />// We can add style names to widgets<br />sendButton.addStyleName("sendButton");<br /><br />// Add the nameField and sendButton to the RootPanel<br />// Use RootPanel.get() to get the entire body element<br />RootPanel.get("nameFieldContainer").add(nameField);<br />RootPanel.get("sendButtonContainer").add(sendButton);<br /><br />// Focus the cursor on the name field when the app loads<br />nameField.setFocus(true);<br />nameField.selectAll();<br /><br />// Create the popup dialog box<br />final DialogBox dialogBox = new DialogBox();<br />dialogBox.setText("Remote Procedure Call");<br />dialogBox.setAnimationEnabled(true);<br />final Button closeButton = new Button("Close");<br />// We can set the id of a widget by accessing its Element<br />closeButton.getElement().setId("closeButton");<br />final Label textToServerLabel = new Label();<br />final HTML serverResponseLabel = new HTML();<br />VerticalPanel dialogVPanel = new VerticalPanel();<br />dialogVPanel.addStyleName("dialogVPanel");<br />dialogVPanel.add(new HTML("Sending name to the server:<b></b>"));<br />dialogVPanel.add(textToServerLabel);<br />dialogVPanel.add(new HTML("Server replies:<b></b>"));<br />dialogVPanel.add(serverResponseLabel);<br />dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);<br />dialogVPanel.add(closeButton);<br />dialogBox.setWidget(dialogVPanel);<br /><br />// Add a handler to close the DialogBox<br />closeButton.addClickHandler(new ClickHandler() {<br /> public void onClick(ClickEvent event) {<br /> dialogBox.hide();<br /> sendButton.setEnabled(true);<br /> sendButton.setFocus(true);<br /> }<br />});<br /><br />// Create a handler for the sendButton and nameField<br />class MyHandler implements ClickHandler, KeyUpHandler {<br /> /**<br /> * Fired when the user clicks on the sendButton.<br /> */<br /> public void onClick(ClickEvent event) {<br /> sendNameToServer();<br /> }<br /><br /> /**<br /> * Fired when the user types in the nameField.<br /> */<br /> public void onKeyUp(KeyUpEvent event) {<br /> if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {<br /> sendNameToServer();<br /> }<br /> }<br /><br /> /**<br /> * Send the name from the nameField to the server and wait for a response.<br /> */<br /> private void sendNameToServer() {<br /> sendButton.setEnabled(false);<br /> String textToServer = nameField.getText();<br /> textToServerLabel.setText(textToServer);<br /> serverResponseLabel.setText("");<br /> testService.testServer(textToServer,<br /> new AsyncCallback<string>() {<br /> public void onFailure(Throwable caught) {<br /> // Show the RPC error message to the user<br /> dialogBox<br /> .setText("Remote Procedure Call - Failure");<br /> serverResponseLabel<br /> .addStyleName("serverResponseLabelError");<br /> serverResponseLabel.setHTML(SERVER_ERROR);<br /> dialogBox.center();<br /> closeButton.setFocus(true);<br /> }<br /><br /> public void onSuccess(String result) {<br /> dialogBox.setText("Remote Procedure Call");<br /> serverResponseLabel<br /> .removeStyleName("serverResponseLabelError");<br /> serverResponseLabel.setHTML(result);<br /> dialogBox.center();<br /> closeButton.setFocus(true);<br /> }<br /> });<br /> }<br />}<br /><br />// Add a handler to send the name to the server<br />MyHandler handler = new MyHandler();<br />sendButton.addClickHandler(handler);<br />nameField.addKeyUpHandler(handler);<br />}<br /></string></span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">}<br /></span></p><p style="margin-bottom: 0in;"><span style="font-size:85%;">The GWT has been simplified a bit from older versions. </span>No more Service casting and the annotation also helps simplify coding.<br /></p><p style="margin-bottom: 0in;">6. Look at the Hosting page:</p><p style="margin-bottom: 0in;"> <span style="font-size:85%;"> In the html header, link style "Areyouup.css" and java script src="areyouup/areyouup.nocache.js"<br />In the body define the field "nameFieldContainer" and "sendButtonContainer" refered in the code<br /></span><br /></p><p style="margin-bottom: 0in;">Ok. Here is the play ground. It has the above function plus some more experimental image animation stuff. The URL:<a href="http://areyouup.appspot.com/">http://areyouup.appspot.com/</a><br /></p><p style="margin-bottom: 0in;"><br /></p>Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com0tag:blogger.com,1999:blog-2317367034642612047.post-62416865488290149432009-09-10T07:44:00.000-07:002009-09-10T19:49:31.440-07:00Application development process for Google Appengine on GrailsApplication development process for Google Appengine on Grails<br /><br />This is the note I took while watching<a href="http://blog.springsource.com/author/grocher/"><span style="font-weight: bold;"> </span>grocher</a> 's <a href="http://grails.org/dist/screencasts/grails-appengine-screencast.mov">Screen cast</a> from the Grails 1.1.1 <a href="http://blog.springsource.com/2009/05/14/grails-111-released-with-google-appengine-support/">announcement </a><br />So all honor goes to grocher and the grails/groovy team.<br /><br />1. grails create-app grails-music-store<br />2. Goto appengine.google.com, add new application grails-music-store . Note the app name must match<br />3. cd grails-music-store<br />4. grails uninstall-plugin hibernate<br />5. grails install-plugin app-engine<br />6. export APPENGINE_HOME=/Developer/appengine-java-sdk-1.2.0<br />7. grails app-engine<br /> then goto 8080 and see the skeleton app<br />8. grails create-domain-class Album<br /> JDO by default<br />9. grails generate-all com.music.Album<br />10. grails app-engine<br /> On Windows, After kill the application, the java process is still running. I have to kill the java process manually. (currports is a nice tool for this process)<br />11. grails app-engine package<br />12. /Developer/appengine-java-sdk-1.2.0/bin/appcfg.sh update ./target/war<br />provide password //first time<br />It will fail. Fail because of version.<br />13. grails set-version 1 (This is not the app engine version)<br />14. do step 11-12 to build and deploy again<br />/Developer/appengine-java-sdk-1.2.0/bin/appcfg.sh update ./target/war<br />15. Check it out<br /> http://grails-music-store.appspot.com/album/create (or list...)<br /><br />Of course you need to install the app engine sdk first.Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com0tag:blogger.com,1999:blog-2317367034642612047.post-6331400887391487942009-08-15T21:47:00.000-07:002009-08-18T18:54:56.322-07:00Bible on TwitterI started to read novels on Twitter(total length within 140 chars) recently.<br /><br />Each Bible verse has a story better than those novels. So I am starting to publish Bible verses on Twitter. This is <a href="http://www.twitter.com/gsword">my twitter with Bible verses</a><br /><br />If you are familiar with the Bible verse, this may serve as a good reminder and a starting point of inspiration so that you can reflect on the verse.<br /><br />If you are not familiar with the particular Bible verse, this may serve as a challenge for you to figure out the context of the verse by going back to the Bible and read around the verse and get familiar with it. <a href="http://rock.ccim.org/gsword/gbook/v">GSword</a> is a comprehensive Bible study tool you can use for this purpose.<br /><br />Any way, this is yet another way to read Bible. I hope you find it fun and challenging.Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com0tag:blogger.com,1999:blog-2317367034642612047.post-49700898899353295352009-08-15T21:25:00.000-07:002009-08-15T21:38:33.018-07:00Twitter through Grails ApplicationDoing twitter is very easy in grails application. <br /><br />Here is a barebone twitter service code. Simply put it into your grails-app/services directory<br /><br />import twitter4j.Twitter<br /><br />class TwitterService { <br /> boolean transactional = false<br /> def update(String message) {<br /> Twitter twitter = new Twitter("username", "password"); <br /> twitter.updateStatus(message);<br /> }<br />}<br /><br />Notice the <a href="http://yusuke.homeip.net/twitter4j/en/index.html">twitter4j</a> package is used here<br /><br />In your controller or job, define the service<br />def twitterService<br /><br />whereever you want to update your twitter, call the service as following:<br />twitterService.update("I am fine so far")Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com0tag:blogger.com,1999:blog-2317367034642612047.post-89021849723119429472009-08-07T11:29:00.000-07:002009-08-14T08:03:40.226-07:00Google Spell CheckerInspired by the <a href="http://immike.net/blog/2007/04/07/hacking-google-spell-checker-for-fun-and-profit/">php blog</a> on google spell checker, Here is my Groovy version:<br /><br />def spellchecker(String word){<br /> <br /> def path = "/tbproxy/spell?lang=en&hl=en";<br /> def urlString = "https://www.google.com"+path<br /> def url = new URL(urlString)<br /> def connection = url.openConnection()<br /> connection.setRequestMethod("POST")<br /> connection.doOutput = true<br /> def post ='<spellrequest textalreadyclipped="0" ignoredups="1" ignoredigits="1" ignoreallcaps="0"><text>'+word+'</text></spellrequest>'<br /> def writer = new OutputStreamWriter(connection.outputStream)<br /> writer.write(post)<br /> writer.flush()<br /> writer.close()<br /> connection.connect()<br /> println "The query:"+word<br /> println "The response:\n"+connection.content.text<br />}<br /><br />def x="""<br />The suggestions are tab-delineated. The .o. attribute is an offset from the start of your query to the misspelled word. .l. is the length of the misspelled w<br />ord. .s. is the confidence of Google.s suggestion (presumably higher is better, but I.ve only gotten 0 or 1).<br />"""<br />println x<br />spellchecker("love is patient")<br />spellchecker("weee nottt sxdsasd")<br />spellchecker("xxx")<br />spellchecker("toatlygrbg")<br /><br />The following is the printout:<br /><br />The query:love is patient<br />The response:<br /><spellresult error="0" clipped="0" charschecked="15"></spellresult><br />The query:weee nottt sxdsasd<br />The response:<br /><spellresult error="0" clipped="0" charschecked="18"><c o="0" l="4" s="0">weer wee were whee weed</c><c o="5" l="5" s="1">not knotty Natty Netty natty</c><c o="11" l="7" s="1">sadsasd</c></spellresult><br />The query:xxx<br />The response:<br /><spellresult error="0" clipped="0" charschecked="3"></spellresult><br />The query:toatlygrbg<br />The response:<br /><spellresult error="0" clipped="0" charschecked="10"><c o="0" l="10" s="1">totally grbg</c></spellresult>Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com2tag:blogger.com,1999:blog-2317367034642612047.post-65106505590060153502009-06-14T12:55:00.000-07:002009-06-14T13:25:20.919-07:00Java Internationalization and Localization(I18N, L10N)<span style="font-size:180%;"><b><a href="http://www.blogger.com/post-edit.g?blogID=2317367034642612047&postID=6510650559006015350#0.1.Preface%7Coutline"></a></b></span><a name="0.1.Preface|outline"></a>Preface <p>This document summarizes some important Java classes and techniques for I18N development. I have made most examples to show the point directly. Many Java based web frameworks(JSF, STRUTS, GRAILS etc) have made I18N/L10N very simple, but the fundamentals behind the frameworks are still the same. So hope the blog is still relevant.<br /></p><p>Many of the samples are adopted from the resources listed in the reference area, especially the book "Java Internationalization<java>" by Andrew Deitsch & David Czarnecki from O’Reilly. This post is taken from my note while reading the I18N book and websites many years ago.<br /></java></p> <h2 class="western"><a name="0.2.Internationalization Tips|outline"></a> Internationalization Tips</h2> <ol><li><p>Don't assume all letters of the alphabet fall between A and Z. Use Character.</p> </li><li><p>Don't hardcode strings. Use resource bundle.</p> </li><li><p>Don’t concatenate strings. Use MessageFormat/ChoiceFormat.</p> </li><li><p>Use InputStreamReader/OutputStreamWriter for reading/writing.</p> </li><li><p>Sort using Collator/RuleBasedCollator.</p> </li><li><p>Use ComponentOrientation for GUI layout.</p> </li><li><p>Don't hardcode fonts.</p> </li><li><p>Use NumberFormat/DecimalFormat to format numbers.</p> </li><li><p>Use DateFormat/SimpleDateFormat to display dates.</p> </li></ol> <h2 class="western"><a name="0.3.Culturally Dependent Data|outline"></a> Culturally Dependent Data</h2> <p style="margin: 0.19in 0.5in;"> In developing internationalized applications, you need to identify culturally dependent data. The following lists some examples of culturally dependent data: </p> <ul><li><p style="margin-right: 0.5in; margin-top: 0.19in; margin-bottom: 0in;"> Messages </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Labels on GUI components </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Online help </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Sounds </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Colors </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Graphics </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Icons </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Dates </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Times </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Numbers </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Currencies </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Measurements </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Phone numbers </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Honorifics and personal titles </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;">Postal addresses </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0.19in;">Page layouts </p> </li></ul> <p>This document introduces some of the key java classes for developing international applications. </p> <h2 class="western"><a name="0.4.Locale|outline"></a>Locale</h2> <p style="margin-bottom: 0in;">A <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span> object represents a specific geographical, political, or cultural region. An operation that requires a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span> to perform its task is called <em>locale-sensitive</em> and uses the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span> to tailor information for the user.</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">Here are the Locale constructors:</p> <p style="margin-left: 0.5in; margin-right: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale(String language, String country)</span></span></p> <p style="margin-left: 0.5in; margin-right: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale(String language, String country, String variant)</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><br /></p> <p style="margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">For Example:</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale ja = new Locale("ja", "JP");</span></span></p> <h2 class="western"><a name="0.5.ResourceBundle|outline"></a>ResourceBundle</h2> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span> objects contain locale-specific objects. When you need a locale-specific object, you fetch it from a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle.</span></span></p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">For example, you have the following resource bundle,</p> <p style="margin-bottom: 0in;"> Image</p> <p style="margin-bottom: 0in;"> Image_ja_JP</p> <p style="margin-bottom: 0in;"> Image_fr_FR</p> <p style="margin-bottom: 0in;"> Image_zh</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">Each of these <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">resource bundles</span></span> is a set of related subclasses that share the same base name Image. To select the appropriate <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span>, invoke the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle.getBundle</span></span> method. For example:</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale currentLocale = new Locale("fr", "FR");</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle myimages =</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle.getBundle("Image", currentLocale);</span></span></p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">To get specific object in the bundle, use get method of ResourceBundle with the key. I.e, </p> <p style="text-indent: 0.5in; margin-bottom: 0in;">String menubar = myimages.getString(“MenuBar”);</p> <p style="text-indent: 0.5in; margin-bottom: 0in;"><br /></p> <p style="text-indent: 0.5in; margin-bottom: 0in;">ResourceBundle may be back by property files or ListResourceBunle.</p> <h2 class="western"><a name="0.6.Property Files|outline"></a>Property Files</h2> <p style="margin-bottom: 0in;">Property file contains properties defined in the format of key=values. You should always create a default properties file. The name of this file begins with the base name of your <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span> and ends with <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">.properties</span></span> suffix. For example:</p> <p style="margin-bottom: 0in;"><br /></p> <p style="text-indent: 0.5in; margin-bottom: 0in;">Image.properties</p> <p style="margin-bottom: 0in;"> Image_ja_JP.properties</p> <p style="margin-bottom: 0in;"> Image_fr_FR.properties</p> <p style="margin-bottom: 0in;"> Image_zh.properties</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">This example show how resources bundle is created:</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle labels =</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle.getBundle("Image", currentLocale);</span></span></p> <p style="margin: 0.19in 0.5in;"> The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">getBundle</span></span> method first looks for a class file that matches the base name and the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span>. If it can't find a class file, it then checks for properties files. </p> <h2 class="western"><a name="0.7.ListResourceBundle|outline"></a>ListResourceBundle</h2> <p style="margin: 0.19in 0.5in;"> When you have non-string object in resource bundle, you need to use ListResourceBundle. Here is a list resource bundle class example:</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">public class StatsBundle_ja_JP extends ListResourceBundle {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">public Object[][] getContents() {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> return contents;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">private Object[][] contents = {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> { "GDP", new Integer(21300) },</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> { "Population", new Integer(125449703) },</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> { "Literacy", new Double(0.99) },</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">};</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin: 0.19in 0.5in;"> To create the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ListResourceBundle</span></span>, invoke the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">getBundle </span></span>method. </p> <h2 class="western"><a name="0.8.NumberFormat|outline"></a>NumberFormat</h2> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">NumberFormat</span></span> is the abstract base class for all number formats.</p> <p style="margin-bottom: 0in;">Here are some examples:</p> <p style="margin-bottom: 0in;">Format number:</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">myString = NumberFormat.getNumberInstance(yourlocale).format(myNumber);</span></span></p> <p style="margin-right: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Format Currency:</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">currencyFormatter = NumberFormat.getCurrencyInstance(currentLocale).format(currency);</span></span></p> <p style="margin-left: 0.63in; margin-right: 1in; text-indent: -0.13in; margin-bottom: 0in;"><br /></p> <p style="margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Format Percentage:</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">percentFormatter = NumberFormat.getPercentInstance(currentLocale).format(percent);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Examples for NumberFormat:</span></span></p> <dl><dd> <table style="color: rgb(0, 0, 0);" border="1" cellpadding="7" cellspacing="0" width="543"> <col width="64"> <col width="94"> <col width="94"> <col width="139"> <col width="80"> <tbody><tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Number</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Number</span></span></p> </td> <td width="139"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Currency</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Percentage</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fr_FR</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123 456</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">345 987,246</span></span></p> </td> <td width="139"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9 876 543,21 F</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">75%</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">de_DE</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123.456</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">345.987,246</span></span></p> </td> <td width="139"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9.876.543,21 DM</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">75%</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">en_US</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123,456</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">345,987.246</span></span></p> </td> <td width="139"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">$9,876,543.21</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">75%</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ja_JP</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123,456</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">345,987.246</span></span></p> </td> <td width="139"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">¥</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9,876,543</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">75%</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">zh_TW</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123,456</span></span></p> </td> <td width="94"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">345,987.246</span></span></p> </td> <td width="139"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">NT$9,876,543.21</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">75%</span></span></p> </td> </tr> </tbody></table> </dd></dl> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <h2 class="western"><a name="0.9.DecimalFormat|outline"></a>DecimalFormat</h2> <p style="margin-bottom: 0in;">You can use the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DecimalFormat</span></span> class to format decimal numbers into locale-specific strings with patterns.</p> <p style="margin: 0.19in 0.5in;"> You can design your own format patterns for numbers by following the rules specified by the following BNF diagram: </p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">pattern := subpattern{;subpattern}</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">subpattern := {prefix}integer{.fraction}{suffix}</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">prefix := '\\u0000'..'\\uFFFD' - specialCharacters</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">suffix := '\\u0000'..'\\uFFFD' - specialCharacters</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">integer := '#'* '0'* '0'</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fraction := '0'* '#'*</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">For example:</p> <p style="margin-left: 0.5in; margin-bottom: 0in;">String pattern=”###.##”;</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DecimalFormat myFormatter = new DecimalFormat(pattern);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String output = myFormatter.format(value);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale sensitive format with pattern:</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">NumberFormat nf = NumberFormat.getNumberInstance(currentlocale);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DecimalFormat df = (DecimalFormat)nf;</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">df.applyPattern(pattern);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String output = df.format(value);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Examples DecimalFormat:</span></span></p> <dl><dd> <table style="color: rgb(0, 0, 0);" border="1" cellpadding="7" cellspacing="0" width="524"> <col width="148"> <col width="220"> <col width="112"> <tbody><tr valign="top"> <td width="148"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Pattern</span></span></p> </td> <td width="220"> <p style="margin-right: 0.23in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Output of 123456.789</span></span></p> </td> <td width="112"> <p style="margin-right: 0.18in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span></p> </td> </tr> <tr valign="top"> <td width="148"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">###,###.###</span></span></p> </td> <td width="220"> <p style="margin-right: 0.23in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123,456.789 </span></span> </p> </td> <td width="112"> <p style="margin-right: 0.18in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">en_US</span></span></p> </td> </tr> <tr valign="top"> <td width="148"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">###,###.###</span></span></p> </td> <td width="220"> <p style="margin-right: 0.23in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123.456,789 </span></span> </p> </td> <td width="112"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">de_DE</span></span></p> </td> </tr> <tr valign="top"> <td width="148"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">###,###.###</span></span></p> </td> <td width="220"> <p style="margin-right: 0.23in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123 456,789 </span></span> </p> </td> <td width="112"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fr_FR</span></span></p> </td> </tr> <tr valign="top"> <td width="148"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">###,###.###</span></span></p> </td> <td width="220"> <p style="margin-right: 0.23in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123,456.789 </span></span> </p> </td> <td width="112"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ja_JP</span></span></p> </td> </tr> <tr valign="top"> <td width="148"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">###,###.###</span></span></p> </td> <td width="220"> <p style="margin-right: 0.23in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">123,456.789 </span></span> </p> </td> <td width="112"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">zh_TW</span></span></p> </td> </tr> </tbody></table> </dd></dl> <h2 class="western"><a name="0.10.DecimalFormatSymbols|outline"></a>DecimalFormatSymbols</h2> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DecimalFormatSymbols unusualSymbols =</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">new DecimalFormatSymbols(currentLocale);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">unusualSymbols.setDecimalSeparator('|');</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">unusualSymbols.setGroupingSeparator('^');</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String strange = "#,##0.###";</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DecimalFormat weirdFormatter = new DecimalFormat(strange, unusualSymbols);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">weirdFormatter.setGroupingSize(4);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String bizarre = weirdFormatter.format(12345.678);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">System.out.println(bizarre);</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">output: 1^2345|678</span></span></p> <h2 class="western"><a name="0.11.DateFormat|outline"></a>DateFormat</h2> <p style="margin-bottom: 0in;">Format date:</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Date today = new Date();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String dateOut;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DateFormat dateFormatter;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">dateFormatter = DateFormat.getDateInstance(DateFormat.DEFAULT, currentLocale);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">dateOut = dateFormatter.format(today);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DateFormat timeFormatter =</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Format time: DateFormat.getTimeInstance(DateFormat.DEFAULT,</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">currentLocale);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Format both date and time:</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DateFormat formatter = </span></span> </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, currentLocale);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Example: Date styles</span></span></p> <dl><dd> <table style="color: rgb(0, 0, 0);" border="1" cellpadding="7" cellspacing="0" width="543"> <col width="52"> <col width="81"> <col width="67"> <col width="81"> <col width="88"> <col width="88"> <tbody><tr valign="top"> <td width="52"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Default</span></span></p> </td> <td width="67"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">SHORT</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MEDIUM</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">LONG</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">FULL</span></span></p> </td> </tr> <tr valign="top"> <td width="52"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">En_US</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Jan 9, 2003</span></span></p> </td> <td width="67"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">1/9/03</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Jan 9, 2003</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">January 9, 2003</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Thursday, January 9, 2003</span></span></p> </td> </tr> <tr valign="top"> <td width="52"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fr_FR</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9 janv. 03</span></span></p> </td> <td width="67"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">09/01/03</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9 janv. 03</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9 janvier 2003</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">jeudi 9 janvier 2003</span></span></p> </td> </tr> <tr valign="top"> <td width="52"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">de_DE</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">09.01.2003</span></span></p> </td> <td width="67"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">09.01.03</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">09.01.2003</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9. Januar 2003</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Donnerstag, 9. Januar 2003</span></span></p> </td> </tr> <tr valign="top"> <td width="52"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ja_JP</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2003/01/09</span></span></p> </td> <td width="67"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">03/01/09</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2003/01/09</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2003/01/09</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2003</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">年</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">1</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">月</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">日</span></span></p> </td> </tr> <tr valign="top"> <td width="52"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">zh_TW</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2003/1/9</span></span></p> </td> <td width="67"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2003/1/9</span></span></p> </td> <td width="81"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2003/1/9</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2003</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">年</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">1</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">月</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">日</span></span></p> </td> <td width="88"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2003</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">年</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">1</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">月</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">9</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">日</span></span></p> </td> </tr> </tbody></table> </dd></dl> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Example: Time Style</span></span></p> <dl><dd> <table style="color: rgb(0, 0, 0);" border="1" cellpadding="7" cellspacing="0" rules="cols" width="583"> <col width="47"> <col width="10"> <col width="42"> <col width="9"> <col width="27"> <col width="1"> <col width="52"> <col width="0"> <col width="115"> <col width="2"> <col width="105"> <col width="3"> <tbody> <tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span></p> </td> <td colspan="2" width="67"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Default</span></span></p> </td> <td colspan="2" width="50"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">SHORT</span></span></p> </td> <td colspan="2" width="67"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MEDIUM</span></span></p> </td> <td colspan="3" width="144"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">LONG</span></span></p> </td> <td colspan="2" width="122"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">FULL</span></span></p> </td> </tr> </tbody> <tbody> <tr valign="top"> <td colspan="2" width="72"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">en_US</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">3:07:28 PM</span></span></p> </td> <td colspan="2" width="43"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">3:07 PM</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">3:07:28 PM</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">3:07:28 PM GMT-05:00</span></span></p> </td> <td colspan="2" width="121"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">3:07:28 PM GMT-05:00</span></span></p> </td> <td width="3"> <p><br /></p> </td> </tr> <tr valign="top"> <td colspan="2" width="72"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fr_FR</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07:28</span></span></p> </td> <td colspan="2" width="43"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07:28</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07:28 GMT-05:00</span></span></p> </td> <td colspan="2" width="121"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15 h 07 GMT-05:00</span></span></p> </td> <td width="3"> <p><br /></p> </td> </tr> <tr valign="top"> <td colspan="2" width="72"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">de_DE</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07:28</span></span></p> </td> <td colspan="2" width="43"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07:28</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07:28 GMT-05:00</span></span></p> </td> <td colspan="2" width="121"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15.07 Uhr GMT-05:00</span></span></p> </td> <td width="3"> <p><br /></p> </td> </tr> <tr valign="top"> <td colspan="2" width="72"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ja_JP</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07:28</span></span></p> </td> <td colspan="2" width="43"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07:28</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15:07:28:GMT-05:00</span></span></p> </td> <td colspan="2" width="121"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">時</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">07</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">分</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">28</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">秒</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">GMT-05:00</span></span></p> </td> <td width="3"> <p><br /></p> </td> </tr> <tr valign="top"> <td colspan="2" width="72"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">zh_TW</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">下午 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">03:07:28</span></span></p> </td> <td colspan="2" width="43"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">下午 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">3:07</span></span></p> </td> <td colspan="2" width="65"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">下午 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">03:07:28</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">下午</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">03</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">時</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">07</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">分</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">28</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">秒</span></span></span></p> </td> <td colspan="2" width="121"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">下午</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">03</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">時</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">07</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">分</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">28</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">秒 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">GMT-05:00</span></span></p> </td> <td width="3"> <p><br /></p> </td> </tr> </tbody> </table> </dd></dl> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Example: Both Date and Time Style</span></span></p> <dl><dd> <table style="color: rgb(0, 0, 0);" border="1" cellpadding="7" cellspacing="0" width="610"> <col width="47"> <col width="80"> <col width="80"> <col width="80"> <col width="115"> <col width="120"> <tbody><tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Default</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">SHORT</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MEDIUM</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">LONG</span></span></p> </td> <td width="120"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">FULL</span></span></p> </td> </tr> <tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">en_US</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Dec 12, 2002 3:07:28 PM</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12/12/02 3:07 PM</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Dec 12, 2002 3:07:28 PM</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">December 12, 2002 3:07:28 PM GMT-05:00</span></span></p> </td> <td width="120"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Thursday, December 12, 2002 3:07:28 PM GMT-05:00</span></span></p> </td> </tr> <tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fr_FR</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12 déc. 02 15:07:28</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12/12/02 15:07</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12 déc. 02 15:07:28</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12 décembre 2002 15:07:28 GMT-05:00</span></span></p> </td> <td width="120"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">jeudi 12 décembre 2002 15 h 07 GMT-05:00</span></span></p> </td> </tr> <tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">de_DE</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12.12.2002 15:07:28</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12.12.02 15:07</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12.12.2002 15:07:28</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12. Dezember 2002 15:07:28 GMT-05:00</span></span></p> </td> <td width="120"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Donnerstag, 12. Dezember 2002 15.07 Uhr GMT-05:00</span></span></p> </td> </tr> <tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ja_JP</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002/12/12 15:07:28</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">02/12/12 15:07</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002/12/12 15:07:28</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002/12/12 15:07:28:GMT-05:00</span></span></p> </td> <td width="120"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">年</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">月</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">日 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">15</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">時</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">07</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">分</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">28</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">秒</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">GMT-05:00</span></span></p> </td> </tr> <tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">zh_TW</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002/12/12 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">下午 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">03:07:28</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002/12/12 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">下午 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">3:07</span></span></p> </td> <td width="80"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002/12/12 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">下午 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">03:07:28</span></span></p> </td> <td width="115"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">年</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">月</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">日 下午</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">03</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">時</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">07</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">分</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">28</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">秒</span></span></span></p> </td> <td width="120"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">年</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">月</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">日 下午</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">03</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">時</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">07</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">分</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">28</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-TW">秒 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">GMT-05:00</span></span></p> </td> </tr> </tbody></table> </dd></dl> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <h2 class="western"><a name="0.12.SimpleDateFormat|outline"></a>SimpleDateFormat</h2> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">If the DateFormat does not satisfy your need, you can define your own pattern to format date use SimpleDateFormat.</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Date today= new Date();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String output;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">SimpleDateFormat formatter;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">formatter = new SimpleDateFormat(pattern, currentLocale);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">output = formatter.format(today);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">System.out.println(pattern + " " + output);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">formatter = new SimpleDateFormat("EEE d MMM yy", currentLocale);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">output = formatter.format(today);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Examples SimpleDateFormat:</span></span></p> <dl><dd> <table style="color: rgb(0, 0, 0);" border="1" cellpadding="7" cellspacing="0" width="543"> <col width="76"> <col width="202"> <col width="221"> <tbody><tr valign="top"> <td width="76"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span></p> </td> <td width="202"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Pattern</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Output</span></span></p> </td> </tr> <tr valign="top"> <td width="76"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">en_US</span></span></p> </td> <td width="202"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">yyyy.MM.dd G 'at' hh:mm:ss z</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002.12.12 AD at 03:29:36 GMT-05:00</span></span></p> </td> </tr> <tr valign="top"> <td width="76"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Fr_FR</span></span></p> </td> <td width="202"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">yyyy.MM.dd G 'at' hh:mm:ss z</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002.12.12 ap. J.-C. at 03:29:36 GMT-05:00</span></span></p> </td> </tr> <tr valign="top"> <td width="76"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">de_DE</span></span></p> </td> <td width="202"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">yyyy.MM.dd G 'at' hh:mm:ss z</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002.12.12 n. Chr. at 03:29:36 GMT-05:00</span></span></p> </td> </tr> <tr valign="top"> <td width="76"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ja_JP</span></span></p> </td> <td width="202"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">yyyy.MM.dd G 'at' hh:mm:ss z</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002.12.12 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">西暦 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">at 03:29:36 GMT-05:00</span></span></p> </td> </tr> <tr valign="top"> <td width="76"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">zh_TW</span></span></p> </td> <td width="202"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">yyyy.MM.dd G 'at' hh:mm:ss z</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002.12.12 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">民國 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">at 03:29:36 GMT-05:00</span></span></p> </td> </tr> <tr valign="top"> <td width="76"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Zh_CN</span></span></p> </td> <td width="202"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">yyyy.MM.dd G 'at' hh:mm:ss z</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002.12.12 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">公元 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">at 03:40:52 GMT-05:00</span></span></p> </td> </tr> </tbody></table> </dd></dl> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <h2 class="western"><a name="0.13.MessageFormat|outline"></a>MessageFormat</h2> <p style="margin-bottom: 0in;">A compound message may contain several kinds of variables: dates, times, strings, numbers, currencies, and percentages. To format a compound message in a locale-independent manner, you construct a pattern that you apply to a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat</span></span> object, and store this pattern in a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span>.</p> <p>1. Identify the Variables in the Message</p> <p style="margin: 0.19in 0.5in;"> Suppose that you want to internationalize the following message: </p> <p style="margin: 0.19in 0.5in;" align="center"> </p> <p style="margin: 0.19in 0.5in;"> Notice that we've underlined the variable data and have identified what kind of objects will represent this data. </p> <p>2. Isolate the Message Pattern in a ResourceBundle</p> <p style="margin: 0.19in 0.5in;"> Store the message in a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span> named <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageBundle</span></span>, as follows: </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle messages =</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle.getBundle("MessageBundle", currentLocale);</span></span></p> <p style="margin: 0.19in 0.5in;"> This <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span> is backed by a properties file for each <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span>. The properties file for U.S. English is named <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageBundle_en_US.properties</span></span>. The contents of this file is as follows: </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">template = At {2,time,short} on {2,date,long}, we detected \</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> {1,number,integer} spaceships on the planet {0}.</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">planet = Mars</span></span></p> <p style="margin: 0.19in 0.5in;"> The first line of the properties file contains the message pattern. If you compare this pattern with the message text shown in step 1, you'll see that an argument enclosed in braces replaces each variable in the message text. Each argument starts with a digit called the argument number, which matches the index of an element in an <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Object</span></span> array that holds the argument values. Note that in the pattern the argument numbers are not in any particular order. You can place the arguments anywhere in the pattern. The only requirement is that the argument number has a matching element in the array of argument values. </p> <p style="margin: 0.19in 0.5in;"> The next step discusses the argument value array, but first let's look at each of the arguments in the pattern. The following table provides some details about the arguments: </p> <dl><dd> <table frame="below" border="1" cellpadding="1" cellspacing="3" width="538"> <col width="98"> <col width="427"> <tbody><tr> <td colspan="2" width="530"> <p><b>Arguments for </b><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span style="font-family:Times New Roman,serif;"><span style="font-size:130%;"><b>template</b></span></span></span></span><b> in </b><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span style="font-family:Times New Roman,serif;"><span style="font-size:130%;"><b>MessageBundle_en_US.properties</b></span></span></span></span></p> </td> </tr> <tr> <td width="98"> <p align="center"><b>Argument </b> </p> </td> <td width="427"> <p align="center"><b>Description </b> </p> </td> </tr> <tr> <td width="98"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{2,time, short}</span></span> </p> </td> <td width="427"> <p>The time portion of a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Date</span></span> object. The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">short</span></span> style specifies the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">DateFormat.SHORT</span></span> formatting style. </p> </td> </tr> <tr> <td width="98"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{2,date, long}</span></span> </p> </td> <td width="427"> <p>The date portion of a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Date</span></span> object. The same <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Date</span></span> object is used for both the date and time variables. In the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Object</span></span> array of arguments the index of the element holding the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Date</span></span> object is 2. (This is described in the next step.) </p> </td> </tr> <tr> <td width="98"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{1,number, integer}</span></span> </p> </td> <td width="427"> <p>A <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Number</span></span> object, further qualified with the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">integer</span></span> number style. </p> </td> </tr> <tr> <td width="98"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{0}</span></span> </p> </td> <td width="427"> <p>The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> in the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span> that corresponds to the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">planet</span></span> key. </p> </td> </tr> </tbody></table> </dd></dl> <p style="margin: 0.19in 0.5in;"><br /><br /></p> <p>3. Set the Message Arguments</p> <p style="margin: 0.19in 0.5in;"> The following lines of code assign values to each argument in the pattern. The indexes of the elements in the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageArguments</span></span> array match the argument numbers in the pattern. For example, the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Integer </span></span>element at index 1 corresponds to the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{1,number,integer}</span></span> argument in the pattern. Because it must be translated, the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> object at element 0 will be fetched from the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span> with the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">getString</span></span> method. Here is the code that defines the array of message arguments: </p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Object[] messageArguments = {</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messages.getString("planet"),</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">new Integer(7),</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">new Date()</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">};</span></span></p> <p>4. Create the Formatter</p> <p style="margin: 0.19in 0.5in;"> Next, create a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat</span></span> object. You set the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span> because the message contains <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Date</span></span> and <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Number</span></span> objects, which should be formatted in a locale-sensitive manner. </p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat formatter = new MessageFormat("");</span></span></p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">formatter.setLocale(currentLocale);</span></span></p> <p>5. Format the Message Using the Pattern and the Arguments</p> <p style="margin: 0.19in 0.5in;"> This step shows how the pattern, message arguments, and formatter all work together. First, fetch the pattern <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> from the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle </span></span>with the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">getString</span></span> method. The key to the pattern is <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">template</span></span>. Pass the pattern <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> to the formatter with the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">applyPattern </span></span>method. Then format the message using the array of message arguments, by invoking the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">format</span></span> method. The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> returned by the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">format</span></span> method is ready to be displayed. All of this is accomplished with just two lines of code: </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">formatter.applyPattern(messages.getString("template"));</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String output = formatter.format(messageArguments);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Example MessageFormat:</span></span></p> <dl><dd> <table style="color: rgb(0, 0, 0);" border="1" cellpadding="7" cellspacing="0" width="583"> <col width="47"> <col width="288"> <col width="203"> <tbody><tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span></p> </td> <td width="288"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Pattern</span></span></p> </td> <td width="203"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Output</span></span></p> </td> </tr> <tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">en_US</span></span></p> </td> <td width="288"> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">template = At {2,time,short} on {2,date,long}, we detected \</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> {1,number,integer} spaceships on the planet {0}.</span></span></p> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">planet = Mars</span></span></p> </td> <td width="203"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">At 4:16 PM on December 12, 2002, we detected 7 spaceships on the planet Mars.</span></span></p> </td> </tr> <tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">de_DE</span></span></p> </td> <td width="288"> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">emplate = Um {2,time,short} Uhr am {2,date,long}, haben wir \</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> {1,number,integer} Raumschiffe auf dem planeten {0} entdeckt.</span></span></p> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">planet = Mars</span></span></p> </td> <td width="203"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Um 16:16 Uhr am 12. Dezember 2002, haben wir 7 Raumschiffe auf dem planeten Mars entdeckt.</span></span></p> </td> </tr> <tr valign="top"> <td width="47"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">zh</span></span></p> </td> <td width="288"> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">template =</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">我们在</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{2,date,long}{2,time,short} </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">在 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{0}</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">上探测到</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{1,number,integer}</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">个太空船。</span></span></span></p> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">planet = </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">火星</span></span></span></p> </td> <td width="203"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">我们在</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2002</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">年</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">月</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">12</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">日下午</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">4:16 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">在 火星上探测到</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">7</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">个太空船。</span></span></span></p> </td> </tr> </tbody></table> </dd></dl> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <h2 class="western"><a name="0.14.ChoiceFormat|outline"></a>ChoiceFormat</h2> <p style="margin-bottom: 0in;">The words in a message may vary if both plural and singular word forms are possible. With the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat</span></span> class, you can map a number to a word or a phrase, allowing you to construct grammatically correct messages.</p> <p>1. Define the Message Pattern</p> <p style="margin: 0.19in 0.5in;"> First, identify the variables in the message: </p> <p style="margin: 0.19in 0.5in;" align="center"> </p> <p style="margin: 0.19in 0.5in;"> Next, replace the variables in the message with arguments, creating a pattern that can be applied to a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat</span></span> object: </p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">There {0} on {1}.</span></span></p> <p style="margin: 0.19in 0.5in;"> The argument for the disk name, which is represented by<span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{1}</span></span>, is easy enough to deal with. You just treat it like any other <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> variable in a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat</span></span> pattern. This argument matches the element at index 1 in the array of argument values. </p> <p style="margin: 0.19in 0.5in;"> Dealing with argument<span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{0}</span></span> is more complex, for a couple of reasons: </p> <ul><li><p style="margin-right: 0.5in; margin-top: 0.19in; margin-bottom: 0in;"> The phrase that this argument replaces varies with the number of files. To construct this phrase at run time, you need to map the number of files to a particular <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span>. For example, the number 1 will map to the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> containing the phrase <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">is one file</span></span>. The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat</span></span> class allows you to perform the necessary mapping. </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0.19in;">If the disk contains multiple files, the phrase includes an integer. The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat</span></span> class lets you insert a number into a phrase. </p> </li></ul> <p>2. Create a ResourceBundle</p> <p style="margin: 0.19in 0.5in;"> Because the message text must be translated, isolate it in a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span>: </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle bundle =</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle.getBundle("ChoiceBundle", currentLocale);</span></span></p> <p style="margin: 0.19in 0.5in;"> The sample program backs the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span> with properties files. The ChoiceBundle_en_US.properties file contains the following lines: </p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">pattern = There {0} on {1}.</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">noFiles = are no files</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">oneFile = is one file</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">multipleFiles = are {2} files</span></span></p> <p style="margin: 0.19in 0.5in;"> The contents of this properties file show how the message will be constructed and formatted. The first line contains the pattern for <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat</span></span>. The other lines contain phrases that will replace argument <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{0}</span></span> in the pattern. The phrase for the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">multipleFiles</span></span> key contains the argument <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{2}</span></span>, which will be replaced by a number. </p> <p style="margin-left: 0.5in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Check out the following table for more examples.</span></span></p> <p>3. Create a Message Formatter</p> <p style="margin: 0.19in 0.5in;"> In this step you instantiate <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat</span></span> and set its <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span>: </p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat messageForm = new MessageFormat("");</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageForm.setLocale(currentLocale);</span></span></p> <p>4. Create a Choice Formatter</p> <p style="margin: 0.19in 0.5in;"> The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat</span></span> object allows you to choose, based on a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">double </span></span>number, a particular <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span>. The range of <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">double</span></span> numbers, and the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> objects to which they map, are specified in arrays: </p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">double[] fileLimits = {0,1,2};</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String [] fileStrings = {</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">bundle.getString("noFiles"),</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">bundle.getString("oneFile"),</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">bundle.getString("multipleFiles")</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">};</span></span></p> <p style="margin: 0.19in 0.5in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat</span></span> maps each element in the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">double</span></span> array to the element in the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> array that has the same index. In the sample code the 0 maps to the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> returned by calling <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">bundle.getString("noFiles")</span></span>. By coincidence the index is the same as the value in the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fileLimits</span></span> array. If the code had set <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fileLimits[0]</span></span> to seven, <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat</span></span> would map the number 7 to <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fileStrings[0]</span></span>. </p> <p style="margin: 0.19in 0.5in;"> You specify the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">double</span></span> and <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> arrays when instantiating <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat</span></span>: </p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat choiceForm = new ChoiceFormat(fileLimits, fileStrings);</span></span></p> <p>5. Apply the Pattern</p> <p style="margin: 0.19in 0.5in;"> Remember the pattern you constructed in step 1? It's time to retrieve the pattern from the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ResourceBundle</span></span> and apply it to the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat </span></span>object: </p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String pattern = bundle.getString("pattern");</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageForm.applyPattern(pattern);</span></span></p> <p>6. Assign the Formats</p> <p style="margin: 0.19in 0.5in;"> In this step you assign to the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat</span></span> object the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat</span></span> object created in step 4: </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Format[] formats = {choiceForm, null, NumberFormat.getInstance()};</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageForm.setFormatsByArgumentIndex(formats); //(After Java1.4)</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">or</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageForm.setFormats(formats);//This method has trouble dealing with complicated grammar. </span></span> </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">setFormats</span></span>ByArgumentIndex (formats); method assigns <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Format</span></span> objects to the arguments in the message pattern. You must invoke the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">applyPattern</span></span> method before you call the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">setFormats</span></span> method. The following table shows how the elements of the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Format</span></span> array correspond to the arguments in the message pattern: </span></span> </p> <dl><dd> <table frame="below" border="1" cellpadding="1" cellspacing="3" width="360"> <col width="215"> <col width="132"> <tbody><tr> <td colspan="2" width="352"> <p>The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Format</span></span> Array of the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormatDemo</span></span> Program</p> </td> </tr> <tr> <td width="215"> <p align="center"><b>Array Element </b> </p> </td> <td width="132"> <p align="center"><b>Pattern Argument </b> </p> </td> </tr> <tr> <td width="215"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">choiceForm</span></span> </p> </td> <td width="132"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{0}</span></span> </p> </td> </tr> <tr> <td width="215"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">null</span></span> </p> </td> <td width="132"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{1}</span></span> </p> </td> </tr> <tr> <td width="215"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">NumberFormat.getInstance()</span></span> </p> </td> <td width="132"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{2}</span></span> </p> </td> </tr> </tbody></table> </dd></dl> <p>7. Set the Arguments and Format the Message</p> <p style="margin: 0.19in 0.5in;"> At run time the program assigns the variables to the array of arguments it passes to the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">MessageFormat</span></span> object. The elements in the array correspond to the arguments in the pattern. For example, <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageArgument[1] </span></span>maps to pattern argument <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{1}</span></span>, which is a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> containing the name of the disk. In the previous step the program assigned a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat </span></span>object to argument <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{0}</span></span> of the pattern. Therefore the number assigned to <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageArgument[0]</span></span> determines which <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">ChoiceFormat </span></span>object selects. If <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageArgument[0]</span></span> is greater than or equal to 2, the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> containing the phrase <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">are {2} files</span></span> replaces argument <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{0}</span></span> in the pattern. The number assigned to <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageArgument[2]</span></span> will be substituted in place of pattern argument <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{2}</span></span>. Here's the code that tries this out: </p> <p style="margin-left: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Object[] messageArguments = {null, "XDisk", null};</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">for (int numFiles = 0; numFiles <></span></span></p> <p style="margin-left: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageArguments[0] = new Integer(numFiles);</span></span></p> <p style="margin-left: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">messageArguments[2] = new Integer(numFiles);</span></span></p> <p style="margin-left: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String result = messageForm.format(messageArguments);</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">System.out.println(result);</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Examples ChoiceFormat:</span></span></p> <dl><dl><dd> <table style="color: rgb(0, 0, 0);" border="1" cellpadding="7" cellspacing="0" width="551"> <col width="64"> <col width="226"> <col width="216"> <tbody><tr valign="top"> <td width="64"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span></p> </td> <td width="226"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Pattern</span></span></p> </td> <td width="216"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">output</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">en_US</span></span></p> </td> <td width="226"> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">noFiles = are no files</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">oneFile = is one file</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">multipleFiles = are {2} files</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">pattern = There {0} on {1}.</span></span></p> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">disk=XDISK</span></span></p> </td> <td width="216"> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">There are no files on XDISK.</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">There is one file on XDISK.</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">There are 2 files on XDISK.</span></span></p> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">There are 3 files on XDISK.</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fr_FR</span></span></p> </td> <td width="226"> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">noFiles = n' y a pas des fichiers</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">oneFile = y a un fichier</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">multipleFiles = y a {2} fichiers</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">pattern = Il {0} sur {1}.</span></span></p> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">disk=frdisk</span></span></p> </td> <td width="216"> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Il n' y a pas des fichiers sur frdisk.</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Il y a un fichier sur frdisk.</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Il y a 2 fichiers sur frdisk.</span></span></p> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Il y a 3 fichiers sur frdisk.</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p style="margin-right: 0.05in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">zh</span></span></p> </td> <td width="226"> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">noFiles =</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">没有文件</span></span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">oneFile = </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">有一个文件</span></span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">multipleFiles = </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">有 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{2} </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">个文件</span></span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">pattern = {0}</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">在</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">{1}.</span></span></p> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">disk=</span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">某磁碟上</span></span></span></p> </td> <td width="216"> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">没有文件在某磁碟上</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">.</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">有一个文件在某磁碟上</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">.</span></span></p> <p style="margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">有 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">2 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">个文件在某磁碟上</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">.</span></span></p> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">有 </span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">3 </span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"><span lang="zh-CN">个文件在某磁碟上</span></span></span><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">.</span></span></p> </td> </tr> </tbody></table> </dd></dl></dl> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 1in; margin-right: 1in; margin-bottom: 0in;"><br /></p> <h2 class="western"><a name="0.15.Character|outline"></a>Character</h2> <p style="margin-bottom: 0in;">To check characters, use Character object method, for example:</p> <ul><li><p style="margin-right: 0.5in; margin-top: 0.19in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">isDigit</span></span> </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">isLetter</span></span> </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">isLetterOrDigit</span></span> </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">isLowerCase</span></span> </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">isUpperCase</span></span> </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">isSpaceChar</span></span> </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0.19in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">isDefined</span></span> </p> </li></ul> <h2 class="western"><a name="0.16.BreakIterator|outline"></a>BreakIterator</h2> <p style="margin-bottom: 0in;">The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">BreakIterator</span></span> class implements methods for finding the location of boundaries in text. Instances of <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">BreakIterator</span></span> maintain a current position and scan over text returning the index of characters where boundaries occur.</p> <p style="margin: 0.19in 0.5in;"> You can analyze four kinds of boundaries with the <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">BreakIterator</span></span> class: character, word, sentence, and potential line break. When instantiating a <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">BreakIterator</span></span>, you invoke the appropriate factory method: </p> <ul><li><p style="margin-right: 0.5in; margin-top: 0.19in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">getCharacterInstance</span></span> </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">getWordInstance</span></span> </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">getSentenceInstance</span></span> </p> </li><li><p style="margin-right: 0.5in; margin-bottom: 0.19in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">getLineInstance</span></span> </p> </li></ul> <p style="margin: 0.19in 0.5in;"> Each instance of <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">BreakIterator</span></span> can detect just one type of boundary.</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale currentLocale = new Locale ("en","US");</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">BreakIterator wordIterator =</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">BreakIterator.getWordInstance(currentLocale);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String someText = "She stopped. " +</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> "She said, \"Hello there,\" and then went on.";</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">markBoundaries(someText, wordIterator);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">static void markBoundaries(String target, BreakIterator iterator) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">StringBuffer markers = new StringBuffer();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">markers.setLength(target.length() + 1);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">for (int k = 0; k <></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> markers.setCharAt(k,' ');</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">iterator.setText(target);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">int boundary = iterator.first();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">while (boundary != BreakIterator.DONE) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> markers.setCharAt(boundary,'^');</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> boundary = iterator.next();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">System.out.println(target);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">System.out.println(markers);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;">}</p> <h2 class="western"><a name="0.17.String|outline"></a>String</h2> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">When you convert between String and byte array, be reminded that you are dealing with locales and provide proper encoding information for conversion. </span></span> </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String original = new String("A" + "\u00ea" + "\u00f1" + "\u00fc" + "C");</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">byte[] utf8Bytes = original.getBytes("UTF8");</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String roundTrip = new String(utf8Bytes, "UTF8");</span></span></p> <h2 class="western"><a name="0.18.Collator|outline"></a>Collator</h2> <p style="margin-top: 0.19in; margin-bottom: 0.19in;">The <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Collator</span></span> class performs locale-sensitive <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String</span></span> comparison. You use this class to build searching and sorting routines for natural language text. </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Collator fr_FRCollator = Collator.getInstance(new Locale("fr","FR"));</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">public static void sortStrings(Collator collator, </span></span> </p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String[] words) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String tmp;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">for (int i = 0; i <></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> for (int j = i + 1; j <></span> </span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> if (collator.compare(words[i], words[j]) > 0) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> tmp = words[i];</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> words[i] = words[j];</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> words[j] = tmp;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> }</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> }</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <dl><dd> <table style="color: rgb(0, 0, 0);" border="1" cellpadding="7" cellspacing="0" width="543"> <col width="64"> <col width="214"> <col width="221"> <tbody><tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Locale</span></span></p> </td> <td width="214"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Original</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Sorted</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">fr_FR</span></span></p> </td> <td width="214"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Péché pêche peach sin</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Peach pêche péché sin</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">en_US</span></span></p> </td> <td width="214"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Peach pêche péché sin</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">Peach péché pêche sin</span></span></p> </td> </tr> <tr valign="top"> <td width="64"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">zh</span></span></p> </td> <td width="214"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">胡益光你好</span></span></p> </td> <td width="221"> <p><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">光好胡你益</span></span></p> </td> </tr> </tbody></table> </dd></dl> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <h2 class="western"><a name="0.19.RuleBasedCollator|outline"></a>RuleBasedCollator</h2> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String smallnTilde = new String("\u00F1"); // ñ</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String capitalNTilde = new String("\u00D1"); // Ñ</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String traditionalSpanishRules =</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">("<></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">"<></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">"<></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">"<></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">"<></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">"<></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">"< " + smallnTilde + "," + capitalNTilde + " " +</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">"<></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">"<></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">"<></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">RuleBasedCollator spCollator =</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">new RuleBasedCollator(traditionalSpanishRules);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">sortStrings(spCollator, words);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">public static void sortStrings(Collator collator, String[] words) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">String tmp;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">for (int i = 0; i <></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> for (int j = i + 1; j <></span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> if (collator.compare(words[i], words[j]) > 0) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> tmp = words[i];</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> words[i] = words[j];</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> words[j] = tmp;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> }</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> }</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <h2 class="western"><a name="0.20.InputStreamReader and OutputStreamWriter|outline"></a> InputStreamReader and OutputStreamWriter</h2> <p style="margin-bottom: 0in;">InputStreamReader and OutputStreamWriter bridges byte streams and character streams. The InputStreamReader allows you to read different bytes from byte stream and convert the bytes into particular encoding character. OutputStreamWriter takes character stream data and output it to a byte stream. The characters written are converted to proper bytes according to the encoding. Failing to use proper encoding may corrupt your data.</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">Non-unicode byte-> InputStreamReader->Unicode character</p> <p style="margin-bottom: 0in;">Unicode character-> OutputStreamWriter->non-unicode byte</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">static String readInput() {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">StringBuffer buffer = new StringBuffer();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">try {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> FileInputStream fis = new FileInputStream("test.txt");</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> InputStreamReader isr = new InputStreamReader(fis,</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> "UTF8");</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> Reader in = new BufferedReader(isr);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> int ch;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> while ((ch = in.read()) > -1) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> buffer.append((char)ch);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> }</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> in.close();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> return buffer.toString();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">} catch (IOException e) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> e.printStackTrace();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> return null;</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">static void writeOutput(String str) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">try {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> FileOutputStream fos = new FileOutputStream("test.txt");</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> Writer out = new OutputStreamWriter(fos, "UTF8");</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> out.write(str);</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> out.close();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">} catch (IOException e) {</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;"> e.printStackTrace();</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> <span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">}</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <h3 class="western"><a name="0.20.1.Socket communication|outline"></a> Socket communication</h3> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:Courier New,monospace;"><span style="font-size:85%;">To pass non-UNICODE data correctly through socket, you need to wrap up the stream of socket with InputStreamReader and OutputStreamWriter and write and read data the same way as above.</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><span style="font-family:MS PGothic,sans-serif;"><span style="font-size:85%;">BufferedWriter out = new BufferedWriter( new OutputStreamWriter(<br /> clientSocket.getOutputStream(), "UTF8"));<br />BufferedReader in = new BufferedReader(new InputStreamReader(<br /> clientSocket.getInputStream(),"UTF8"));</span></span></p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <h2 class="western"><a name="0.21.ComponentOrientation|outline"></a>ComponentOrientation</h2> <p style="text-indent: 0.5in; margin-bottom: 0in;">The ComponentOrientation class encapsulates the language-sensitive orientation that is to be used to order the elements of a component or of text. </p> <p style="margin-bottom: 0in;"> For different locale, you need to setup ComponentOrientation and apply it to each component such as menu, items etc. </p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;">ComponentOrientation Co;</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;">If (Chinese) {</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> Locale.setDefault(Locale.CHINA);</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> Co=ComponentOrientation.LEFT_TO_RIGHT;</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> }</p> <p style="margin-left: 0.5in; margin-bottom: 0in;">If (Hebrew){</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> Locale.setDefault(new Locale(“iw”,”IL”);</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> Co=ComponentOrientation.RIGHT_TO_LEFT;</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> }</p> <p style="margin-left: 0.5in; margin-bottom: 0in;">applyComponentOrientation(Frame frm, Co);</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"><br /></p> <p style="margin-left: 0.5in; margin-bottom: 0in;">applyComponentOrientation(Component c, ComponentOrientation co){</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> c.setComponentOrientation(co);</p> <p style="margin-left: 0.5in; margin-bottom: 0in;"> applyComponentOrientation(EachComponent of c,co);</p> <p style="margin-left: 0.5in; margin-bottom: 0in;">}</p> <h2 class="western"><a name="0.22.Internationalized web applications|outline"></a> Internationalized web applications</h2> <p style="margin-bottom: 0in;">For a internationalized web application, the same server need to response to request from different locales properly. This section describes how the client and server are communicating the locale information.</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">1. The client sends the preferred locale information in the Accept-Language header. </p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">2. ServletRequest provide two methods to parse the Accept-Language header: getLocale() and getLocales().</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">3. ServletResponse has two methods, getLocale() and setLocale(Locale loc) to get and set the locale of the response to client.</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">4. There are two ways to set the character set in servlet response. Browser uses the charset information to render the page with proper font.</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">4.1 One way is to call response.setContentType(“text/html”, charset=ISO-8859-1”); </p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">4.2 The second way is to use response.setLocale(Locale loc). </p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">The second method takes a Locale object and set the appropriate headers in the response to the client. The second method is preferred since you may not know the exact charset name of a given locale. Even if you know the exact charset name, you would still need to maintain that list and manage the mapping between locale and charset name if you use the first method.</p> <p style="margin-bottom: 0in;"><br /></p> <p style="margin-bottom: 0in;">The character set must be set using either setContentType or setLocale before constructing PrintWriter object by calling ServletResponse.getWriter(); the call to getWriter() uses the charset tag to construct PrintWriter object.</p> <h2 class="western"><a name="0.23.JSP Custom Tags|outline"></a>JSP Custom Tags</h2> <p>JSP custom tags are a very powerful mechanism for performing complex processing that removes java code from the JSP pages while encapsulating nontrivial programming logic in a reusable component, which can be packaged in tag library. There are many open source tag libraries on inter-net including an I18N tag library that we can take advantage. To use tag lib, you need to configure the tag lib and then refer to it in JSP pages similar to normal html tags. You can also develop your own tags and package them into tag lib.</p> <h2 class="western"><a name="0.24.FAQ|outline"></a>FAQ</h2> <ol><li><p style="margin-bottom: 0in;">Why I saw square blocks on my screen where I supposed to see MBCS characters?</p> <ol><li><p style="margin-bottom: 0in;">This may be because you are using the wrong font. You need to install the necessary font on your client machine. If you see square blocks on applet, you need to use the right font.properties file for the java VM.</p> </li></ol> </li><li><p style="margin-bottom: 0in;">Why I saw ‘?’ marks on my screen or my output file where I supposed to see MBCS characters?</p> <ol><li><p style="margin-bottom: 0in;">When you convert characters from one encoding EC1 to another EC2 while the EC2 may not have a character or has more than one character that maps to the character in EC1, the result is unknown character-the ‘?’ mark. The following is a small list of possible scenario:</p> <ol type="i"><li><p style="margin-bottom: 0in;">When you read the file, you didn’t use InputStreamReader with proper encoding.</p> </li><li><p style="margin-bottom: 0in;">When you write the file, you didn’t use OutputStreamWriter with proper encoding.</p> </li><li><p style="margin-bottom: 0in;">When you convert a String to byte array, you didn’t use the proper encoding. Or you may not specify encoding at all and the default encoding doesn’t match your String.</p> </li><li><p style="margin-bottom: 0in;">When you convert byte array to String, you didn’t use the proper encoding. Or you may not specify encoding at all and the default encoding doesn’t match your original String encoding.</p> </li><li><p style="margin-bottom: 0in;">When you convert characters from one encoding EC1 to another EC2 while the EC2 may have more than one character that maps to the character in EC1. For example, one simplified Chinese character may map to more than one Traditional Chinese Character.</p> </li></ol> </li></ol> </li></ol> <p style="margin-bottom: 0in;"><br /></p> <h2 class="western"><a name="0.25.Reference|outline"></a>Reference</h2> <ol><li><p style="margin-bottom: 0in;">Java Internationalization by Andrew Deitsch & David Czarnecki from O’Reilly</p> </li><li><p style="margin-bottom: 0in;"><span style="color: rgb(0, 0, 128);"><u><a href="http://java.oreilly.com/news/javainternational_0301.html">http://java.oreilly.com/news/javainternational_0301.html</a></u></span></p> </li><li><p style="margin-bottom: 0in;"><span style="color: rgb(0, 0, 128);"><u><a href="http://java.oreilly.com/news/javaintl_0701.html">http://java.oreilly.com/news/javaintl_0701.html</a></u></span></p> </li><li><p style="margin-bottom: 0in;"><span style="color: rgb(0, 0, 128);"><u><a href="http://java.sun.com/docs/books/tutorial/i18n/TOC.html">http://java.sun.com/docs/books/tutorial/i18n/TOC.html</a></u></span></p> </li><li><p style="margin-bottom: 0in;"><span style="color: rgb(0, 0, 128);"><u><a href="http://www.apache.org/">http://www.apache.org</a></u></span></p> </li></ol> <p style="margin-left: 0.25in; margin-bottom: 0in;"><br /></p>Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com3tag:blogger.com,1999:blog-2317367034642612047.post-9001901540466435452009-06-02T05:24:00.001-07:002009-06-12T12:20:22.728-07:00Joy Of GroovyIn my work and my volunteer, I need to do a lot of scripting for all kinds of things such as system administration, data migration etc. I have used shell script, perl and many other tools on and off for a long time before. Scripting with Groovy brings me much joy and fun.<br /><br />Groovy brings the full Java language and its own flavors to your finger tips while you are typing and thinking your tasks and you can do a lot of things very easily and much simpler than other scripting. Here I will put up a few examples that demonstrate a few tricks that can simplify a lot of daily scripting work:<br /><br />1. File Manipulation<br />2. Configuration<br />3. Database Access<br />4. Logging<br /><br />Suppose you want to create a file and put stuff in it. This is how you do it:<br /><br />def file=new File("test.txt")<br /><br />do this:<br />file.append("line 1\n")<br />file.append("line 2\n")<br /><br />Now the file has 2 lines.<br /><br />Later some where, you want to do something with the data in the file, for example email the data to some one, this is how you get the content for the file:<br />def content=file.txt()<br /><br />Suppose you want to write a method, for example log(String text). The log method basically append what ever text into a log file.<br /><br />binding['writer']=new File("log.txt")<br /><br />def log(String text){<br />writer.append(text+"\n")<br />}<br /><br />Now suppose, instead of putting the log into a log file, I want to put the log into a database table for easy to search or may be for data warehousing purpose, you can easily change the above log method without touching the rest of your code.<br /><br />def log(String text){<br />sql.execute("insert into usertrace(id,description) value(?,?)",[nextid(),text])<br />}<br /><br />where nextid() is simply a method that return you the next id of table usertrace.<br /><br />Now what is sql here? Before I talk sql, I need to touch the groovy configuration a bit.<br /><br />The ConfigSlurper is a cool thing.<br /><br />You might have been used to manipulate the java Property class which you need to load the property file and then use getter method to fetch the string property you want. The groovy way is much simpler.<br /><br />In this code, MyConfig.groovy is a groovy class which contains name/value pairs, one of them is<br />called sql like this:<br /><br />sql=groovy.sql.Sql.newInstance("jdbc:postgresql://localhost:5432/helloworld","username","password","org.postgresql.Driver")<br /><br />This is how you get the property sql.<br /><br />binding['cfg']=new ConfigSlurper().parse(new File('MyConfig.groovy').toURL())<br />binding['sql']=cfg.sql<br /><br />Notice that the new variable sql is an object, not just a String as in Java Property file.<br />Then you can execute the method of the object sql, like we did in the above log method.<br /><br />Have fun so far! As you explore Groovy, you will discover them more.Yiguang Huhttp://www.blogger.com/profile/10518057727848569179noreply@blogger.com1