2000-09-18 16:07:07 +00:00
/*************************************************************************
*
2008-04-10 22:23:18 +00:00
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER .
2000-09-18 16:07:07 +00:00
*
2008-04-10 22:23:18 +00:00
* Copyright 2008 by Sun Microsystems , Inc .
2000-09-18 16:07:07 +00:00
*
2008-04-10 22:23:18 +00:00
* OpenOffice . org - a multi - platform office productivity suite
2000-09-18 16:07:07 +00:00
*
2008-04-10 22:23:18 +00:00
* $ RCSfile : XMLTextMarkImportContext . cxx , v $
2008-09-04 09:08:48 +00:00
* $ Revision : 1.15 $
2000-09-18 16:07:07 +00:00
*
2008-04-10 22:23:18 +00:00
* This file is part of OpenOffice . org .
2000-09-18 16:07:07 +00:00
*
2008-04-10 22:23:18 +00:00
* OpenOffice . org is free software : you can redistribute it and / or modify
* it under the terms of the GNU Lesser General Public License version 3
* only , as published by the Free Software Foundation .
2000-09-18 16:07:07 +00:00
*
2008-04-10 22:23:18 +00:00
* OpenOffice . org is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU Lesser General Public License version 3 for more details
* ( a copy is included in the LICENSE file that accompanied this code ) .
2000-09-18 16:07:07 +00:00
*
2008-04-10 22:23:18 +00:00
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with OpenOffice . org . If not , see
* < http : //www.openoffice.org/license.html>
* for a copy of the LGPLv3 License .
2000-09-18 16:07:07 +00:00
*
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2006-09-17 10:14:21 +00:00
// MARKER(update_precomp.py): autogen include statement, do not remove
# include "precompiled_xmloff.hxx"
2000-09-18 16:07:07 +00:00
# include "XMLTextMarkImportContext.hxx"
2006-06-19 17:46:39 +00:00
# include <rtl/ustring.hxx>
2000-09-18 16:07:07 +00:00
# include <tools/debug.hxx>
2007-06-27 15:10:36 +00:00
# include <xmloff/xmluconv.hxx>
# include <xmloff/xmltoken.hxx>
# include <xmloff/xmlimp.hxx>
# include <xmloff/nmspmap.hxx>
2000-09-18 16:07:07 +00:00
# include "xmlnmspe.hxx"
# include <com/sun/star/xml/sax/XAttributeList.hpp>
# include <com/sun/star/text/XTextContent.hpp>
# include <com/sun/star/beans/XPropertySet.hpp>
# include <com/sun/star/lang/XMultiServiceFactory.hpp>
# include <com/sun/star/container/XNamed.hpp>
2008-09-04 09:08:48 +00:00
# include <com/sun/star/text/XFormField.hpp>
2000-09-18 16:07:07 +00:00
2008-03-12 10:03:57 +00:00
using : : rtl : : OUString ;
using : : rtl : : OUStringBuffer ;
2000-09-18 16:07:07 +00:00
using namespace : : com : : sun : : star : : text ;
using namespace : : com : : sun : : star : : uno ;
using namespace : : com : : sun : : star : : beans ;
using namespace : : com : : sun : : star : : lang ;
using namespace : : com : : sun : : star : : container ;
using namespace : : com : : sun : : star : : xml : : sax ;
2001-06-15 09:37:08 +00:00
using namespace : : xmloff : : token ;
2000-09-18 16:07:07 +00:00
2008-09-04 09:08:48 +00:00
XMLFieldParamImportContext : : XMLFieldParamImportContext (
SvXMLImport & rImport ,
XMLTextImportHelper & rHlp ,
sal_uInt16 nPrefix ,
const OUString & rLocalName ) :
SvXMLImportContext ( rImport , nPrefix , rLocalName ) ,
rHelper ( rHlp )
{
}
void XMLFieldParamImportContext : : StartElement ( const : : com : : sun : : star : : uno : : Reference < : : com : : sun : : star : : xml : : sax : : XAttributeList > & xAttrList )
{
SvXMLImport & rImport = GetImport ( ) ;
: : rtl : : OUString sName ;
: : rtl : : OUString sValue ;
sal_Int16 nLength = xAttrList - > getLength ( ) ;
for ( sal_Int16 nAttr = 0 ; nAttr < nLength ; nAttr + + )
{
OUString sLocalName ;
sal_uInt16 nPrefix = rImport . GetNamespaceMap ( ) .
GetKeyByAttrName ( xAttrList - > getNameByIndex ( nAttr ) ,
& sLocalName ) ;
if ( ( XML_NAMESPACE_FIELD = = nPrefix ) & &
IsXMLToken ( sLocalName , XML_NAME ) )
{
sName = xAttrList - > getValueByIndex ( nAttr ) ;
}
if ( ( XML_NAMESPACE_FIELD = = nPrefix ) & &
IsXMLToken ( sLocalName , XML_VALUE ) )
{
sValue = xAttrList - > getValueByIndex ( nAttr ) ;
}
}
if ( rHelper . hasCurrentFieldCtx ( ) & & sName . getLength ( ) > 0 ) {
rHelper . addFieldParam ( sName , sValue ) ;
}
}
2000-09-18 16:07:07 +00:00
TYPEINIT1 ( XMLTextMarkImportContext , SvXMLImportContext ) ;
XMLTextMarkImportContext : : XMLTextMarkImportContext (
SvXMLImport & rImport ,
XMLTextImportHelper & rHlp ,
sal_uInt16 nPrefix ,
const OUString & rLocalName ) :
SvXMLImportContext ( rImport , nPrefix , rLocalName ) ,
rHelper ( rHlp )
{
}
enum lcl_MarkType { TypeReference , TypeReferenceStart , TypeReferenceEnd ,
2008-09-04 09:08:48 +00:00
TypeBookmark , TypeBookmarkStart , TypeBookmarkEnd ,
TypeFieldmark , TypeFieldmarkStart , TypeFieldmarkEnd
} ;
2000-09-18 16:07:07 +00:00
static SvXMLEnumMapEntry __READONLY_DATA lcl_aMarkTypeMap [ ] =
{
2001-06-15 09:37:08 +00:00
{ XML_REFERENCE_MARK , TypeReference } ,
{ XML_REFERENCE_MARK_START , TypeReferenceStart } ,
{ XML_REFERENCE_MARK_END , TypeReferenceEnd } ,
{ XML_BOOKMARK , TypeBookmark } ,
{ XML_BOOKMARK_START , TypeBookmarkStart } ,
{ XML_BOOKMARK_END , TypeBookmarkEnd } ,
2008-09-04 09:08:48 +00:00
{ XML_FIELDMARK , TypeFieldmark } ,
{ XML_FIELDMARK_START , TypeFieldmarkStart } ,
{ XML_FIELDMARK_END , TypeFieldmarkEnd } ,
2001-06-15 09:37:08 +00:00
{ XML_TOKEN_INVALID , 0 } ,
2000-09-18 16:07:07 +00:00
} ;
void XMLTextMarkImportContext : : StartElement (
const Reference < XAttributeList > & xAttrList )
{
2008-09-04 09:08:48 +00:00
if ( ! FindName ( GetImport ( ) , xAttrList , sBookmarkName , m_XmlId , & sFieldName ) )
sBookmarkName = OUString ( ) ;
if ( IsXMLToken ( GetLocalName ( ) , XML_FIELDMARK_END ) )
sBookmarkName = rHelper . FindActiveBookmarkName ( ) ;
if ( IsXMLToken ( GetLocalName ( ) , XML_FIELDMARK_START ) | | IsXMLToken ( GetLocalName ( ) , XML_FIELDMARK ) )
{
if ( sBookmarkName . getLength ( ) = = 0 )
sBookmarkName = : : rtl : : OUString : : createFromAscii ( " Unknown " ) ;
rHelper . pushFieldCtx ( sBookmarkName , sFieldName ) ;
}
}
void XMLTextMarkImportContext : : EndElement ( )
{
SvXMLImportContext : : EndElement ( ) ;
2000-09-18 16:07:07 +00:00
const OUString sAPI_reference_mark (
RTL_CONSTASCII_USTRINGPARAM ( " com.sun.star.text.ReferenceMark " ) ) ;
const OUString sAPI_bookmark (
RTL_CONSTASCII_USTRINGPARAM ( " com.sun.star.text.Bookmark " ) ) ;
2008-09-04 09:08:48 +00:00
const OUString sAPI_fieldmark (
RTL_CONSTASCII_USTRINGPARAM ( " com.sun.star.text.Fieldmark " ) ) ;
const OUString sAPI_formfieldmark (
RTL_CONSTASCII_USTRINGPARAM ( " com.sun.star.text.FormFieldmark " ) ) ;
2000-09-18 16:07:07 +00:00
2008-09-04 09:08:48 +00:00
if ( sBookmarkName . getLength ( ) > 0 )
2000-09-18 16:07:07 +00:00
{
sal_uInt16 nTmp ;
if ( SvXMLUnitConverter : : convertEnum ( nTmp , GetLocalName ( ) ,
lcl_aMarkTypeMap ) )
{
switch ( ( lcl_MarkType ) nTmp )
{
case TypeReference :
// export point reference mark
CreateAndInsertMark ( GetImport ( ) ,
sAPI_reference_mark ,
2008-09-04 09:08:48 +00:00
sBookmarkName ,
2008-07-01 15:07:08 +00:00
rHelper . GetCursorAsRange ( ) - > getStart ( ) ,
: : rtl : : OUString ( ) ) ;
2000-09-18 16:07:07 +00:00
break ;
2008-09-04 09:08:48 +00:00
case TypeFieldmark :
2000-09-18 16:07:07 +00:00
case TypeBookmark :
2008-09-04 09:08:48 +00:00
{
bool bImportAsField = ( ( lcl_MarkType ) nTmp = = TypeFieldmark & & sFieldName . compareToAscii ( " msoffice.field.FORMCHECKBOX " ) = = 0 ) ; // for now only import FORMCHECKBOX boxes
// export point bookmark
Reference < XInterface > xIfc = CreateAndInsertMark ( GetImport ( ) ,
( bImportAsField ? sAPI_formfieldmark : sAPI_bookmark ) ,
sBookmarkName ,
2008-07-01 15:07:08 +00:00
rHelper . GetCursorAsRange ( ) - > getStart ( ) ,
2008-09-04 09:08:48 +00:00
m_XmlId ) ;
if ( ( lcl_MarkType ) nTmp = = TypeFieldmark ) {
if ( xIfc . is ( ) & & bImportAsField ) {
// setup fieldmark...
Reference < : : com : : sun : : star : : text : : XFormField > xFormField ( xIfc , UNO_QUERY ) ;
xFormField - > setType ( 1 ) ; // Checkbox...
if ( xFormField . is ( ) & & rHelper . hasCurrentFieldCtx ( ) ) {
// xFormField->setDescription(::rtl::OUString::createFromAscii("HELLO CHECKBOX"));
// xFormField->setRes(1);
rHelper . setCurrentFieldParamsTo ( xFormField ) ;
}
}
rHelper . popFieldCtx ( ) ;
}
}
2000-09-18 16:07:07 +00:00
break ;
2008-09-04 09:08:48 +00:00
case TypeFieldmarkStart :
2000-09-18 16:07:07 +00:00
case TypeBookmarkStart :
// save XTextRange for later construction of bookmark
rHelper . InsertBookmarkStartRange (
2008-09-04 09:08:48 +00:00
sBookmarkName , rHelper . GetCursorAsRange ( ) - > getStart ( ) ,
m_XmlId ) ;
2000-09-18 16:07:07 +00:00
break ;
2008-09-04 09:08:48 +00:00
case TypeFieldmarkEnd :
2000-09-18 16:07:07 +00:00
case TypeBookmarkEnd :
{
// get old range, and construct
Reference < XTextRange > xStartRange ;
2008-09-04 09:08:48 +00:00
if ( rHelper . FindAndRemoveBookmarkStartRange ( sBookmarkName ,
xStartRange , m_XmlId ) )
2000-09-18 16:07:07 +00:00
{
Reference < XTextRange > xEndRange (
2004-11-02 13:38:46 +00:00
rHelper . GetCursorAsRange ( ) - > getStart ( ) ) ;
2000-09-18 16:07:07 +00:00
// check if beginning and end are in same XText
if ( xStartRange - > getText ( ) = = xEndRange - > getText ( ) )
{
// create range for insertion
Reference < XTextCursor > xInsertionCursor =
rHelper . GetText ( ) - > createTextCursorByRange (
xEndRange ) ;
xInsertionCursor - > gotoRange ( xStartRange , sal_True ) ;
//DBG_ASSERT(! xInsertionCursor->isCollapsed(),
// "we want no point mark");
// can't assert, because someone could
// create a file with subsequence
// start/end elements
Reference < XTextRange > xInsertionRange (
xInsertionCursor , UNO_QUERY ) ;
2008-09-04 09:08:48 +00:00
bool bImportAsField = ( ( lcl_MarkType ) nTmp = = TypeFieldmarkEnd & & rHelper . hasCurrentFieldCtx ( ) ) ;
if ( bImportAsField ) {
: : rtl : : OUString currentFieldType = rHelper . getCurrentFieldType ( ) ;
bImportAsField = currentFieldType . compareToAscii ( " msoffice.field.FORMTEXT " ) = = 0 ; // for now only import FORMTEXT boxes
}
2000-09-18 16:07:07 +00:00
// insert reference
2008-09-04 09:08:48 +00:00
Reference < XInterface > xIfc = CreateAndInsertMark ( GetImport ( ) ,
( bImportAsField ? sAPI_fieldmark : sAPI_bookmark ) ,
sBookmarkName ,
2008-07-01 15:07:08 +00:00
xInsertionRange ,
2008-09-04 09:08:48 +00:00
m_XmlId ) ;
if ( ( lcl_MarkType ) nTmp = = TypeFieldmarkEnd ) {
if ( xIfc . is ( ) & & bImportAsField ) {
// setup fieldmark...
Reference < : : com : : sun : : star : : text : : XFormField > xFormField ( xIfc , UNO_QUERY ) ;
xFormField - > setType ( 0 ) ; // Text
if ( xFormField . is ( ) & & rHelper . hasCurrentFieldCtx ( ) ) {
rHelper . setCurrentFieldParamsTo ( xFormField ) ;
// xFormField->setDescription(::rtl::OUString::createFromAscii("HELLO"));
}
}
rHelper . popFieldCtx ( ) ;
}
2000-09-18 16:07:07 +00:00
}
// else: beginning/end in different XText -> ignore!
}
// else: no start found -> ignore!
break ;
}
case TypeReferenceStart :
case TypeReferenceEnd :
DBG_ERROR ( " reference start/end are handled in txtparai ! " ) ;
break ;
default :
DBG_ERROR ( " unknown mark type " ) ;
break ;
}
}
}
}
2008-09-04 09:08:48 +00:00
SvXMLImportContext * XMLTextMarkImportContext : : CreateChildContext ( USHORT nPrefix ,
const : : rtl : : OUString & rLocalName ,
const : : com : : sun : : star : : uno : : Reference < : : com : : sun : : star : : xml : : sax : : XAttributeList > & )
{
return new XMLFieldParamImportContext ( GetImport ( ) , rHelper , nPrefix , rLocalName ) ;
}
2000-09-18 16:07:07 +00:00
2008-09-04 09:08:48 +00:00
Reference < XInterface > XMLTextMarkImportContext : : CreateAndInsertMark (
2000-09-18 16:07:07 +00:00
SvXMLImport & rImport ,
const OUString & sServiceName ,
const OUString & sMarkName ,
2008-07-01 15:07:08 +00:00
const Reference < XTextRange > & rRange ,
const OUString & i_rXmlId )
2000-09-18 16:07:07 +00:00
{
// create mark
Reference < XMultiServiceFactory > xFactory ( rImport . GetModel ( ) , UNO_QUERY ) ;
if ( xFactory . is ( ) )
{
Reference < XInterface > xIfc = xFactory - > createInstance ( sServiceName ) ;
// set name
Reference < XNamed > xNamed ( xIfc , UNO_QUERY ) ;
if ( xNamed . is ( ) )
{
xNamed - > setName ( sMarkName ) ;
2008-07-01 15:07:08 +00:00
// xml:id for RDF metadata
rImport . SetXmlId ( xIfc , i_rXmlId ) ;
2000-09-18 16:07:07 +00:00
// cast to XTextContent and attach to document
Reference < XTextContent > xTextContent ( xIfc , UNO_QUERY ) ;
if ( xTextContent . is ( ) )
{
2007-07-31 16:35:27 +00:00
try
{
2000-09-18 16:07:07 +00:00
// if inserting marks, bAbsorb==sal_False will cause
// collapsing of the given XTextRange.
2007-07-31 16:35:27 +00:00
rImport . GetTextImport ( ) - > GetText ( ) - > insertTextContent ( rRange ,
xTextContent , sal_True ) ;
}
catch ( com : : sun : : star : : lang : : IllegalArgumentException e )
{
// ignore
}
2000-09-18 16:07:07 +00:00
}
}
2008-09-04 09:08:48 +00:00
return xIfc ;
} else return NULL ;
2000-09-18 16:07:07 +00:00
}
sal_Bool XMLTextMarkImportContext : : FindName (
SvXMLImport & rImport ,
const Reference < XAttributeList > & xAttrList ,
2008-07-01 15:07:08 +00:00
OUString & sName ,
2008-09-04 09:08:48 +00:00
OUString & o_rXmlId ,
: : rtl : : OUString * pFieldName )
2000-09-18 16:07:07 +00:00
{
2006-06-19 17:46:39 +00:00
sal_Bool bNameOK = sal_False ;
2000-09-18 16:07:07 +00:00
// find name attribute first
2001-01-02 13:41:38 +00:00
sal_Int16 nLength = xAttrList - > getLength ( ) ;
for ( sal_Int16 nAttr = 0 ; nAttr < nLength ; nAttr + + )
2000-09-18 16:07:07 +00:00
{
OUString sLocalName ;
sal_uInt16 nPrefix = rImport . GetNamespaceMap ( ) .
GetKeyByAttrName ( xAttrList - > getNameByIndex ( nAttr ) ,
& sLocalName ) ;
if ( ( XML_NAMESPACE_TEXT = = nPrefix ) & &
2001-06-29 20:07:26 +00:00
IsXMLToken ( sLocalName , XML_NAME ) )
2000-09-18 16:07:07 +00:00
{
sName = xAttrList - > getValueByIndex ( nAttr ) ;
bNameOK = sal_True ;
}
2008-07-01 15:07:08 +00:00
else if ( ( XML_NAMESPACE_XML = = nPrefix ) & &
IsXMLToken ( sLocalName , XML_ID ) )
{
o_rXmlId = xAttrList - > getValueByIndex ( nAttr ) ;
}
//FIXME: RDFa (text:bookmark-start)
2008-09-04 09:08:48 +00:00
else if ( pFieldName ! = NULL & &
( XML_NAMESPACE_FIELD = = nPrefix ) & &
IsXMLToken ( sLocalName , XML_TYPE ) )
{
* pFieldName = xAttrList - > getValueByIndex ( nAttr ) ;
}
2000-09-18 16:07:07 +00:00
}
return bNameOK ;
}
2008-09-04 09:08:48 +00:00