change transliterate library and implement log swing

git-svn-id: https://osmand.googlecode.com/svn/trunk@116 e29c36b1-1cfa-d876-8d93-3434fc2bb7b8
This commit is contained in:
Victor Shcherb 2010-06-02 13:27:50 +00:00
parent 409f31bfcc
commit dab9f436cf
13 changed files with 148 additions and 53 deletions

View file

@ -5,6 +5,6 @@
<classpathentry kind="lib" path="lib/bzip2-20090327.jar"/> <classpathentry kind="lib" path="lib/bzip2-20090327.jar"/>
<classpathentry kind="lib" path="lib/sqlitejdbc-v056.jar"/> <classpathentry kind="lib" path="lib/sqlitejdbc-v056.jar"/>
<classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/> <classpathentry kind="lib" path="lib/commons-logging-1.1.1.jar"/>
<classpathentry kind="lib" path="lib/icu4j-4_4_1.jar" sourcepath="C:/Download/icu4j-4_4_1-src.jar"/> <classpathentry kind="lib" path="lib/junidecode-0.1.jar"/>
<classpathentry kind="output" path="bin"/> <classpathentry kind="output" path="bin"/>
</classpath> </classpath>

Binary file not shown.

View file

@ -1,8 +1,13 @@
.level = SEVERE .level = SEVERE
handlers=java.util.logging.ConsoleHandler handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler
# Set the default logging level for the logger named com.mycompany # Set the default logging level for the logger named com.mycompany
com.osmand.level = ALL com.osmand.level = ALL
java.util.logging.FileHandler.pattern=%h/Application Data/Osmand/osmand.log
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.append = false
java.util.logging.FileHandler.limit = 100000
java.util.logging.ConsoleHandler.level=ALL java.util.logging.ConsoleHandler.level=ALL
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

View file

@ -1,18 +1,43 @@
package com.osmand; package com.osmand;
import javax.swing.JOptionPane;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import com.osmand.swing.OsmExtractionUI;
public class ExceptionHandler { public class ExceptionHandler {
private static final Log log = LogUtil.getLog(ExceptionHandler.class); private static final Log log = LogUtil.getLog(ExceptionHandler.class);
public static void handle(Exception e){ public static void handle(Throwable e){
e.printStackTrace(); handle("Error occurred", e);
log.error("Error occurred", e);
} }
public static void handle(String msg, Exception e){ public static void handle(String msg, Throwable e){
e.printStackTrace(); if(e != null){
log.error(msg, e); log.error(msg, e);
} else {
log.error(msg);
}
if(e != null){
e.printStackTrace();
}
if (OsmExtractionUI.MAIN_APP != null && OsmExtractionUI.MAIN_APP.getFrame() != null) {
String text;
String title;
if (e != null) {
text = e.getMessage();
title = msg;
} else {
title = "Error occured";
text = msg;
}
JOptionPane.showMessageDialog(OsmExtractionUI.MAIN_APP.getFrame(), text, title, JOptionPane.ERROR_MESSAGE);
}
}
public static void handle(String msg){
handle(msg, null);
} }
} }

View file

