update emoji autocorrect entries from po-files
[LibreOffice.git] / swext / mediawiki / src / com / sun / star / wiki / Helper.java
blob5900e9aa96cd147d3674fad2550a44b823d56b5d
1 /*
2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com.sun.star.wiki;
21 import com.sun.star.awt.MessageBoxButtons;
22 import com.sun.star.awt.MessageBoxType;
23 import com.sun.star.awt.XControl;
24 import com.sun.star.awt.XDialog;
25 import com.sun.star.awt.XMessageBox;
26 import com.sun.star.awt.XMessageBoxFactory;
27 import com.sun.star.awt.XWindowPeer;
28 import com.sun.star.beans.NamedValue;
29 import com.sun.star.beans.PropertyValue;
30 import com.sun.star.beans.XPropertySet;
31 import com.sun.star.container.XContainerQuery;
32 import com.sun.star.container.XEnumeration;
33 import com.sun.star.container.XNameAccess;
34 import com.sun.star.container.XNameContainer;
35 import com.sun.star.document.XDocumentPropertiesSupplier;
36 import com.sun.star.document.XDocumentProperties;
37 import com.sun.star.frame.XModel;
38 import com.sun.star.frame.XModuleManager;
39 import com.sun.star.io.XInputStream;
40 import com.sun.star.io.XOutputStream;
41 import com.sun.star.lang.XMultiComponentFactory;
42 import com.sun.star.lang.XMultiServiceFactory;
43 import com.sun.star.lang.XComponent;
44 import com.sun.star.system.SystemShellExecuteFlags;
45 import com.sun.star.system.XSystemShellExecute;
46 import com.sun.star.task.UrlRecord;
47 import com.sun.star.task.XInteractionHandler;
48 import com.sun.star.task.XMasterPasswordHandling;
49 import com.sun.star.task.XPasswordContainer;
50 import com.sun.star.uno.AnyConverter;
51 import com.sun.star.uno.UnoRuntime;
52 import com.sun.star.uno.XComponentContext;
53 import com.sun.star.util.XChangesBatch;
54 import java.net.*;
55 import java.io.*;
56 import javax.net.ssl.SSLException;
57 import javax.swing.text.html.HTMLEditorKit;
59 import org.apache.commons.httpclient.HttpClient;
60 import org.apache.commons.httpclient.HostConfiguration;
61 import org.apache.commons.httpclient.HttpMethodBase;
62 import org.apache.commons.httpclient.cookie.CookiePolicy;
63 import org.apache.commons.httpclient.MultiThreadedHttpConnectionManager;
64 import org.apache.commons.httpclient.protocol.Protocol;
65 import org.apache.commons.httpclient.URI;
66 import org.apache.commons.httpclient.methods.GetMethod;
67 import org.apache.commons.httpclient.methods.PostMethod;
69 public class Helper
71 public final static int GENERALSEND_ERROR = 0;
72 public final static int NOWIKIFILTER_ERROR = 1;
73 public final static int NOURLCONNECTION_ERROR = 2;
74 public final static int WRONGLOGIN_ERROR = 3;
75 public final static int INVALIDURL_ERROR = 4;
76 public final static int NOURL_ERROR = 5;
78 public final static int DLG_SENDTITLE = 6;
79 public final static int DLG_WIKIARTICLE = 7;
81 public final static int DLG_OK = 9;
83 // 11 is reserved
84 public final static int DLG_ADDBUTTON = 12;
85 public final static int DLG_EDITBUTTON = 13;
86 public final static int DLG_SENDBUTTON = 14;
87 public final static int DLG_REMOVEBUTTON = 15;
89 public final static int DLG_EDITSETTING_URLLABEL = 16;
90 public final static int DLG_EDITSETTING_USERNAMELABEL = 17;
91 public final static int DLG_EDITSETTING_PASSWORDLABEL = 18;
93 public final static int DLG_SENDTOMEDIAWIKI_LABEL1 = 20;
94 public final static int DLG_SENDTOMEDIAWIKI_LABEL2 = 21;
95 public final static int DLG_SENDTOMEDIAWIKI_LABEL3 = 22;
96 public final static int DLG_SENDTOMEDIAWIKI_MINORCHECK = 23;
97 public final static int DLG_SENDTOMEDIAWIKI_BROWSERCHECK = 24;
98 public final static int UNKNOWNCERT_ERROR = 25;
99 public final static int DLG_MEDIAWIKI_TITLE = 26;
100 public final static int DLG_EDITSETTING_ACCOUNTLINE = 27;
101 public final static int DLG_EDITSETTING_WIKILINE = 28;
102 public final static int DLG_EDITSETTING_SAVEBOX = 29;
103 public final static int CANCELSENDING_ERROR = 30;
104 public final static int DLG_MEDIAWIKIEXTENSION_STRING = 31;
105 public final static int DLG_WIKIPAGEEXISTS_LABEL1 = 32;
107 private final static int STRINGS_NUM = 33;
109 private final static String[] m_pEntryNames = { "GeneralSendError",
110 "NoWikiFilter",
111 "NoConnectionToURL",
112 "WrongLogin",
113 "InvalidURL",
114 "NoURL",
115 "Dlg_SendTitle",
116 "Dlg_WikiArticle",
117 "Dlg_No",
118 "Dlg_OK",
119 "Dlg_Yes",
120 null, // reserved
121 "Dlg_AddButton",
122 "Dlg_EditButton",
123 "Dlg_SendButton",
124 "Dlg_RemoveButton",
125 "Dlg_EditSetting_UrlLabel",
126 "Dlg_EditSetting_UsernameLabel",
127 "Dlg_EditSetting_PasswordLabel",
128 "Dlg_NewWikiPage_Label1",
129 "Dlg_SendToMediaWiki_Label1",
130 "Dlg_SendToMediaWiki_Label2",
131 "Dlg_SendToMediaWiki_Label3",
132 "Dlg_SendToMediaWiki_MinorCheck",
133 "Dlg_SendToMediaWiki_BrowserCheck",
134 "UnknownCert",
135 "Dlg_MediaWiki_Title",
136 "Dlg_EditSetting_AccountLine",
137 "Dlg_EditSetting_WikiLine",
138 "Dlg_EditSetting_SaveBox",
139 "CancelSending",
140 "Dlg_MediaWiki_Extension_String",
141 "Dlg_WikiPageExists_Label1" };
143 private static String[] m_pConfigStrings;
145 private static MultiThreadedHttpConnectionManager m_aConnectionManager;
146 private static HttpClient m_aClient;
147 private static boolean m_bAllowConnection = true;
149 private static Boolean m_bShowInBrowser = null;
151 private static XPasswordContainer m_xPasswordContainer;
152 private static XInteractionHandler m_xInteractionHandler;
154 synchronized protected static String GetLocalizedString( XComponentContext xContext, int nID )
155 throws com.sun.star.uno.Exception
157 if ( nID >= STRINGS_NUM )
158 throw new com.sun.star.uno.RuntimeException();
160 if ( m_pConfigStrings == null )
162 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/Strings" );
164 String[] pStrings = new String[STRINGS_NUM];
165 for ( int nInd = 0; nInd < STRINGS_NUM; nInd++ )
166 if ( m_pEntryNames[nInd] != null )
167 pStrings[nInd] = AnyConverter.toString( xNameAccess.getByName( m_pEntryNames[nInd] ) );
168 else
169 pStrings[nInd] = "";
171 m_pConfigStrings = pStrings;
174 return m_pConfigStrings[nID];
177 synchronized private static HttpClient GetHttpClient()
178 throws WikiCancelException
180 if ( !m_bAllowConnection )
181 throw new WikiCancelException();
183 if ( m_aConnectionManager == null )
184 m_aConnectionManager = new MultiThreadedHttpConnectionManager();
186 if ( m_aClient == null )
188 m_aClient = new HttpClient( m_aConnectionManager );
189 m_aClient.getParams().setParameter( "http.protocol.cookie-policy", CookiePolicy.BROWSER_COMPATIBILITY );
190 m_aClient.getParams().setParameter( "http.protocol.single-cookie-header", Boolean.TRUE );
191 m_aClient.getParams().setParameter( "http.protocol.content-charset", "UTF-8" );
194 return m_aClient;
197 synchronized protected static void AllowConnection( boolean bAllow )
199 m_bAllowConnection = bAllow;
200 if ( !bAllow && m_aConnectionManager != null )
202 m_aClient = null;
203 m_aConnectionManager.shutdown();
204 m_aConnectionManager = null;
208 synchronized protected static boolean IsConnectionAllowed()
210 return m_bAllowConnection;
213 synchronized protected static boolean GetShowInBrowserByDefault( XComponentContext xContext )
215 if ( m_bShowInBrowser == null )
219 XNameAccess xAccess = Helper.GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/Settings" );
220 m_bShowInBrowser = Boolean.valueOf( AnyConverter.toBoolean( xAccess.getByName( "PreselectShowBrowser" ) ) );
222 catch( com.sun.star.uno.Exception e )
224 e.printStackTrace();
228 return m_bShowInBrowser.booleanValue();
231 synchronized protected static void SetShowInBrowserByDefault( XComponentContext xContext, boolean bValue )
235 m_bShowInBrowser = Boolean.valueOf( bValue );
237 XPropertySet xProps = Helper.GetConfigProps( xContext, "org.openoffice.Office.Custom.WikiExtension/Settings" );
238 xProps.setPropertyValue( "PreselectShowBrowser", Boolean.valueOf( bValue ) );
239 XChangesBatch xBatch = UnoRuntime.queryInterface( XChangesBatch.class, xProps );
240 if ( xBatch != null )
241 xBatch.commitChanges();
243 catch( Exception e )
245 e.printStackTrace();
249 synchronized protected static XPasswordContainer GetPasswordContainer( XComponentContext xContext )
250 throws com.sun.star.uno.Exception
252 if ( m_xPasswordContainer == null && xContext != null )
254 XMultiComponentFactory xFactory = xContext.getServiceManager();
255 if ( xFactory != null )
256 m_xPasswordContainer = UnoRuntime.queryInterface(
257 XPasswordContainer.class,
258 xFactory.createInstanceWithContext( "com.sun.star.task.PasswordContainer", xContext ) );
261 if ( m_xPasswordContainer == null )
262 throw new com.sun.star.uno.RuntimeException();
264 return m_xPasswordContainer;
267 synchronized protected static XInteractionHandler GetInteractionHandler( XComponentContext xContext )
268 throws com.sun.star.uno.Exception
270 if ( m_xInteractionHandler == null && xContext != null )
272 XMultiComponentFactory xFactory = xContext.getServiceManager();
273 if ( xFactory != null )
274 m_xInteractionHandler = UnoRuntime.queryInterface(
275 XInteractionHandler.class,
276 xFactory.createInstanceWithContext( "com.sun.star.task.InteractionHandler", xContext ) );
279 if ( m_xInteractionHandler == null )
280 throw new com.sun.star.uno.RuntimeException();
282 return m_xInteractionHandler;
285 private static Protocol GetOwnHttps( int nPort )
287 return new Protocol( "https", new WikiProtocolSocketFactory(), ( ( nPort < 0 ) ? 443 : nPort ) );
290 protected static String GetMainURL( String sWebPage, String sVURL )
292 String sResultURL = "";
295 StringReader aReader = new StringReader( sWebPage );
296 HTMLEditorKit.Parser aParser = GetHTMLParser();
297 EditPageParser aCallback = new EditPageParser();
299 aParser.parse( aReader, aCallback, true );
300 sResultURL = aCallback.m_sMainURL;
302 if ( !sResultURL.startsWith( "http" ) )
304 //if the url is only relative then complete it
305 URL aURL = new URL( sVURL );
306 sResultURL = aURL.getProtocol() + "://" + aURL.getHost() + sResultURL;
309 catch( Exception e )
311 e.printStackTrace();
314 if ( sResultURL.length() == 0 )
316 // usually that should not happen
317 // workaround: try to get index.php from the provided URL
318 int nIndex = sVURL.indexOf( "index.php" );
319 if ( nIndex >= 0 )
320 sResultURL = sVURL.substring( 0, nIndex );
323 return sResultURL;
326 protected static String GetRedirectURL( String sWebPage, String sURL )
328 //scrape the HTML source and find the EditURL
329 // TODO/LATER: Use parser in future
331 int nInd = sWebPage.indexOf( "http-equiv=\"refresh\"" );
332 if ( nInd == -1 )
333 return "";
335 String sResultURL = "";
336 int nContent = sWebPage.indexOf( "content=", nInd );
337 if ( nContent > 0 )
339 int nURL = sWebPage.indexOf( "URL=", nContent );
340 if ( nURL > 0 )
342 int nEndURL = sWebPage.indexOf('"', nURL );
343 if ( nEndURL > 0 )
344 sResultURL = sWebPage.substring( nURL + 4, nEndURL );
350 URL aURL = new URL( sURL );
351 if ( !sResultURL.startsWith( aURL.getProtocol() ))
353 //if the url is only relative then complete it
354 if ( sResultURL.startsWith( "/" ) )
355 sResultURL = aURL.getProtocol() + "://" + aURL.getHost() + sResultURL;
356 else
357 sResultURL = aURL.getProtocol() + "://" + aURL.getHost() + aURL.getPath() + sResultURL;
360 catch ( MalformedURLException ex )
362 ex.printStackTrace();
365 return sResultURL;
372 protected static String CreateTempFile( XComponentContext xContext )
374 String sURL = "";
377 Object oTempFile = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.io.TempFile", xContext );
378 XPropertySet xPropertySet = UnoRuntime.queryInterface( XPropertySet.class, oTempFile );
379 xPropertySet.setPropertyValue( "RemoveFile", Boolean.FALSE );
380 sURL = ( String ) xPropertySet.getPropertyValue( "Uri" );
382 XInputStream xInputStream = UnoRuntime.queryInterface( XInputStream.class, oTempFile );
383 xInputStream.closeInput();
384 XOutputStream xOutputStream = UnoRuntime.queryInterface( XOutputStream.class, oTempFile );
385 xOutputStream.closeOutput();
386 } catch ( com.sun.star.uno.Exception ex )
388 ex.printStackTrace();
390 return sURL;
393 protected static String EachLine( String sURL )
395 String sText = "";
398 URL aURL = new URL( sURL );
399 File aFile = new File( aURL.getFile() );
400 InputStreamReader aInputReader = new InputStreamReader( new FileInputStream( aFile ), "UTF-8" );
401 BufferedReader aBufReader = new BufferedReader( aInputReader );
403 StringBuffer aBuf = new StringBuffer();
404 String sEachLine = aBufReader.readLine();
406 while( sEachLine != null )
408 aBuf.append( sEachLine );
409 aBuf.append( "\n" );
411 sEachLine = aBufReader.readLine();
413 sText = aBuf.toString();
415 aBufReader.close();
416 } catch ( Exception e )
418 e.printStackTrace();
420 return sText;
423 protected static String GetDocTitle( XModel xDoc )
425 XDocumentPropertiesSupplier xDocPropSup =
426 UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, xDoc);
427 XDocumentProperties xDocProps = xDocPropSup.getDocumentProperties();
428 return xDocProps.getTitle();
431 protected static void SetDocTitle( XModel xDoc, String sTitle )
433 XDocumentPropertiesSupplier xDocPropSup =
434 UnoRuntime.queryInterface(XDocumentPropertiesSupplier.class, xDoc);
435 XDocumentProperties xDocProps = xDocPropSup.getDocumentProperties();
436 xDocProps.setTitle(sTitle);
439 protected static String GetDocServiceName( XComponentContext xContext, XModel xModel )
441 String aDocServiceName = "";
442 if ( xModel != null && xContext != null )
446 XMultiComponentFactory xFactory = xContext.getServiceManager();
447 if ( xFactory == null )
448 throw new com.sun.star.uno.RuntimeException();
450 Object oModuleManager = xFactory.createInstanceWithContext( "com.sun.star.frame.ModuleManager", xContext );
451 XModuleManager xModuleManager = UnoRuntime.queryInterface( XModuleManager.class, oModuleManager );
452 if ( xModuleManager != null )
453 aDocServiceName = xModuleManager.identify( xModel );
455 catch( java.lang.Exception e )
457 e.printStackTrace();
461 return aDocServiceName;
464 protected static String GetFilterName( XComponentContext xContext, String aTypeName, String aDocServiceName )
466 if ( xContext == null || aTypeName == null || aTypeName.length() == 0
467 || aDocServiceName == null || aDocServiceName.length() == 0 ) {
468 return "";
470 String aFilterName = "";
473 Object oFilterFactory = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.document.FilterFactory", xContext );
474 XContainerQuery xQuery = UnoRuntime.queryInterface( XContainerQuery.class, oFilterFactory );
475 if ( xQuery != null )
477 NamedValue[] aRequest = new NamedValue[2];
478 aRequest[0] = new NamedValue( "Type", aTypeName );
479 aRequest[1] = new NamedValue( "DocumentService", aDocServiceName );
481 XEnumeration xSet = xQuery.createSubSetEnumerationByProperties( aRequest );
482 if ( xSet != null )
484 boolean bAcceptable = false;
485 while ( xSet.hasMoreElements() && !bAcceptable )
487 PropertyValue[] pFilterProps = ( PropertyValue[] )AnyConverter.toArray( xSet.nextElement() );
488 if ( pFilterProps != null )
490 int nLen = pFilterProps.length;
491 String aTmpFilter = null;
493 for ( int nInd = 0; nInd < nLen; nInd++ )
495 if ( pFilterProps[nInd].Name.equals( "Name" ) )
496 aTmpFilter = AnyConverter.toString( pFilterProps[nInd].Value );
497 else if ( pFilterProps[nInd].Name.equals( "Flags" ) )
498 bAcceptable = ( ( AnyConverter.toInt( pFilterProps[nInd].Value ) & 2 ) == 2 ); // must allow export
501 if ( bAcceptable )
502 aFilterName = aTmpFilter;
508 catch( java.lang.Exception e )
510 e.printStackTrace();
513 return aFilterName;
516 private static XMultiServiceFactory GetConfigurationProvider( XComponentContext xContext )
517 throws com.sun.star.uno.Exception
519 XMultiServiceFactory xConfigurationProvider = null;
520 if ( xContext != null )
522 XMultiComponentFactory xFactory = xContext.getServiceManager();
523 Object oConfigProvider = xFactory.createInstanceWithContext( "com.sun.star.configuration.ConfigurationProvider", xContext );
524 xConfigurationProvider = UnoRuntime.queryInterface( XMultiServiceFactory.class, oConfigProvider );
527 if ( xConfigurationProvider == null )
528 throw new com.sun.star.uno.RuntimeException();
530 return xConfigurationProvider;
533 private static Object GetConfig( XComponentContext xContext, String sNodepath, boolean bWriteAccess )
534 throws com.sun.star.uno.Exception
536 if ( xContext == null || sNodepath == null )
537 throw new com.sun.star.uno.RuntimeException();
539 PropertyValue aVal = new PropertyValue();
540 aVal.Name = "nodepath";
541 aVal.Value = sNodepath;
542 Object[] aArgs = new Object[1];
543 aArgs[0] = aVal;
545 return GetConfigurationProvider( xContext ).createInstanceWithArguments(
546 ( bWriteAccess ? "com.sun.star.configuration.ConfigurationUpdateAccess"
547 : "com.sun.star.configuration.ConfigurationAccess" ),
548 aArgs );
551 private static XPropertySet GetConfigProps( XComponentContext xContext, String sNodepath )
552 throws com.sun.star.uno.Exception
554 XPropertySet xProps = UnoRuntime.queryInterface( XPropertySet.class, GetConfig( xContext, sNodepath, true ) );
555 if ( xProps == null )
556 throw new com.sun.star.uno.RuntimeException();
558 return xProps;
562 protected static XNameContainer GetConfigNameContainer( XComponentContext xContext, String sNodepath )
563 throws com.sun.star.uno.Exception
565 XNameContainer xContainer = UnoRuntime.queryInterface( XNameContainer.class, GetConfig( xContext, sNodepath, true ) );
566 if ( xContainer == null )
567 throw new com.sun.star.uno.RuntimeException();
569 return xContainer;
572 protected static XNameAccess GetConfigNameAccess( XComponentContext xContext, String sNodepath )
573 throws com.sun.star.uno.Exception
575 XNameAccess xNameAccess = UnoRuntime.queryInterface( XNameAccess.class, GetConfig( xContext, sNodepath, false ) );
576 if ( xNameAccess == null )
577 throw new com.sun.star.uno.RuntimeException();
579 return xNameAccess;
582 private static void SetConfigurationProxy( HostConfiguration aHostConfig, XComponentContext xContext )
584 if ( aHostConfig == null || xContext == null )
585 return;
589 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Inet/Settings" );
591 int nProxyType = AnyConverter.toInt( xNameAccess.getByName( "ooInetProxyType" ) );
592 if ( nProxyType == 0 )
593 aHostConfig.setProxyHost( null );
594 else
596 if ( nProxyType == 1 )
598 // system proxy
600 else if ( nProxyType == 2 )
602 String aProxyNameProp = "ooInetHTTPProxyName";
603 String aProxyPortProp = "ooInetHTTPProxyPort";
605 if ( aHostConfig.getProtocol().getScheme().equals( "https" ) )
607 aProxyNameProp = "ooInetHTTPSProxyName";
608 aProxyPortProp = "ooInetHTTPSProxyPort";
611 String aProxyName = AnyConverter.toString( xNameAccess.getByName( aProxyNameProp ) );
613 int nProxyPort = 80;
615 Object aPortNo = xNameAccess.getByName( aProxyPortProp );
616 if ( !AnyConverter.isVoid( aPortNo ) )
617 nProxyPort = AnyConverter.toInt( aPortNo );
619 if ( nProxyPort == -1 )
620 nProxyPort = 80;
622 // TODO: check whether the URL is in the NoProxy list
623 aHostConfig.setProxy( aProxyName, nProxyPort );
627 catch( java.lang.Exception e )
629 e.printStackTrace();
633 protected static void ShowURLInBrowser( XComponentContext xContext, String sURL )
635 if ( xContext != null && sURL != null && sURL.length() > 0 )
639 Object oSystemShell = xContext.getServiceManager().createInstanceWithContext( "com.sun.star.system.SystemShellExecute", xContext );
640 XSystemShellExecute xSystemShell = UnoRuntime.queryInterface( XSystemShellExecute.class, oSystemShell );
641 if ( xSystemShell != null )
642 xSystemShell.execute( sURL, "", SystemShellExecuteFlags.URIS_ONLY );
644 catch( Exception e )
646 e.printStackTrace();
651 protected static void ExecuteMethod( HttpMethodBase aMethod, HostConfiguration aHostConfig, URI aURI, XComponentContext xContext, boolean bSetHost )
652 throws WikiCancelException, IOException, SSLException
654 if ( aMethod != null && aHostConfig != null && aURI != null && xContext != null )
656 if ( bSetHost )
658 aHostConfig.setHost( aURI );
659 SetConfigurationProxy( aHostConfig, xContext );
662 if ( aHostConfig.getProtocol().getScheme().equals( "https" )
663 && AllowUnknownCert( xContext, aURI.getHost() ) )
665 // let unknown certificates be accepted
668 aHostConfig.setHost( aHostConfig.getHost(), ( aURI.getPort() < 0 ? 443 : aURI.getPort() ), Helper.GetOwnHttps( aURI.getPort() ) );
669 Helper.GetHttpClient().executeMethod( aHostConfig, aMethod );
673 else
675 Helper.GetHttpClient().executeMethod( aHostConfig, aMethod );
680 static private class HTMLParse extends HTMLEditorKit
683 @Override
684 public HTMLEditorKit.Parser getParser()
686 return super.getParser();
690 static protected HTMLEditorKit.Parser GetHTMLParser()
692 return new HTMLParse().getParser();
695 static private boolean LoginReportsError( String sRespond )
697 boolean bResult = true;
698 if ( sRespond != null )
702 StringReader aReader = new StringReader( sRespond );
703 HTMLEditorKit.Parser aParser = GetHTMLParser();
704 EditPageParser aCallback = new EditPageParser();
706 aParser.parse( aReader, aCallback, true );
707 bResult = ( aCallback.m_nErrorInd >= 0 );
709 catch( Exception e )
711 e.printStackTrace();
715 return bResult;
718 static private String GetLoginToken( String sLoginPage )
720 String sResult = "";
721 if ( sLoginPage != null && sLoginPage.length() > 0 )
725 StringReader aReader = new StringReader( sLoginPage );
726 HTMLEditorKit.Parser aParser = Helper.GetHTMLParser();
727 EditPageParser aCallbacks = new EditPageParser();
729 aParser.parse( aReader, aCallbacks, true );
730 sResult = aCallbacks.m_sLoginToken;
732 catch( Exception e )
734 e.printStackTrace();
738 return sResult;
741 static protected HostConfiguration Login( URI aMainURL, String sWikiUser, String sWikiPass, XComponentContext xContext )
742 throws java.io.IOException, WikiCancelException
744 HostConfiguration aHostConfig = null;
746 if ( sWikiUser != null && sWikiPass != null && xContext != null )
748 HostConfiguration aNewHostConfig = new HostConfiguration();
750 URI aURI = new URI( aMainURL.toString() + "index.php?title=Special:Userlogin", false );
751 GetMethod aGetCookie = new GetMethod( aURI.getEscapedPathQuery() );
753 ExecuteMethod( aGetCookie, aNewHostConfig, aURI, xContext, true );
755 int nResultCode = aGetCookie.getStatusCode();
756 String sLoginPage = null;
757 if ( nResultCode == 200 )
758 sLoginPage = aGetCookie.getResponseBodyAsString();
760 aGetCookie.releaseConnection();
762 if ( sLoginPage != null )
764 String sLoginToken = GetLoginToken( sLoginPage );
766 PostMethod aPost = new PostMethod();
767 URI aPostURI = new URI( aMainURL.getPath() + "index.php?title=Special:Userlogin&action=submitlogin", false );
768 aPost.setPath( aPostURI.getEscapedPathQuery() );
770 aPost.addParameter( "wpName", sWikiUser );
771 aPost.addParameter( "wpRemember", "1" );
772 aPost.addParameter( "wpPassword", sWikiPass );
773 if ( sLoginToken.length() > 0 )
774 aPost.addParameter( "wpLoginToken", sLoginToken );
776 String[][] pArgs = GetSpecialArgs( xContext, aMainURL.getHost() );
777 if ( pArgs != null )
778 for ( int nArgInd = 0; nArgInd < pArgs.length; nArgInd++ )
779 if ( pArgs[nArgInd].length == 2 && pArgs[nArgInd][0] != null && pArgs[nArgInd][1] != null )
780 aPost.addParameter( pArgs[nArgInd][0], pArgs[nArgInd][1] );
782 ExecuteMethod( aPost, aNewHostConfig, aPostURI, xContext, false );
784 nResultCode = aPost.getStatusCode();
786 while( nResultCode >= 301 && nResultCode <= 303 || nResultCode == 307 )
788 String sRedirectURL = aPost.getResponseHeader( "Location" ).getValue();
789 aPost.releaseConnection();
791 aURI = new URI( sRedirectURL );
792 aPost = new PostMethod();
793 aPost.setPath( aURI.getEscapedPathQuery() );
794 ExecuteMethod( aPost, aNewHostConfig, aURI, xContext, false );
796 nResultCode = aPost.getStatusCode();
799 if ( nResultCode == 200 )
801 String sResult = aPost.getResponseBodyAsString();
802 if ( !LoginReportsError( sResult ) )
803 aHostConfig = aNewHostConfig;
806 aPost.releaseConnection();
810 return aHostConfig;
813 protected static String[] GetPasswordsForURLAndUser( XComponentContext xContext, String sURL, String sUserName )
815 String[] aResult = null;
819 if ( xContext != null && sURL != null && sURL.length() > 0 && sUserName != null && sUserName.length() > 0 )
821 UrlRecord aRec = GetPasswordContainer( xContext ).findForName( sURL, sUserName, GetInteractionHandler( xContext ) );
822 if ( aRec != null && aRec.UserList != null && aRec.UserList.length > 0
823 && aRec.UserList[0].UserName.equals( sUserName ) )
824 aResult = aRec.UserList[0].Passwords;
827 catch( Exception e )
829 e.printStackTrace();
832 return aResult;
835 protected static boolean PasswordStoringIsAllowed( XComponentContext xContext )
837 boolean bResult = false;
840 XMasterPasswordHandling xMasterHdl = UnoRuntime.queryInterface( XMasterPasswordHandling.class, GetPasswordContainer( xContext ) );
841 if ( xMasterHdl != null )
842 bResult = xMasterHdl.isPersistentStoringAllowed();
844 catch( Exception e )
846 e.printStackTrace();
849 return bResult;
852 protected static void ShowError( XComponentContext xContext, XDialog xDialog, int nTitleID, int nErrorID, String sArg, boolean bQuery )
854 XWindowPeer xPeer = null;
855 XControl xControl = UnoRuntime.queryInterface( XControl.class, xDialog );
856 if ( xControl != null )
857 xPeer = xControl.getPeer();
858 ShowError( xContext, xPeer, nTitleID, nErrorID, sArg, bQuery );
861 protected static boolean ShowError( XComponentContext xContext, XWindowPeer xParentPeer, int nTitleID, int nErrorID, String sArg, boolean bQuery )
863 boolean bResult = false;
865 if ( xContext != null && nErrorID >= 0 && nErrorID < STRINGS_NUM )
867 String sError = null;
868 String sTitle = "";
872 sError = GetLocalizedString( xContext, nErrorID );
873 if ( sError != null && sArg != null )
874 sError = sError.replaceAll( "\\$ARG1", sArg );
876 sTitle = GetLocalizedString( xContext, nTitleID );
878 catch( Exception e )
880 e.printStackTrace();
883 if ( sError == null )
884 sError = "Error: " + nErrorID;
886 if ( xParentPeer != null )
888 XMessageBoxFactory xMBFactory = null;
889 XMessageBox xMB = null;
892 XMultiComponentFactory xFactory = xContext.getServiceManager();
893 if ( xFactory != null )
894 xMBFactory = UnoRuntime.queryInterface(
895 XMessageBoxFactory.class,
896 xFactory.createInstanceWithContext( "com.sun.star.awt.Toolkit", xContext ) );
898 if ( xMBFactory != null )
900 if ( bQuery )
902 xMB = xMBFactory.createMessageBox(
903 xParentPeer,
904 MessageBoxType.QUERYBOX,
905 MessageBoxButtons.BUTTONS_YES_NO | MessageBoxButtons.DEFAULT_BUTTON_NO,
906 sTitle,
907 sError );
909 else
911 xMB = xMBFactory.createMessageBox(
912 xParentPeer,
913 MessageBoxType.ERRORBOX,
914 MessageBoxButtons.BUTTONS_OK,
915 sTitle,
916 sError );
918 if ( xMB != null )
920 bResult = MainThreadDialogExecutor.Execute( xContext, xMB );
924 catch( Exception e )
926 e.printStackTrace();
928 finally
930 if ( xMB != null )
931 Dispose( xMB );
936 return bResult;
939 private static boolean AllowUnknownCert( XComponentContext xContext, String aURL )
943 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/SpecialData" );
944 if ( xNameAccess.hasByName( aURL ) )
946 XNameAccess xEntry = UnoRuntime.queryInterface( XNameAccess.class, xNameAccess.getByName( aURL ) );
947 if ( xEntry != null && xEntry.hasByName( "AllowUnknownCertificate" ) )
948 return AnyConverter.toBoolean( xEntry.getByName( "AllowUnknownCertificate" ) );
951 catch( Exception e )
953 e.printStackTrace();
956 return false;
959 private static String[][] GetSpecialArgs( XComponentContext xContext, String aURL )
963 XNameAccess xNameAccess = GetConfigNameAccess( xContext, "org.openoffice.Office.Custom.WikiExtension/SpecialData" );
964 if ( xNameAccess.hasByName( aURL ) )
966 XNameAccess xEntry = UnoRuntime.queryInterface( XNameAccess.class, xNameAccess.getByName( aURL ) );
967 if ( xEntry != null )
969 XNameAccess xArgs = UnoRuntime.queryInterface( XNameAccess.class, xEntry.getByName( "AdditionalLoginArguments" ) );
970 if ( xArgs != null )
972 String[] pNames = xArgs.getElementNames();
973 if ( pNames != null && pNames.length > 0 )
975 String[][] pResult = new String[pNames.length][2];
976 for ( int nInd = 0; nInd < pNames.length; nInd++ )
978 XNameAccess xArgument = UnoRuntime.queryInterface( XNameAccess.class, xArgs.getByName( pNames[nInd] ) );
979 if ( xArgument == null )
980 throw new com.sun.star.uno.RuntimeException();
982 pResult[nInd][0] = pNames[nInd];
983 pResult[nInd][1] = AnyConverter.toString( xArgument.getByName( "Value" ) );
986 return pResult;
992 catch( Exception e )
994 e.printStackTrace();
997 return null;
1000 protected static boolean AllowThreadUsage( XComponentContext xContext )
1002 if ( xContext != null )
1006 XMultiComponentFactory xFactory = xContext.getServiceManager();
1007 if ( xFactory == null )
1008 throw new com.sun.star.uno.RuntimeException();
1010 Object oCheckCallback = xFactory.createInstanceWithContext( "com.sun.star.awt.AsyncCallback", xContext );
1011 return ( oCheckCallback != null );
1013 catch( Exception e )
1015 e.printStackTrace();
1019 return false;
1022 public static void Dispose( Object oObject )
1024 if ( oObject != null )
1028 XComponent xComp = UnoRuntime.queryInterface( XComponent.class, oObject );
1029 if ( xComp != null )
1030 xComp.dispose();
1032 catch( Exception e )
1034 e.printStackTrace();