[Schmitzm-commits] r885 - in trunk/src/schmitzm: lang swing

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Thu Jun 3 12:15:28 CEST 2010


Author: mojays
Date: 2010-06-03 12:15:27 +0200 (Thu, 03 Jun 2010)
New Revision: 885

Modified:
   trunk/src/schmitzm/lang/ResourceProvider.java
   trunk/src/schmitzm/swing/ResourceProviderManagerFrame.java
Log:
Optional root path for the new language files in ResourceProvider.createNewLanguage(.) and ResourceProviderManagerFrame.

Modified: trunk/src/schmitzm/lang/ResourceProvider.java
===================================================================
--- trunk/src/schmitzm/lang/ResourceProvider.java	2010-06-03 09:19:08 UTC (rev 884)
+++ trunk/src/schmitzm/lang/ResourceProvider.java	2010-06-03 10:15:27 UTC (rev 885)
@@ -33,6 +33,7 @@
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
 import java.io.PrintWriter;
+import java.net.MalformedURLException;
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.util.HashMap;
@@ -639,27 +640,45 @@
 	 *            Resource-Provider fuer den das neue Bundle angelegt wird
 	 * @param l
 	 *            Sprache des neuen Bundles
+	 * @param rootPath
+	 *            root path for the bundle package (if {@code null} the exact
+	 *            path is determined according to the classpath by using
+	 *            {@code ClassLoader.getSystemResource(.)}); if you want to
+	 *            determine the exact location of an existing file the {@code rootPath}
+	 *            <b>should be {@code null}</b>!!         
 	 * @exception URISyntaxException
 	 *                wenn bei der Ermittlung des Datei-Pfads ein Fehler
 	 *                auftritt
+	 * @exception MalformedURLException 
+     *                wenn bei der Ermittlung des Datei-Pfads ein Fehler
+     *                auftritt
 	 */
-	public static File getPropertyFile(ResourceProvider provider, Locale l)
-			throws URISyntaxException {
+	public static File getPropertyFile(ResourceProvider provider, Locale l, File rootPath)
+			throws URISyntaxException, MalformedURLException {
 		String bundlePath = provider.getBundleName();
 		String bundlePackage= "";
 		String bundleName = bundlePath;
+
 		int lastDotIdx = bundlePath.lastIndexOf(".");
-		
 		if (lastDotIdx >= 0  ) {
 			bundleName = bundlePath.substring(lastDotIdx + 1);
 			bundlePackage = bundlePath.substring(0, lastDotIdx).replace(".",
 			"/");
 		}
 		
-		URL bundlePackageURL = ClassLoader.getSystemResource(bundlePackage);
-		String newBundleName = bundleName + "_" + l.toString() + ".properties";
-		File newBundleFile = new File(new File(bundlePackageURL.toURI()),
-				newBundleName);
+		// determine the exact package URL on the file system
+        String newBundleName = bundleName + "_" + l.toString() + ".properties";
+        File   newBundleFile = null;
+		if ( rootPath != null ) {
+		  // use the given root path
+		  File bundlePackageFile = new File(rootPath,bundlePackage.replaceAll("\\.", "/"));
+          newBundleFile = new File(bundlePackageFile,newBundleName);
+		} else {
+		  // use the classpath
+		  URL bundlePackageURL = ClassLoader.getSystemResource(bundlePackage);
+		  newBundleFile = new File(new File(bundlePackageURL.toURI()),
+		                                  newBundleName);
+		}
 		return newBundleFile;
 	}
 
@@ -675,10 +694,15 @@
 	 * @param append
 	 *            wenn {@code true}, wird eine bestehende Datei erweitert,
 	 *            andernfalls ueberschrieben
+	 * @param rootPath
+	 *            
 	 */
 	public static void createPropertyFile(ResourceProvider provider, Locale l,
-			boolean append) throws URISyntaxException, FileNotFoundException {
-		File newBundleFile = getPropertyFile(provider, l);
+			boolean append, File rootPath) throws URISyntaxException, FileNotFoundException, MalformedURLException {
+		File newBundleFile = getPropertyFile(provider, l, rootPath);
+		// create package subdirectories if necessary
+		if ( newBundleFile.getParentFile() != null )
+		  newBundleFile.getParentFile().mkdirs(); 
 		PrintWriter out = new PrintWriter(new FileOutputStream(newBundleFile,
 				append));
 		// Wenn Datei neu angelegt wird, wird ein Header erstellt

Modified: trunk/src/schmitzm/swing/ResourceProviderManagerFrame.java
===================================================================
--- trunk/src/schmitzm/swing/ResourceProviderManagerFrame.java	2010-06-03 09:19:08 UTC (rev 884)
+++ trunk/src/schmitzm/swing/ResourceProviderManagerFrame.java	2010-06-03 10:15:27 UTC (rev 885)
@@ -61,6 +61,8 @@
 
   private SelectionInputOption newLangDialog_LangDesc = null;
   private ManualInputOption    newLangDialog_LangCode = null;
+  
+  private File    rootPath      = null;
 
   /**
    * Creates a new dialog.
@@ -142,6 +144,25 @@
   }
 
   /**
+   * Sets the root path underneath the new language files are stored. This
+   * folder must be included in the classpath!!
+   * @param rootPath the root path ({@code null} is allowed, but the location
+   *                 of the new files is undefined if the bundle package is
+   *                 ambiguous in the classpath!)
+   */
+  public void setRootPath(File rootPath) {
+    this.rootPath = rootPath;
+  }
+
+  /**
+   * Returns the root path underneath the new language files are stored. This
+   * folder must be included in the classpath!!
+   */
+  public File getRootPath() {
+    return rootPath;
+  }
+
+  /**
    * Initializes the dialog to create new languages.
    */
   private void initNewLanguageDialog() {
@@ -225,7 +246,7 @@
     try {
       // check whether the file exists for one bundle
       for ( ResourceProvider rp : ResourceProvider.getRegisteredResourceProvider() ) {
-        File file = ResourceProvider.getPropertyFile(rp,newLocale);
+        File file = ResourceProvider.getPropertyFile(rp,newLocale,getRootPath());
         if ( file.exists() ) {
           SwingUtil.FileOverrideOption answer = SwingUtil.approveFileOverwrite(this,file.getName(),true);
           if ( answer.equals( SwingUtil.FileOverrideOption.CANCEL ) )
@@ -236,14 +257,14 @@
       }
       // If all files are approved to overwrite, delete the files
       for ( ResourceProvider rp : ResourceProvider.getRegisteredResourceProvider() ) {
-        File file = ResourceProvider.getPropertyFile(rp,newLocale);
+        File file = ResourceProvider.getPropertyFile(rp,newLocale,getRootPath());
         if ( file.exists() )
           file.delete();
       }
       // Create new files (with APPEND-function, so extended bundles are
       // not overwritten directly)
       for ( ResourceProvider rp : ResourceProvider.getRegisteredResourceProvider() )
-        ResourceProvider.createPropertyFile(rp, newLocale, true);
+        ResourceProvider.createPropertyFile(rp, newLocale, true, getRootPath());
 
       // Update table
       ResourceBundle.clearCache();



More information about the Schmitzm-commits mailing list