@ -30,10 +30,9 @@ public class ToDoConstants {
// FIXME Bugs Android : // FIXME Bugs Android :
// Investigate good transliterate
// 6. Understand concept of application where to save/restore global setting. // 6. Understand concept of application where to save/restore global setting.
// (for example reset navigate to point, reset link map with location). It should be reset after user call exit. // (for example reset navigate to point, reset link map with location). It should be reset after user call exit.
// Call ResourceManager.close when it is needed. // Call ResourceManager.close when it is needed (+).
// 10. Notification is gone after clear all notifications // 10. Notification is gone after clear all notifications
// Performance improvements Android : // Performance improvements Android :
@ -41,8 +40,6 @@ public class ToDoConstants {
// TODO SWING : in release 0.1 // TODO SWING : in release 0.1
// 5. Implement suppress warning for duplicate id (+)
// 2. Configure file log & see log from file (add uncaught exception handling) (+)
// 6. Implement renaming/deleting street/building/city (+) // 6. Implement renaming/deleting street/building/city (+)
// 7. Implement saving bundle of tiles in different folder (+) // 7. Implement saving bundle of tiles in different folder (+)
@ -70,4 +67,5 @@ public class ToDoConstants {
// DONE SWING // DONE SWING
// 8. Implement basic transliteration version // 8. Implement basic transliteration version
// 2. Configure file log & see log from file (add uncaught exception handling)
} }

View file

@ -14,12 +14,13 @@ import java.util.ArrayList;
import java.util.LinkedHashMap; import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import net.sf.junidecode.Junidecode;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.tools.bzip2.CBZip2InputStream; import org.apache.tools.bzip2.CBZip2InputStream;
import org.xml.sax.SAXException; import org.xml.sax.SAXException;
import com.ibm.icu.text.Transliterator;
import com.osmand.Algoritms; import com.osmand.Algoritms;
import com.osmand.IProgress; import com.osmand.IProgress;
import com.osmand.data.Amenity; import com.osmand.data.Amenity;
@ -249,6 +250,7 @@ public class DataExtraction {
progress.startTask("Loading file " + path, -1); progress.startTask("Loading file " + path, -1);
} }
OsmBaseStorage storage = new OsmBaseStorage(); OsmBaseStorage storage = new OsmBaseStorage();
storage.setSupressWarnings(DataExtractionSettings.getSettings().isSupressWarningsForDuplicatedId());
if (addFilter != null) { if (addFilter != null) {
storage.getFilters().add(addFilter); storage.getFilters().add(addFilter);
} }
@ -306,31 +308,32 @@ public class DataExtraction {
// 7. Call data preparation to sort cities, calculate center location, assign id to objects // 7. Call data preparation to sort cities, calculate center location, assign id to objects
country.doDataPreparation(); country.doDataPreparation();
// 8. Transliterate names to english // 8. Transliterate names to english
Transliterator latin = Transliterator.getInstance("Any-Latin;NFD;[:Nonspacing Mark:] Remove;NFC");
if(latin != null){ convertEnglishName(country);
convertEnglishName(country, latin); for (CityType c : CityType.values()) {
for(CityType c : CityType.values() ){ for (City city : country.getCitiesByType(c)) {
for(City city : country.getCitiesByType(c)){ convertEnglishName(city);
convertEnglishName(city, latin); for (Street s : city.getStreets()) {
for(Street s : city.getStreets()){ convertEnglishName(s);
convertEnglishName(s, latin); for (Building b : s.getBuildings()) {
for(Building b : s.getBuildings()){ convertEnglishName(b);
convertEnglishName(b, latin); }
} }
} }
} }
} for (Amenity a : country.getAmenityManager().getAllObjects()) {
for(Amenity a : country.getAmenityManager().getAllObjects()){ convertEnglishName(a);
convertEnglishName(a, latin); }
}
}
return country; return country;
} }
// icu4j example - icu is not good in transliteration russian names
// Transliterator latin = Transliterator.getInstance("Any-Latin;NFD;[:Nonspacing Mark:] Remove;NFC");
private void convertEnglishName(MapObject o, Transliterator transliterator){ private void convertEnglishName(MapObject o){
String name = o.getName(); String name = o.getName();
if(name != null && o.getEnName() == null){ if(name != null && o.getEnName() == null){
o.setEnName(transliterator.transliterate(name)); o.setEnName(Junidecode.unidecode(name));
// o.setEnName(transliterator.transliterate(name));
} }
} }

View file

