xref: /trunk/main/xmloff/source/text/XMLSectionSourceImportContext.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_xmloff.hxx"
30 #include "XMLSectionSourceImportContext.hxx"
31 #include "XMLSectionImportContext.hxx"
32 #include <com/sun/star/text/SectionFileLink.hpp>
33 #include <xmloff/xmlictxt.hxx>
34 #include <xmloff/xmlimp.hxx>
35 #include <xmloff/txtimp.hxx>
36 #include <xmloff/nmspmap.hxx>
37 #include "xmloff/xmlnmspe.hxx"
38 #include <xmloff/xmltoken.hxx>
39 #include <com/sun/star/uno/Reference.h>
40 #include <com/sun/star/beans/XPropertySet.hpp>
41 
42 
43 using ::rtl::OUString;
44 using ::com::sun::star::beans::XPropertySet;
45 using ::com::sun::star::uno::Reference;
46 using ::com::sun::star::xml::sax::XAttributeList;
47 
48 using namespace ::com::sun::star::uno;
49 using namespace ::com::sun::star::text;
50 using namespace ::xmloff::token;
51 
52 
53 TYPEINIT1(XMLSectionSourceImportContext, SvXMLImportContext);
54 
55 XMLSectionSourceImportContext::XMLSectionSourceImportContext(
56     SvXMLImport& rImport,
57     sal_uInt16 nPrfx,
58     const OUString& rLocalName,
59     Reference<XPropertySet> & rSectPropSet) :
60         SvXMLImportContext(rImport, nPrfx, rLocalName),
61         rSectionPropertySet(rSectPropSet)
62 {
63 }
64 
65 XMLSectionSourceImportContext::~XMLSectionSourceImportContext()
66 {
67 }
68 
69 enum XMLSectionSourceToken
70 {
71     XML_TOK_SECTION_XLINK_HREF,
72     XML_TOK_SECTION_TEXT_FILTER_NAME,
73     XML_TOK_SECTION_TEXT_SECTION_NAME
74 };
75 
76 static __FAR_DATA SvXMLTokenMapEntry aSectionSourceTokenMap[] =
77 {
78     { XML_NAMESPACE_XLINK, XML_HREF, XML_TOK_SECTION_XLINK_HREF },
79     { XML_NAMESPACE_TEXT, XML_FILTER_NAME, XML_TOK_SECTION_TEXT_FILTER_NAME },
80     { XML_NAMESPACE_TEXT, XML_SECTION_NAME,
81                                         XML_TOK_SECTION_TEXT_SECTION_NAME },
82     XML_TOKEN_MAP_END
83 };
84 
85 
86 void XMLSectionSourceImportContext::StartElement(
87     const Reference<XAttributeList> & xAttrList)
88 {
89     SvXMLTokenMap aTokenMap(aSectionSourceTokenMap);
90     OUString sURL;
91     OUString sFilterName;
92     OUString sSectionName;
93 
94     sal_Int16 nLength = xAttrList->getLength();
95     for(sal_Int16 nAttr = 0; nAttr < nLength; nAttr++)
96     {
97         OUString sLocalName;
98         sal_uInt16 nPrefix = GetImport().GetNamespaceMap().
99             GetKeyByAttrName( xAttrList->getNameByIndex(nAttr),
100                               &sLocalName );
101 
102         switch (aTokenMap.Get(nPrefix, sLocalName))
103         {
104             case XML_TOK_SECTION_XLINK_HREF:
105                 sURL = xAttrList->getValueByIndex(nAttr);
106                 break;
107 
108             case XML_TOK_SECTION_TEXT_FILTER_NAME:
109                 sFilterName = xAttrList->getValueByIndex(nAttr);
110                 break;
111 
112             case XML_TOK_SECTION_TEXT_SECTION_NAME:
113                 sSectionName = xAttrList->getValueByIndex(nAttr);
114                 break;
115 
116             default:
117                 ; // ignore
118                 break;
119         }
120     }
121 
122     // we only need them once
123     const OUString sFileLink(RTL_CONSTASCII_USTRINGPARAM("FileLink"));
124     const OUString sLinkRegion(RTL_CONSTASCII_USTRINGPARAM("LinkRegion"));
125 
126     Any aAny;
127     if ((sURL.getLength() > 0) || (sFilterName.getLength() > 0))
128     {
129         SectionFileLink aFileLink;
130         aFileLink.FileURL = GetImport().GetAbsoluteReference( sURL );
131         aFileLink.FilterName = sFilterName;
132 
133         aAny <<= aFileLink;
134         rSectionPropertySet->setPropertyValue(sFileLink, aAny);
135     }
136 
137     if (sSectionName.getLength() > 0)
138     {
139         aAny <<= sSectionName;
140         rSectionPropertySet->setPropertyValue(sLinkRegion, aAny);
141     }
142 }
143 
144 void XMLSectionSourceImportContext::EndElement()
145 {
146     // this space intentionally left blank.
147 }
148 
149 SvXMLImportContext* XMLSectionSourceImportContext::CreateChildContext(
150     sal_uInt16 nPrefix,
151     const OUString& rLocalName,
152     const Reference<XAttributeList> & )
153 {
154     // ignore -> default context
155     return new SvXMLImportContext(GetImport(), nPrefix, rLocalName);
156 }
157