Kaydet (Commit) 8e26b478 authored tarafından Zolnai Tamás's avatar Zolnai Tamás

Refactor l10ntools

Delete unused functions.
Make Export class more encapsulated.
Move to local that functions which are used only in one file.
Common contans method which are used by all executables.
Helper contains methods belong to xml parsing.

Change-Id: I28773a2c7eea90da7df7f32720fd38de2cb661ac
üst 239fb4cb
......@@ -43,7 +43,8 @@ $(eval $(call gb_StaticLibrary_use_externals,transex,\
))
$(eval $(call gb_StaticLibrary_add_exception_objects,transex,\
l10ntools/source/export2 \
l10ntools/source/helper \
l10ntools/source/common \
l10ntools/source/merge \
l10ntools/source/po \
))
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/// Methods used by all of executables
#ifndef INCLUDED_L10NTOOLS_SOURCE_COMMON_HXX
#define INCLUDED_L10NTOOLS_SOURCE_COMMON_HXX
#include "sal/config.h"
#include <iostream>
#include <rtl/string.hxx>
#include "po.hxx"
namespace common {
//result type of handleArguments()
struct HandledArgs
{
OString m_sInputFile;
OString m_sOutputFile;
OString m_sMergeSrc;
OString m_sLanguage;
bool m_bMergeMode;
HandledArgs()
: m_sInputFile( OString() )
, m_sOutputFile( OString() )
, m_sMergeSrc( OString() )
, m_sLanguage( OString() )
, m_bMergeMode( false )
{}
};
bool handleArguments(int argc, char * argv[], HandledArgs& o_aHandledArgs);
void writeUsage(const OString& rName, const OString& rFileType);
void writePoEntry(
const OString& rExecutable, PoOfstream& rPoStream, const OString& rSourceFile,
const OString& rResType, const OString& rGroupId, const OString& rLocalId,
const OString& rHelpText, const OString& rText, const PoEntry::TYPE eType = PoEntry::TTEXT );
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -109,6 +109,8 @@ public:
class ResData
{
public:
ResData(const rtl::OString &rPF, const rtl::OString &rGId);
ResData(const rtl::OString &rPF, const rtl::OString &rGId , const rtl::OString &rFilename);
~ResData();
sal_Bool SetId(const rtl::OString &rId, sal_uInt16 nLevel);
......@@ -153,67 +155,6 @@ public:
ExportList *pPairedList;
rtl::OString sPForm;
ResData(const rtl::OString &rPF, const rtl::OString &rGId)
:
nWidth( 0 ),
nChildIndex( 0 ),
nIdLevel( ID_LEVEL_NULL ),
bChild( sal_False ),
bChildWithText( sal_False ),
bText( sal_False ),
bHelpText( sal_False ),
bQuickHelpText( sal_False ),
bTitle( sal_False ),
bList( sal_False ),
bRestMerged( sal_False ),
sGId( rGId ),
nTextRefId( REFID_NONE ),
nHelpTextRefId( REFID_NONE ),
nQuickHelpTextRefId( REFID_NONE ),
nTitleRefId( REFID_NONE ),
sTextTyp( "Text" ),
pStringList( NULL ),
pUIEntries( NULL ),
pItemList( NULL ),
pFilterList( NULL ),
pPairedList( NULL ),
sPForm( rPF )
{
sGId = sGId.replaceAll("\r", rtl::OString());
sPForm = sPForm.replaceAll("\r", rtl::OString());
}
ResData(const rtl::OString &rPF, const rtl::OString &rGId , const rtl::OString &rFilename)
:
nWidth( 0 ),
nChildIndex( 0 ),
nIdLevel( ID_LEVEL_NULL ),
bChild( sal_False ),
bChildWithText( sal_False ),
bText( sal_False ),
bHelpText( sal_False ),
bQuickHelpText( sal_False ),
bTitle( sal_False ),
bList( sal_False ),
bRestMerged( sal_False ),
sGId( rGId ),
sFilename( rFilename ),
nTextRefId( REFID_NONE ),
nHelpTextRefId( REFID_NONE ),
nQuickHelpTextRefId( REFID_NONE ),
nTitleRefId( REFID_NONE ),
sTextTyp( "Text" ),
pStringList( NULL ),
pUIEntries( NULL ),
pItemList( NULL ),
pFilterList( NULL ),
pPairedList( NULL ),
sPForm( rPF )
{
sGId = sGId.replaceAll("\r", rtl::OString());
sPForm = sPForm.replaceAll("\r", rtl::OString());
}
};
......@@ -244,23 +185,6 @@ typedef ::std::vector< ResData* > ResStack;
class WordTransformer;
class ParserQueue;
//result type of handleArguments()
struct HandledArgs
{
OString m_sInputFile;
OString m_sOutputFile;
OString m_sMergeSrc;
OString m_sLanguage;
bool m_bMergeMode;
HandledArgs()
: m_sInputFile( OString() )
, m_sOutputFile( OString() )
, m_sMergeSrc( OString() )
, m_sLanguage( OString() )
, m_bMergeMode( false )
{}
};
class Export
{
private:
......@@ -292,40 +216,11 @@ private:
sal_Bool bReadOver;
sal_Bool bDontWriteOutput;
rtl::OString sLastTextTyp;
static bool isInitialized;
bool isInitialized;
rtl::OString sFilename;
rtl::OString sLanguages;
public:
ParserQueue* pParseQueue; // public ?
static rtl::OString sLanguages; // public ?
static bool handleArguments(int argc, char * argv[], HandledArgs& o_aHandledArgs);
static void writeUsage(const OString& rName, const OString& rFileType);
static void writePoEntry(const OString& rExecutable, PoOfstream& rPoStream, const OString& rSourceFile,
const OString& rResType, const OString& rGroupId, const OString& rLocalId,
const OString& rHelpText, const OString& rText,
const PoEntry::TYPE eType = PoEntry::TTEXT);
static void InitLanguages( bool bMergeMode = false );
static void InitForcedLanguages( bool bMergeMode = false );
static std::vector<rtl::OString> GetLanguages();
static void SetLanguages( std::vector<rtl::OString> val );
static void RemoveUTF8ByteOrderMarker( rtl::OString &rString );
static bool hasUTF8ByteOrderMarker( const rtl::OString &rString );
static rtl::OString QuoteHTML( rtl::OString const &rString );
static rtl::OString UnquoteHTML( rtl::OString const &rString );
static bool isSourceLanguage(const rtl::OString &rLanguage);
static bool isAllowed(const rtl::OString &rLanguage);
static void Languages( std::vector<rtl::OString>::const_iterator& begin , std::vector<rtl::OString>::const_iterator& end );
private:
static std::vector<rtl::OString> aLanguages;
static std::vector<rtl::OString> aForcedLanguages;
std::vector<rtl::OString> aLanguages;
sal_Bool WriteData( ResData *pResData, sal_Bool bCreateNew = sal_False );// called befor dest. cur ResData
sal_Bool WriteExportList( ResData *pResData, ExportList *pExportList,
......@@ -352,17 +247,20 @@ private:
void WriteToMerged(const rtl::OString &rText , bool bSDFContent);
void SetChildWithText();
void InitLanguages( bool bMergeMode = false );
void CutComment( rtl::OString &rText );
public:
Export(const rtl::OString &rOutput);
Export(const rtl::OString &rMergeSource, const rtl::OString &rOutput);
Export(const OString &rOutput, const OString &rLanguage);
Export(const OString &rMergeSource, const OString &rOutput, const OString &rLanguage);
~Export();
void Init();
int Execute( int nToken, const char * pToken ); // called from lexer
void SetError() { bError = sal_True; }
sal_Bool GetError() { return bError; }
ParserQueue* pParseQueue; // public!!
};
......
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
/// Helper methods to work with xml files
#ifndef INCLUDED_L10NTOOLS_SOURCE_HELPER_HXX
#define INCLUDED_L10NTOOLS_SOURCE_HELPER_HXX
#include "sal/config.h"
#include "sal/types.h"
#include <cassert>
#include <libxml/parser.h>
#include <rtl/string.hxx>
#include <rtl/strbuf.hxx>
namespace helper {
OString QuotHTML(const rtl::OString &rString);
bool isWellFormedXML( OString const & text );
//Convert xmlChar* to OString
OString xmlStrToOString( const xmlChar* pString );
}
#endif
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
......@@ -17,7 +17,6 @@
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "export.hxx"
#include "xmlparse.hxx"
#include <rtl/ustring.hxx>
#include <rtl/ustrbuf.hxx>
......
......@@ -22,8 +22,8 @@
#include <iosfwd>
#include <vector>
#include "common.hxx"
#include "export.hxx"
#include "po.hxx"
typedef std::vector< rtl::OString* > LngLineList;
......
......@@ -20,7 +20,6 @@
#include "sal/config.h"
#include <fstream>
#include "po.hxx"
//
// XRMResParser
......
......@@ -47,10 +47,10 @@ extern "C" {
FILE * init(int argc, char ** argv) {
HandledArgs aArgs;
if ( !Export::handleArguments(argc, argv, aArgs) )
common::HandledArgs aArgs;
if ( !common::handleArguments(argc, argv, aArgs) )
{
Export::writeUsage("cfgex","*.xcu");
common::writeUsage("cfgex","*.xcu");
std::exit(EXIT_FAILURE);
}
global::inputPathname = aArgs.m_sInputFile;
......@@ -83,6 +83,88 @@ void workOnTokenSet(int nTyp, char * pTokenText) {
}
namespace
{
static OString lcl_QuoteHTML( const OString& rString )
{
rtl::OStringBuffer sReturn;
for ( sal_Int32 i = 0; i < rString.getLength(); i++ ) {
rtl::OString sTemp = rString.copy( i );
if ( sTemp.match( "<Arg n=" ) ) {
while ( i < rString.getLength() && rString[i] != '>' ) {
sReturn.append(rString[i]);
i++;
}
if ( rString[i] == '>' ) {
sReturn.append('>');
i++;
}
}
if ( i < rString.getLength()) {
switch ( rString[i]) {
case '<':
sReturn.append("&lt;");
break;
case '>':
sReturn.append("&gt;");
break;
case '\"':
sReturn.append("&quot;");
break;
case '\'':
sReturn.append("&apos;");
break;
case '&':
if ((( i + 4 ) < rString.getLength()) &&
( rString.copy( i, 5 ) == "&amp;" ))
sReturn.append(rString[i]);
else
sReturn.append("&amp;");
break;
default:
sReturn.append(rString[i]);
break;
}
}
}
return sReturn.makeStringAndClear();
}
static OString lcl_UnquoteHTML( const OString& rString )
{
rtl::OStringBuffer sReturn;
for (sal_Int32 i = 0; i != rString.getLength();) {
if (rString.match("&amp;", i)) {
sReturn.append('&');
i += RTL_CONSTASCII_LENGTH("&amp;");
} else if (rString.match("&lt;", i)) {
sReturn.append('<');
i += RTL_CONSTASCII_LENGTH("&lt;");
} else if (rString.match("&gt;", i)) {
sReturn.append('>');
i += RTL_CONSTASCII_LENGTH("&gt;");
} else if (rString.match("&quot;", i)) {
sReturn.append('"');
i += RTL_CONSTASCII_LENGTH("&quot;");
} else if (rString.match("&apos;", i)) {
sReturn.append('\'');
i += RTL_CONSTASCII_LENGTH("&apos;");
} else {
sReturn.append(rString[i]);
++i;
}
}
return sReturn.makeStringAndClear();
}
} // anonymous namespace
//
// class CfgStackData
//
......@@ -407,9 +489,9 @@ void CfgExport::WorkOnResourceEnd()
if ( sText.isEmpty())
sText = sFallback;
sText = Export::UnquoteHTML( sText );
sText = lcl_UnquoteHTML( sText );
Export::writePoEntry(
common::writePoEntry(
"Cfgex", pOutputStream, sPath, pStackData->sResTyp,
sGroupId, sLocalId, sXComment, sText);
}
......@@ -422,7 +504,7 @@ void CfgExport::WorkOnText(
const rtl::OString &rIsoLang
)
{
if( rIsoLang.getLength() ) rText = Export::UnquoteHTML( rText );
if( rIsoLang.getLength() ) rText = lcl_UnquoteHTML( rText );
}
......@@ -499,10 +581,10 @@ void CfgMerge::WorkOnText(rtl::OString &rText, const rtl::OString& rLangIndex)
rtl::OString sContent;
pEntrys->GetText( sContent, STRING_TYP_TEXT, rLangIndex );
if ( Export::isAllowed( rLangIndex ) &&
if ( !rLangIndex.equalsIgnoreAsciiCase("en-US") &&
( sContent != "-" ) && !sContent.isEmpty())
{
rText = Export::QuoteHTML( rText );
rText = lcl_QuoteHTML( rText );
}
}
}
......@@ -534,7 +616,7 @@ void CfgMerge::WorkOnResourceEnd()
( sContent != "-" ) && !sContent.isEmpty())
{
rtl::OString sText = Export::QuoteHTML( sContent);
rtl::OString sText = lcl_QuoteHTML( sContent);
rtl::OString sAdditionalLine( "\t" );
......
......@@ -5,90 +5,21 @@
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*
* This file incorporates work covered by the following license notice:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright
* ownership. The ASF licenses this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this file
* except in compliance with the License. You may obtain a copy of
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
*/
#include "sal/config.h"
#include "export.hxx"
#include <stdio.h>
#include <osl/time.h>
#include <osl/process.h>
#include <rtl/strbuf.hxx>
#include <rtl/ustring.hxx>
#include <sal/macros.h>
#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
#include "common.hxx"
#include "po.cxx"
//flags for handleArguments()
#define STATE_NON 0x0001
#define STATE_INPUT 0x0002
#define STATE_OUTPUT 0x0003
#define STATE_MERGESRC 0x0006
#define STATE_LANGUAGES 0x0007
#define STATE_MERGESRC 0x0005
#define STATE_LANGUAGES 0x0006
//
// class ResData();
//
namespace common {
/*****************************************************************************/
ResData::~ResData()
/*****************************************************************************/
{
if ( pStringList ) {
// delete existing res. of type StringList
for ( size_t i = 0; i < pStringList->size(); i++ ) {
ExportListEntry* test = (*pStringList)[ i ];
if( test != NULL ) delete test;
}
delete pStringList;
}
if ( pFilterList ) {
// delete existing res. of type FilterList
for ( size_t i = 0; i < pFilterList->size(); i++ ) {
ExportListEntry* test = (*pFilterList)[ i ];
delete test;
}
delete pFilterList;
}
if ( pItemList ) {
// delete existing res. of type ItemList
for ( size_t i = 0; i < pItemList->size(); i++ ) {
ExportListEntry* test = (*pItemList)[ i ];
delete test;
}
delete pItemList;
}
if ( pUIEntries ) {
// delete existing res. of type UIEntries
for ( size_t i = 0; i < pUIEntries->size(); i++ ) {
ExportListEntry* test = (*pUIEntries)[ i ];
delete test;
}
delete pUIEntries;
}
}
//
// class Export
//
/*****************************************************************************/
rtl::OString Export::sLanguages;
/*****************************************************************************/
bool Export::handleArguments(
bool handleArguments(
int argc, char * argv[], HandledArgs& o_aHandledArgs)
{
o_aHandledArgs = HandledArgs();
......@@ -156,7 +87,7 @@ bool Export::handleArguments(
}
}
void Export::writeUsage(const OString& rName, const OString& rFileType)
void writeUsage(const OString& rName, const OString& rFileType)
{
std::cout
<< " Syntax: " << rName.getStr()
......@@ -168,7 +99,7 @@ void Export::writeUsage(const OString& rName, const OString& rFileType)
<< " (de, en-US, ...) or all\n";
}
void Export::writePoEntry(
void writePoEntry(
const OString& rExecutable, PoOfstream& rPoStream, const OString& rSourceFile,
const OString& rResType, const OString& rGroupId, const OString& rLocalId,
const OString& rHelpText, const OString& rText, const PoEntry::TYPE eType )
......@@ -217,148 +148,6 @@ void Export::writePoEntry(
}
}
/*****************************************************************************/
void Export::SetLanguages( std::vector<rtl::OString> val ){
/*****************************************************************************/
aLanguages = val;
isInitialized = true;
}
/*****************************************************************************/
std::vector<rtl::OString> Export::GetLanguages(){
/*****************************************************************************/
return aLanguages;
}
std::vector<rtl::OString> Export::aLanguages = std::vector<rtl::OString>();
std::vector<rtl::OString> Export::aForcedLanguages = std::vector<rtl::OString>();
/*****************************************************************************/
rtl::OString Export::QuoteHTML( rtl::OString const &rString )
/*****************************************************************************/
{
rtl::OStringBuffer sReturn;
for ( sal_Int32 i = 0; i < rString.getLength(); i++ ) {
rtl::OString sTemp = rString.copy( i );
if ( sTemp.match( "<Arg n=" ) ) {
while ( i < rString.getLength() && rString[i] != '>' ) {
sReturn.append(rString[i]);
i++;
}
if ( rString[i] == '>' ) {
sReturn.append('>');
i++;
}
}
if ( i < rString.getLength()) {
switch ( rString[i]) {
case '<':
sReturn.append("&lt;");
break;
case '>':
sReturn.append("&gt;");
break;
case '\"':
sReturn.append("&quot;");
break;
case '\'':
sReturn.append("&apos;");
break;
case '&':
if ((( i + 4 ) < rString.getLength()) &&
( rString.copy( i, 5 ) == "&amp;" ))
sReturn.append(rString[i]);
else
sReturn.append("&amp;");
break;
default:
sReturn.append(rString[i]);
break;
}
}
}
return sReturn.makeStringAndClear();
}
void Export::RemoveUTF8ByteOrderMarker( rtl::OString &rString )
{
if( hasUTF8ByteOrderMarker( rString ) )
rString = rString.copy(3);
}
bool Export::hasUTF8ByteOrderMarker( const rtl::OString &rString )
{
return rString.getLength() >= 3 && rString[0] == '\xEF' &&
rString[1] == '\xBB' && rString[2] == '\xBF' ;
}
/*****************************************************************************/
rtl::OString Export::UnquoteHTML( rtl::OString const &rString )
/*****************************************************************************/
{
rtl::OStringBuffer sReturn;
for (sal_Int32 i = 0; i != rString.getLength();) {
if (rString.match("&amp;", i)) {
sReturn.append('&');
i += RTL_CONSTASCII_LENGTH("&amp;");
} else if (rString.match("&lt;", i)) {
sReturn.append('<');
i += RTL_CONSTASCII_LENGTH("&lt;");
} else if (rString.match("&gt;", i)) {
sReturn.append('>');
i += RTL_CONSTASCII_LENGTH("&gt;");
} else if (rString.match("&quot;", i)) {
sReturn.append('"');
i += RTL_CONSTASCII_LENGTH("&quot;");
} else if (rString.match("&apos;", i)) {
sReturn.append('\'');
i += RTL_CONSTASCII_LENGTH("&apos;");
} else {
sReturn.append(rString[i]);
++i;
}
}
return sReturn.makeStringAndClear();
}
bool Export::isSourceLanguage(const rtl::OString &rLanguage)
{
return !isAllowed(rLanguage);
}
bool Export::isAllowed(const rtl::OString &rLanguage)
{
return !rLanguage.equalsIgnoreAsciiCaseL(RTL_CONSTASCII_STRINGPARAM("en-US"));
}
bool Export::isInitialized = false;
/*****************************************************************************/
void Export::InitLanguages( bool bMergeMode ){
/*****************************************************************************/
if( !isInitialized )
{
rtl::OString sTmp;
OStringBoolHashMap aEnvLangs;
sal_Int32 nIndex = 0;
do
{
rtl::OString aToken = sLanguages.getToken(0, ',', nIndex);
sTmp = aToken.getToken(0, '=').trim();
if( bMergeMode && !isAllowed( sTmp ) ){}
else if( !( (sTmp[0]=='x' || sTmp[0]=='X') && sTmp[1]=='-' ) ){
aLanguages.push_back( sTmp );
}
}
while ( nIndex >= 0 );
isInitialized = true;
}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* Version: MPL 1.1 / GPLv3+ / LGPLv3+
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License or as specified alternatively below. You may obtain a copy of
* the License at http://www.mozilla.org/MPL/
*
* Software distributed under