update emoji autocorrect entries from po-files
[LibreOffice.git] / swext / mediawiki / src / com / sun / star / wiki / WikiArticle.java
blobe16db7156831417adcc6fa49b6ea2230324b201d
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 java.io.StringReader;
22 import java.util.Map;
24 import javax.swing.text.html.HTMLEditorKit;
26 import org.apache.commons.httpclient.HostConfiguration;
27 import org.apache.commons.httpclient.URI;
28 import org.apache.commons.httpclient.methods.GetMethod;
29 import org.apache.commons.httpclient.methods.PostMethod;
31 import com.sun.star.uno.XComponentContext;
34 public class WikiArticle
36 private final XComponentContext m_xContext;
38 private String m_sEditTime = "";
39 private String m_sEditToken = "";
41 private String m_sHTMLCode;
42 private boolean m_bNoArticle = true;
44 private String m_sWikiUser;
45 private String m_sWikiPass;
47 private final String m_sTitle;
49 private final URI m_aMainURI;
50 private HostConfiguration m_aHostConfig;
53 /** Creates a new instance of WikiArticle */
54 public WikiArticle( XComponentContext xContext, String sTitle, Map<String,String> wikiSettings, boolean bLogin, WikiPropDialog aPropDialog )
55 throws java.net.MalformedURLException, java.io.IOException, WikiCancelException
57 m_xContext = xContext;
59 String sMainUrl = wikiSettings.get("Url");
60 m_sWikiUser = wikiSettings.get("Username");
61 m_sWikiPass = wikiSettings.get("Password");
62 m_sTitle = sTitle;
64 m_aMainURI = new URI( sMainUrl, false );
66 if ( bLogin )
68 WikiEditSettingDialog aDialog = new WikiEditSettingDialog(m_xContext, "vnd.sun.star.script:WikiEditor.EditSetting?location=application", wikiSettings, false );
69 try
71 while( !Login() )
73 if ( aPropDialog != null )
74 aPropDialog.SetThrobberActive( false );
76 if ( MainThreadDialogExecutor.Show( xContext, aDialog ) )
78 m_sWikiUser = wikiSettings.get("Username");
79 m_sWikiPass = wikiSettings.get("Password");
81 else
82 throw new WikiCancelException();
84 if ( aPropDialog != null )
86 aPropDialog.SetThrobberActive( true );
87 Thread.yield();
91 finally
93 aDialog.DisposeDialog();
97 // in case of loading the html contents are used
98 // in case of saving the contents should be checked whether they are empty
99 InitArticleHTML();
102 public String GetMainURL()
104 return m_aMainURI.toString();
107 public String GetTitle()
109 return m_sTitle;
114 private String getArticleWiki()
115 throws java.io.IOException, WikiCancelException
117 String sWikiCode = null;
119 if ( m_aHostConfig != null )
121 URI aURI = new URI( m_aMainURI.toString() + "index.php?title=" + m_sTitle + "&action=edit", false );
122 GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() );
124 Helper.ExecuteMethod( aRequest, m_aHostConfig, aURI, m_xContext, false );
126 int nResultCode = aRequest.getStatusCode();
127 String sWebPage = null;
128 if ( nResultCode == 200 )
129 sWebPage = aRequest.getResponseBodyAsString();
131 aRequest.releaseConnection();
133 if ( sWebPage != null )
135 StringReader r = new StringReader(sWebPage);
136 HTMLEditorKit.Parser parse = Helper.GetHTMLParser();
137 EditPageParser callback = new EditPageParser();
139 parse.parse(r,callback,true);
140 m_sEditTime = callback.m_sEditTime;
141 m_sEditToken = callback.m_sEditToken;
143 int iPosStart = callback.m_nWikiArticleStart;
144 int iPosEnd = callback.m_nWikiArticleEnd;
146 if ( iPosStart >= 0 && iPosEnd > 0 )
148 String sArticle = sWebPage.substring(iPosStart, iPosEnd);
149 iPosStart = sArticle.indexOf('>') + 1;
150 sWikiCode = sArticle.substring( iPosStart, sArticle.length() );
155 return sWikiCode;
158 private void InitArticleHTML()
159 throws java.io.IOException, WikiCancelException
161 if ( m_aHostConfig != null )
163 URI aURI = new URI( m_aMainURI.toString() + "index.php?title=" + m_sTitle, false );
164 GetMethod aRequest = new GetMethod( aURI.getEscapedPathQuery() );
166 Helper.ExecuteMethod( aRequest, m_aHostConfig, aURI, m_xContext, false );
168 int nResultCode = aRequest.getStatusCode();
169 String sWebPage = null;
170 if ( nResultCode == 200 )
171 sWebPage = aRequest.getResponseBodyAsString();
173 if ( sWebPage != null )
175 StringReader r = new StringReader(sWebPage);
176 HTMLEditorKit.Parser parse = Helper.GetHTMLParser();
177 EditPageParser callback = new EditPageParser();
179 parse.parse(r,callback,true);
181 int iPosStart = callback.m_nHTMLArticleStart;
182 int iPosEnd = callback.m_nHTMLArticleEnd;
183 int nPosNoArt = callback.m_nNoArticleInd;
185 if ( iPosStart >= 0 && iPosEnd > 0 )
187 m_sHTMLCode = sWebPage.substring(iPosStart, iPosEnd);
188 m_bNoArticle = ( nPosNoArt >= 0 && nPosNoArt >= iPosStart && nPosNoArt <= iPosEnd );
194 protected boolean setArticle( String sWikiCode, String sWikiComment, boolean bMinorEdit )
195 throws java.io.IOException, WikiCancelException
197 boolean bResult = false;
199 if ( m_aHostConfig != null && sWikiCode != null && sWikiComment != null )
201 // get the edit time and token
202 getArticleWiki();
204 URI aURI = new URI( m_aMainURI.getPath() + "index.php?title=" + m_sTitle + "&action=submit", false );
205 PostMethod aPost = new PostMethod();
206 aPost.setPath( aURI.getEscapedPathQuery() );
208 aPost.addParameter( "wpTextbox1", sWikiCode );
209 aPost.addParameter( "wpSummary", sWikiComment );
210 aPost.addParameter( "wpSection", "" );
211 aPost.addParameter( "wpEdittime", m_sEditTime );
212 aPost.addParameter( "wpSave", "Save page" );
213 aPost.addParameter( "wpEditToken", m_sEditToken );
215 if ( bMinorEdit )
216 aPost.addParameter( "wpMinoredit", "1" );
218 Helper.ExecuteMethod( aPost, m_aHostConfig, aURI, m_xContext, false );
220 int nResultCode = aPost.getStatusCode();
221 if ( nResultCode < 400 )
222 bResult = true;
224 String aResult = aPost.getResponseBodyAsString();
226 // TODO: remove the debug printing, try to detect the error
227 System.out.print( "nSubmitCode = " + nResultCode + "\n===\n" + aResult );
230 return bResult;
233 private boolean Login()
234 throws java.io.IOException, WikiCancelException
236 m_aHostConfig = Helper.Login( m_aMainURI, m_sWikiUser, m_sWikiPass, m_xContext );
237 return ( m_aHostConfig != null );
243 protected boolean NotExist()
245 boolean bResult = true;
246 if ( m_sHTMLCode != null )
247 bResult = m_bNoArticle;
249 return bResult;