From c88eae0866b943e7c664819fadbf4d2c4f0c55a8 Mon Sep 17 00:00:00 2001 From: rbuj Date: Mon, 8 Sep 2014 02:40:54 +0200 Subject: mediawiki: improve storeConfiguration & loadConfiguration methods * Use enhanced for-loops (storeConfiguration & loadConfiguration) * Use curly braces to reduce the variable scope and, to reuse variable names (storeConfiguration) * Avoid map.get(key) in iterations over each map's entry (storeConfiguration): for (Map.Entry entry : ht.entrySet()) Change-Id: I678d5a9f205efb2c89ab868b59e1b654419381d8 Reviewed-on: https://gerrit.libreoffice.org/11331 Reviewed-by: Noel Grandin Tested-by: Noel Grandin --- .../mediawiki/src/com/sun/star/wiki/Settings.java | 101 ++++++++++----------- 1 file changed, 48 insertions(+), 53 deletions(-) (limited to 'swext') diff --git a/swext/mediawiki/src/com/sun/star/wiki/Settings.java b/swext/mediawiki/src/com/sun/star/wiki/Settings.java index 3d87446e93e5..74d60180ef28 100644 --- a/swext/mediawiki/src/com/sun/star/wiki/Settings.java +++ b/swext/mediawiki/src/com/sun/star/wiki/Settings.java @@ -20,7 +20,6 @@ package com.sun.star.wiki; import java.util.ArrayList; import java.util.HashMap; -import java.util.Iterator; import java.util.List; import java.util.Map; @@ -214,58 +213,56 @@ public class Settings { try { - // remove stored connection information - XNameContainer xContainer = Helper.GetConfigNameContainer( m_xContext, "org.openoffice.Office.Custom.WikiExtension/ConnectionList" ); - String[] pNames = xContainer.getElementNames(); - for( int i=0; i ht : m_WikiConnections) + { + Object oNewConnection = xConnectionFactory.createInstance(); + XNameReplace xNewConn = UnoRuntime.queryInterface(XNameReplace.class, oNewConnection); + if (xNewConn != null) + { + xNewConn.replaceByName("UserName", ht.get("Username")); + } + xContainer.insertByName(ht.get("Url"), xNewConn); + } + // commit changes + XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, xContainer); + xBatch.commitChanges(); } - // store all connections - XSingleServiceFactory xConnectionFactory = UnoRuntime.queryInterface( XSingleServiceFactory.class, xContainer ); - for ( int i=0; i< m_WikiConnections.size(); i++ ) { - Object oNewConnection = xConnectionFactory.createInstance(); - Map ht = m_WikiConnections.get( i ); - XNameReplace xNewConn = UnoRuntime.queryInterface( XNameReplace.class, oNewConnection ); - - if ( xNewConn != null ) - xNewConn.replaceByName( "UserName", ht.get( "Username" ) ); - - xContainer.insertByName( ht.get( "Url" ), xNewConn ); - } - // commit changes - XChangesBatch xBatch = UnoRuntime.queryInterface( XChangesBatch.class, xContainer ); - xBatch.commitChanges(); - - // remove stored connection information - XNameContainer xContainer2 = Helper.GetConfigNameContainer( m_xContext, "org.openoffice.Office.Custom.WikiExtension/RecentDocs" ); - String[] pNames2 = xContainer2.getElementNames(); - for( int i=0; i ht = m_aWikiDocs.get( i ); - - Object oNewDoc = xDocListFactory.createInstance(); - XNameReplace xNewDoc = UnoRuntime.queryInterface( XNameReplace.class, oNewDoc ); - - for ( Iterator iter = ht.keySet().iterator(); iter.hasNext(); ) + // remove stored connection information + XNameContainer xContainer = Helper.GetConfigNameContainer(m_xContext, "org.openoffice.Office.Custom.WikiExtension/RecentDocs"); + String[] pNames = xContainer.getElementNames(); + for (String pName : pNames) { - String key = iter.next(); - xNewDoc.replaceByName( key, ht.get( key ) ); + xContainer.removeByName(pName); } - - xContainer2.insertByName( "d" + i, xNewDoc ); + // store all Docs + XSingleServiceFactory xDocListFactory = UnoRuntime.queryInterface(XSingleServiceFactory.class, xContainer); + int i = 0; + for (Map ht : m_aWikiDocs) + { + Object oNewDoc = xDocListFactory.createInstance(); + XNameReplace xNewDoc = UnoRuntime.queryInterface(XNameReplace.class, oNewDoc); + for (Map.Entry entry : ht.entrySet()) + { + xNewDoc.replaceByName(entry.getKey(), entry.getValue()); + } + xContainer.insertByName("d" + i++, xNewDoc); + } + // commit changes + XChangesBatch xBatch = UnoRuntime.queryInterface(XChangesBatch.class, xContainer); + xBatch.commitChanges(); } - // commit changes - XChangesBatch xBatch2 = UnoRuntime.queryInterface( XChangesBatch.class, xContainer2 ); - xBatch2.commitChanges(); } catch ( Exception ex ) @@ -288,16 +285,15 @@ public class Settings Object oList = xAccess.getByName( "ConnectionList" ); XNameAccess xConnectionList = UnoRuntime.queryInterface( XNameAccess.class, oList ); String [] allCons = xConnectionList.getElementNames(); - for ( int i=0; i ht = new HashMap(); - ht.put( "Url", allCons[i] ); + ht.put("Url", aConnection); ht.put( "Username", "" ); ht.put( "Password", "" ); - try { - XPropertySet xProps = UnoRuntime.queryInterface( XPropertySet.class, xConnectionList.getByName( allCons[i] ) ); + XPropertySet xProps = UnoRuntime.queryInterface(XPropertySet.class, xConnectionList.getByName(aConnection)); if ( xProps != null ) { String aUsername = AnyConverter.toString( xProps.getPropertyValue( "UserName" ) ); @@ -309,16 +305,15 @@ public class Settings { e.printStackTrace(); } - addWikiCon( ht ); } Object oDocs = xAccess.getByName( "RecentDocs" ); XNameAccess xRecentDocs = UnoRuntime.queryInterface( XNameAccess.class, oDocs ); String [] allDocs = xRecentDocs.getElementNames(); - for ( int i=0; i ht = new HashMap(); ht.put( "Url", xDoc.getByName( "Url" ) ); -- cgit v1.2.3