2009-06-25 07:16:22 -07:00
|
|
|
|
/*
|
|
|
|
|
* MessageBody.cs
|
|
|
|
|
* Copyright (c) 2008-2009 kbinani
|
|
|
|
|
*
|
|
|
|
|
* This file is part of Boare.Lib.AppUtil.
|
|
|
|
|
*
|
|
|
|
|
* Boare.Lib.AppUtil is free software; you can redistribute it and/or
|
|
|
|
|
* modify it under the terms of the BSD License.
|
|
|
|
|
*
|
|
|
|
|
* Boare.Lib.AppUtil is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
|
*/
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if JAVA
|
|
|
|
|
package org.kbinani.apputil;
|
|
|
|
|
|
|
|
|
|
import java.util.*;
|
|
|
|
|
import java.io.*;
|
|
|
|
|
import java.awt.image.*;
|
|
|
|
|
import org.kbinani.*;
|
|
|
|
|
#else
|
2009-06-25 07:16:22 -07:00
|
|
|
|
using System;
|
2010-03-16 20:14:08 -07:00
|
|
|
|
using bocoree;
|
|
|
|
|
using bocoree.java.util;
|
|
|
|
|
using bocoree.java.io;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
|
|
|
|
|
namespace Boare.Lib.AppUtil {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
using boolean = System.Boolean;
|
|
|
|
|
#endif
|
2009-06-25 07:16:22 -07:00
|
|
|
|
|
|
|
|
|
public class MessageBody {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public String lang;
|
|
|
|
|
public String poHeader = "";
|
|
|
|
|
public TreeMap<String, MessageBodyEntry> list = new TreeMap<String, MessageBodyEntry>();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public MessageBody( String lang_ ) {
|
2009-06-25 07:16:22 -07:00
|
|
|
|
lang = lang_;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public MessageBody( String lang, String[] ids, String[] messages ) {
|
2009-06-25 07:16:22 -07:00
|
|
|
|
this.lang = lang;
|
2010-03-16 20:14:08 -07:00
|
|
|
|
list = new TreeMap<String, MessageBodyEntry>();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
for( int i = 0; i < ids.Length; i++ ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
list.put( ids[i], new MessageBodyEntry( messages[i], new String[] { } ) );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public MessageBody( String lang_, String file ) {
|
2009-06-25 07:16:22 -07:00
|
|
|
|
lang = lang_;
|
2010-03-16 20:14:08 -07:00
|
|
|
|
poHeader = "";
|
|
|
|
|
BufferedReader sr = null;
|
|
|
|
|
try {
|
|
|
|
|
sr = new BufferedReader( new FileReader( file ) );
|
|
|
|
|
String line2 = "";
|
|
|
|
|
while ( (line2 = sr.readLine()) != null ) {
|
|
|
|
|
ByRef<String> msgid = new ByRef<String>( "" );
|
|
|
|
|
String first_line = line2;
|
|
|
|
|
ByRef<String[]> location = new ByRef<String[]>();
|
|
|
|
|
String last_line = readTillMessageEnd( sr, first_line, "msgid", msgid, location );
|
|
|
|
|
ByRef<String> msgstr = new ByRef<String>( "" );
|
|
|
|
|
ByRef<String[]> location_dumy = new ByRef<String[]>();
|
|
|
|
|
last_line = readTillMessageEnd( sr, last_line, "msgstr", msgstr, location_dumy );
|
|
|
|
|
if ( PortUtil.getStringLength( msgid.value ) > 0 ) {
|
|
|
|
|
list.put( msgid.value, new MessageBodyEntry( msgstr.value, location.value ) );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
} else {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
poHeader = msgstr.value;
|
|
|
|
|
String[] spl = PortUtil.splitString( poHeader, new char[] { (char)0x0d, (char)0x0a }, true );
|
|
|
|
|
poHeader = "";
|
2009-07-29 10:03:20 -07:00
|
|
|
|
int count = 0;
|
2010-03-16 20:14:08 -07:00
|
|
|
|
for ( int i = 0; i < spl.Length; i++ ) {
|
|
|
|
|
String line = spl[i];
|
|
|
|
|
String[] spl2 = PortUtil.splitString( line, new char[] { ':' }, 2 );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
if ( spl2.Length == 2 ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
String name = spl2[0].Trim();
|
|
|
|
|
String ct = "Content-Type";
|
|
|
|
|
String cte = "Content-Transfer-Encoding";
|
|
|
|
|
if ( name.ToLower().Equals( ct.ToLower() ) ) {
|
|
|
|
|
poHeader += (count == 0 ? "" : "\n") + "Content-Type: text/plain; charset=UTF-8";
|
|
|
|
|
} else if ( name.ToLower().Equals( cte.ToLower() ) ) {
|
|
|
|
|
poHeader += (count == 0 ? "" : "\n") + "Content-Transfer-Encoding: 8bit";
|
2009-07-29 10:03:20 -07:00
|
|
|
|
} else {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
poHeader += (count == 0 ? "" : "\n") + line;
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
|
|
|
|
} else {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
poHeader += (count == 0 ? "" : "\n") + line;
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
|
|
|
|
count++;
|
|
|
|
|
}
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
} catch ( Exception ex ) {
|
|
|
|
|
} finally {
|
|
|
|
|
if ( sr != null ) {
|
|
|
|
|
try {
|
|
|
|
|
sr.close();
|
|
|
|
|
} catch ( Exception ex2 ) {
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public String getMessage( String id ) {
|
|
|
|
|
if ( list.containsKey( id ) ) {
|
|
|
|
|
String ret = list.get( id ).message;
|
|
|
|
|
if ( ret.Equals( "" ) ) {
|
2009-06-25 07:16:22 -07:00
|
|
|
|
return id;
|
|
|
|
|
} else {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
return list.get( id ).message;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return id;
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public MessageBodyEntry getMessageDetail( String id ) {
|
|
|
|
|
if ( list.containsKey( id ) ) {
|
|
|
|
|
String ret = list.get( id ).message;
|
|
|
|
|
if ( ret.Equals( "" ) ) {
|
|
|
|
|
return new MessageBodyEntry( id, new String[] { } );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
} else {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
return list.get( id );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
return new MessageBodyEntry( id, new String[] { } );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
public void write( String file ) {
|
|
|
|
|
BufferedWriter sw = null;
|
|
|
|
|
try {
|
|
|
|
|
sw = new BufferedWriter( new FileWriter( file ) );
|
|
|
|
|
if ( !poHeader.Equals( "" ) ) {
|
|
|
|
|
sw.write( "msgid \"\"" );
|
|
|
|
|
sw.newLine();
|
|
|
|
|
sw.write( "msgstr \"\"" );
|
|
|
|
|
sw.newLine();
|
|
|
|
|
String[] spl = PortUtil.splitString( poHeader, new char[] { (char)0x0d, (char)0x0a }, true );
|
|
|
|
|
for ( int i = 0; i < spl.Length; i++ ){
|
|
|
|
|
String line = spl[i];
|
|
|
|
|
sw.write( "\"" + line + "\\" + "n\"" );
|
|
|
|
|
sw.newLine();
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
sw.newLine();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
} else {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
sw.write( "msgid \"\"" );
|
|
|
|
|
sw.newLine();
|
|
|
|
|
sw.write( "msgstr \"\"" );
|
|
|
|
|
sw.newLine();
|
|
|
|
|
sw.write( "\"Content-Type: text/plain; charset=UTF-8\\" + "n\"" );
|
|
|
|
|
sw.newLine();
|
|
|
|
|
sw.write( "\"Content-Transfer-Encoding: 8bit\\" + "n\"" );
|
|
|
|
|
sw.newLine();
|
|
|
|
|
sw.newLine();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
for ( Iterator itr = list.keySet().iterator(); itr.hasNext(); ){
|
|
|
|
|
String key = (String)itr.next();
|
|
|
|
|
String skey = key.Replace( "\n", "\\n\"\n\"" );
|
|
|
|
|
MessageBodyEntry mbe = list.get( key );
|
|
|
|
|
String s = mbe.message;
|
|
|
|
|
Vector<String> location = mbe.location;
|
|
|
|
|
int count = location.size();
|
|
|
|
|
for ( int i = 0; i < count; i++ ) {
|
|
|
|
|
sw.write( "#: " + location.get( i ) );
|
|
|
|
|
sw.newLine();
|
2009-07-29 10:03:20 -07:00
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
sw.write( "msgid \"" + skey + "\"" );
|
|
|
|
|
sw.newLine();
|
2009-06-25 07:16:22 -07:00
|
|
|
|
s = s.Replace( "\n", "\\n\"\n\"" );
|
2010-03-16 20:14:08 -07:00
|
|
|
|
sw.write( "msgstr \"" + s + "\"" );
|
|
|
|
|
sw.newLine();
|
|
|
|
|
sw.newLine();
|
|
|
|
|
}
|
|
|
|
|
} catch ( Exception ex ) {
|
|
|
|
|
} finally {
|
|
|
|
|
if ( sw != null ) {
|
|
|
|
|
try {
|
|
|
|
|
sw.close();
|
|
|
|
|
} catch ( Exception ex2 ) {
|
|
|
|
|
}
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
private static void separateEntryAndMessage( String source, ByRef<String> entry, ByRef<String> message ) {
|
|
|
|
|
String line = source.Trim();
|
|
|
|
|
entry.value = "";
|
|
|
|
|
message.value = "";
|
|
|
|
|
if ( PortUtil.getStringLength( line ) <= 0 ) {
|
2009-06-25 07:16:22 -07:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
int index_space = line.IndexOf( ' ' );
|
|
|
|
|
int index_dquoter = line.IndexOf( '"' );
|
|
|
|
|
int index = Math.Min( index_dquoter, index_space );
|
2010-03-16 20:14:08 -07:00
|
|
|
|
entry.value = line.Substring( 0, index );
|
|
|
|
|
message.value = line.Substring( index_dquoter + 1 );
|
|
|
|
|
message.value = message.value.Substring( 0, PortUtil.getStringLength( message.value ) - 1 );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
private static String readTillMessageEnd( BufferedReader sr, String first_line, String entry, ByRef<String> msg, ByRef<String[]> locations )
|
|
|
|
|
#if JAVA
|
|
|
|
|
throws IOException
|
|
|
|
|
#endif
|
|
|
|
|
{
|
|
|
|
|
msg.value = "";
|
|
|
|
|
String line = first_line;
|
|
|
|
|
Vector<String> location = new Vector<String>();
|
|
|
|
|
boolean entry_found = false;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
if ( line.StartsWith( entry ) ) {
|
2009-07-29 10:03:20 -07:00
|
|
|
|
// 1行目がすでに"entry"の行だった場合
|
2010-03-16 20:14:08 -07:00
|
|
|
|
ByRef<String> dum = new ByRef<String>( "" );
|
|
|
|
|
ByRef<String> dum2 = new ByRef<String>( "" );
|
|
|
|
|
separateEntryAndMessage( line, dum, dum2 );
|
|
|
|
|
msg.value += dum2.value;
|
2009-06-25 07:16:22 -07:00
|
|
|
|
} else {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
while ( (line = sr.readLine()) != null ) {
|
2009-07-29 10:03:20 -07:00
|
|
|
|
if ( line.StartsWith( "#:" ) ) {
|
|
|
|
|
line = line.Substring( 2 ).Trim();
|
2010-03-16 20:14:08 -07:00
|
|
|
|
location.add( line );
|
2009-07-29 10:03:20 -07:00
|
|
|
|
} else if ( line.StartsWith( entry ) ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
ByRef<String> dum = new ByRef<String>( "" );
|
|
|
|
|
ByRef<String> dum2 = new ByRef<String>( "" );
|
|
|
|
|
separateEntryAndMessage( line, dum, dum2 );
|
|
|
|
|
msg.value += dum2.value;
|
2009-07-29 10:03:20 -07:00
|
|
|
|
break;
|
|
|
|
|
}
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
locations.value = location.toArray( new String[] { } );
|
|
|
|
|
String ret = "";
|
|
|
|
|
while ( (line = sr.readLine()) != null ) {
|
2009-06-25 07:16:22 -07:00
|
|
|
|
if ( !line.StartsWith( "\"" ) ) {
|
2010-03-16 20:14:08 -07:00
|
|
|
|
msg.value = msg.value.Replace( "\\\"", "\"" );
|
|
|
|
|
msg.value = msg.value.Replace( "\\n", "\n" );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
int index = line.LastIndexOf( "\"" );
|
2010-03-16 20:14:08 -07:00
|
|
|
|
msg.value += line.Substring( 1, index - 1 );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
msg.value = msg.value.Replace( "\\\"", "\"" );
|
|
|
|
|
msg.value = msg.value.Replace( "\\n", "\n" );
|
2009-06-25 07:16:22 -07:00
|
|
|
|
return line;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#if !JAVA
|
2009-06-25 07:16:22 -07:00
|
|
|
|
}
|
2010-03-16 20:14:08 -07:00
|
|
|
|
#endif
|