@ -56,6 +56,7 @@ public class OsmBaseStorage extends DefaultHandler {
protected InputStream inputStream; protected InputStream inputStream;
protected InputStream streamForProgress; protected InputStream streamForProgress;
protected List<IOsmStorageFilter> filters = new ArrayList<IOsmStorageFilter>(); protected List<IOsmStorageFilter> filters = new ArrayList<IOsmStorageFilter>();
protected boolean supressWarnings = true;
public synchronized void parseOSM(InputStream stream, IProgress progress, InputStream streamForProgress) throws IOException, SAXException { public synchronized void parseOSM(InputStream stream, IProgress progress, InputStream streamForProgress) throws IOException, SAXException {
@ -89,6 +90,13 @@ public class OsmBaseStorage extends DefaultHandler {
} }
public boolean isSupressWarnings() {
return supressWarnings;
}
public void setSupressWarnings(boolean supressWarnings) {
this.supressWarnings = supressWarnings;
}
protected SAXParser saxParser; protected SAXParser saxParser;
public SAXParser initSaxParser(){ public SAXParser initSaxParser(){
if(saxParser != null){ if(saxParser != null){
@ -192,8 +200,8 @@ public class OsmBaseStorage extends DefaultHandler {
if(currentParsedEntity != null){ if(currentParsedEntity != null){
if(acceptEntityToLoad(currentParsedEntity)){ if(acceptEntityToLoad(currentParsedEntity)){
Entity oldEntity = entities.put(currentParsedEntity.getId(), currentParsedEntity); Entity oldEntity = entities.put(currentParsedEntity.getId(), currentParsedEntity);
if(oldEntity!= null){ if(!supressWarnings && oldEntity!= null){
// throw new UnsupportedOperationException("Entity with id=" + oldEntity.getId() +" is duplicated in osm map"); throw new UnsupportedOperationException("Entity with id=" + oldEntity.getId() +" is duplicated in osm map");
} }
} else { } else {
// System.gc(); // System.gc();

View file

@ -21,7 +21,6 @@ import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Enumeration;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Stack; import java.util.Stack;
@ -30,7 +29,6 @@ import java.util.Map.Entry;
import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter; import javax.xml.stream.XMLStreamWriter;
import com.ibm.icu.text.Transliterator;
import com.osmand.Algoritms; import com.osmand.Algoritms;
import com.osmand.data.MapObject; import com.osmand.data.MapObject;
import com.osmand.osm.Entity; import com.osmand.osm.Entity;
@ -172,13 +170,17 @@ public class OsmStorageWriter {
} }
} }
public static void main(String[] args) { /*public static void main(String[] args) {
Transliterator inst = Transliterator.getInstance("Any-Latin;NFD;[:Nonspacing Mark:] Remove;NFC"); // Transliterator inst = Transliterator.getInstance("Any-Latin;NFD;[:Nonspacing Mark:] Remove;NFKC");
Transliterator inst = Transliterator.getInstance("Any-Latin;");
Enumeration<String> e = Transliterator.getAvailableIDs(); Enumeration<String> e = Transliterator.getAvailableIDs();
while(e.hasMoreElements()){ while(e.hasMoreElements()){
System.out.println(e.nextElement()); System.out.println(e.nextElement());
} }
System.out.println(inst.transliterate("Привет всемь жаваыфайу ы х й к ще цуккак у ваа")); String str = "Привет Гомель жаль прощаться до скорой встречи когда ы ";
} System.out.println(Junidecode.unidecode(str));
System.out.println(inst.transliterate(str));
}*/
} }

View file

@ -84,6 +84,14 @@ public class DataExtractionSettings {
preferences.putBoolean("use_internet", b); preferences.putBoolean("use_internet", b);
} }
public boolean isSupressWarningsForDuplicatedId(){
return preferences.getBoolean("supress_duplicated_id", true);
}
public void setSupressWarningsForDuplicatedId(boolean b){
preferences.putBoolean("supress_duplicated_id", b);
}
String[] SUFFIXES = new String[] {"av.", "avenue", "просп.", "пер.", "пр.","заул.", "проспект", "переул.", "бул.", "бульвар", "тракт"}; String[] SUFFIXES = new String[] {"av.", "avenue", "просп.", "пер.", "пр.","заул.", "проспект", "переул.", "бул.", "бульвар", "тракт"};
String[] DEFAUTL_SUFFIXES = new String[] {"str.", "street", "улица", "ул."}; String[] DEFAUTL_SUFFIXES = new String[] {"str.", "street", "улица", "ул."};

View file

@ -32,6 +32,7 @@ public class OsmExtractionPreferencesDialog extends JDialog {
private JTextField streetDefaultSuffixes; private JTextField streetDefaultSuffixes;
private JCheckBox useInternet; private JCheckBox useInternet;
private JCheckBox supressWarning;
public OsmExtractionPreferencesDialog(Component parent){ public OsmExtractionPreferencesDialog(Component parent){
super(JOptionPane.getFrameForComponent(parent), true); super(JOptionPane.getFrameForComponent(parent), true);
@ -41,7 +42,7 @@ public class OsmExtractionPreferencesDialog extends JDialog {
} }
public void showDialog(){ public void showDialog(){
setSize(600, 220); setSize(600, 250);
double x = getParent().getBounds().getCenterX(); double x = getParent().getBounds().getCenterX();
double y = getParent().getBounds().getCenterY(); double y = getParent().getBounds().getCenterY();
setLocation((int) x - getWidth() / 2, (int) y - getHeight() / 2); setLocation((int) x - getWidth() / 2, (int) y - getHeight() / 2);
@ -74,7 +75,7 @@ public class OsmExtractionPreferencesDialog extends JDialog {
private void createGeneralSection(JPanel root) { private void createGeneralSection(JPanel root) {
JPanel panel = new JPanel(); JPanel panel = new JPanel();
panel.setLayout(new GridLayout(1, 1, 5, 5)); panel.setLayout(new GridLayout(2, 1, 5, 5));
panel.setBorder(BorderFactory.createTitledBorder("General")); panel.setBorder(BorderFactory.createTitledBorder("General"));
root.add(panel); root.add(panel);
@ -82,6 +83,11 @@ public class OsmExtractionPreferencesDialog extends JDialog {
useInternet.setText("Use internet to download tiles"); useInternet.setText("Use internet to download tiles");
useInternet.setSelected(DataExtractionSettings.getSettings().useInternetToLoadImages()); useInternet.setSelected(DataExtractionSettings.getSettings().useInternetToLoadImages());
panel.add(useInternet); panel.add(useInternet);
supressWarning = new JCheckBox();
supressWarning.setText("Supress warnings for duplicated id in osm file");
supressWarning.setSelected(DataExtractionSettings.getSettings().isSupressWarningsForDuplicatedId());
panel.add(supressWarning);
panel.setMaximumSize(new Dimension(Short.MAX_VALUE, panel.getPreferredSize().height)); panel.setMaximumSize(new Dimension(Short.MAX_VALUE, panel.getPreferredSize().height));
} }
@ -164,6 +170,9 @@ public class OsmExtractionPreferencesDialog extends JDialog {
if(settings.useInternetToLoadImages() != useInternet.isSelected()){ if(settings.useInternetToLoadImages() != useInternet.isSelected()){
settings.setUseInterentToLoadImages(useInternet.isSelected()); settings.setUseInterentToLoadImages(useInternet.isSelected());
} }
if(settings.isSupressWarningsForDuplicatedId() != supressWarning.isSelected()){
settings.setSupressWarningsForDuplicatedId (supressWarning.isSelected());
}
} }

View file

@ -12,6 +12,7 @@ import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.lang.Thread.UncaughtExceptionHandler;
import java.lang.reflect.InvocationTargetException; import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException; import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
@ -87,12 +88,25 @@ import com.osmand.swing.MapPanel.MapSelectionArea;
public class OsmExtractionUI implements IMapLocationListener { public class OsmExtractionUI implements IMapLocationListener {
private static final Log log = LogFactory.getLog(OsmExtractionUI.class); private static final Log log = LogFactory.getLog(OsmExtractionUI.class);
public static final String LOG_PATH = System.getProperty("user.home")+"/Application Data/Osmand/osmand.log";
public static OsmExtractionUI MAIN_APP;
public static void main(String[] args) { public static void main(String[] args) {
OsmExtractionUI ui = new OsmExtractionUI(null); // first of all config log
ui.frame.setBounds(DataExtractionSettings.getSettings().getWindowBounds()); new File(LOG_PATH).getParentFile().mkdirs();
ui.frame.setVisible(true); final UncaughtExceptionHandler defaultHandler = Thread.getDefaultUncaughtExceptionHandler();
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler(){
@Override
public void uncaughtException(Thread t, Throwable e) {
log.error("Error in thread " + t.getName(), e);
defaultHandler.uncaughtException(t, e);
}
});
MAIN_APP = new OsmExtractionUI(null);
MAIN_APP.frame.setBounds(DataExtractionSettings.getSettings().getWindowBounds());
MAIN_APP.frame.setVisible(true);
} }
protected City selectedCity; protected City selectedCity;
@ -113,8 +127,7 @@ public class OsmExtractionUI implements IMapLocationListener {
private JCheckBox normalizingStreets; private JCheckBox normalizingStreets;
private TreeModelListener treeModelListener; private TreeModelListener treeModelListener;
private JCheckBox loadingAllData; private JCheckBox loadingAllData;
public OsmExtractionUI(final Region r){ public OsmExtractionUI(final Region r){
@ -478,8 +491,28 @@ public class OsmExtractionUI implements IMapLocationListener {
bar.add(menu); bar.add(menu);
JMenuItem settings = new JMenuItem("Settings..."); JMenuItem settings = new JMenuItem("Settings...");
menu.add(settings); menu.add(settings);
menu.addSeparator();
JMenuItem openLogFile = new JMenuItem("Open log file...");
menu.add(openLogFile);
openLogFile.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
File file = new File(OsmExtractionUI.LOG_PATH);
if (file != null && file.exists()) {
try {
Runtime.getRuntime().exec(new String[] { "notepad.exe", file.getAbsolutePath() }); //$NON-NLS-1$
} catch (IOException es) {
ExceptionHandler.handle("Failed to open log file ", es);
}
} else {
ExceptionHandler.handle("Log file is not found");
}
}
});
exitMenu.addActionListener(new ActionListener(){ exitMenu.addActionListener(new ActionListener(){
@Override @Override
@ -616,6 +649,10 @@ public class OsmExtractionUI implements IMapLocationListener {
return fc; return fc;
} }
public JFrame getFrame() {
return frame;
}
public void loadCountry(final File f, final IOsmStorageFilter filter){ public void loadCountry(final File f, final IOsmStorageFilter filter){
try { try {
final ProgressDialog dlg = new ProgressDialog(frame, "Loading osm file"); final ProgressDialog dlg = new ProgressDialog(frame, "Loading osm file");
@ -655,7 +692,7 @@ public class OsmExtractionUI implements IMapLocationListener {
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
log.error("Interrupted", e1); log.error("Interrupted", e1);
} catch (InvocationTargetException e1) { } catch (InvocationTargetException e1) {
log.error("Exception during operation", e1.getCause()); ExceptionHandler.handle("Exception during operation", e1.getCause());
} }
} }
@ -689,7 +726,7 @@ public class OsmExtractionUI implements IMapLocationListener {
} catch (InterruptedException e1) { } catch (InterruptedException e1) {
log.error("Interrupted", e1); log.error("Interrupted", e1);
} catch (InvocationTargetException e1) { } catch (InvocationTargetException e1) {
log.error("Exception during operation", e1.getCause()); ExceptionHandler.handle("Log file is not found", e1.getCause());
} }
} }

View file

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<classpath> <classpath>
<classpathentry kind="src" path="src"/> <classpathentry kind="src" path="src"/>
<classpathentry excluding="**/MapPanel*|com/osmand/LogUtil.java|com/osmand/osm/io/OSMStorageWriter.java|com/osmand/DataExtraction.java|com/osmand/swing/|com/osmand/data/preparation/DataExtraction.java|com/osmand/data/preparation/DataIndexBuilder.java|com/osmand/osm/io/OsmStorageWriter.java|test/" kind="src" path="use"/> <classpathentry excluding="**/MapPanel*|com/osmand/LogUtil.java|com/osmand/osm/io/OSMStorageWriter.java|com/osmand/DataExtraction.java|com/osmand/swing/|com/osmand/data/preparation/DataExtraction.java|com/osmand/data/preparation/DataIndexBuilder.java|com/osmand/osm/io/OsmStorageWriter.java|test/|com/osmand/ExceptionHandler.java" kind="src" path="use"/>
<classpathentry kind="src" path="gen"/> <classpathentry kind="src" path="gen"/>
<classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/> <classpathentry kind="con" path="com.android.ide.eclipse.adt.ANDROID_FRAMEWORK"/>
<classpathentry kind="lib" path="lib/bzip2-20090327.jar"/> <classpathentry kind="lib" path="lib/bzip2-20090327.jar"/>