1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  *   Licensed to the Apache Software Foundation (ASF) under one or more
12  *   contributor license agreements. See the NOTICE file distributed
13  *   with this work for additional information regarding copyright
14  *   ownership. The ASF licenses this file to you under the Apache
15  *   License, Version 2.0 (the "License"); you may not use this file
16  *   except in compliance with the License. You may obtain a copy of
17  *   the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #include "XMLIndexBibliographyEntryContext.hxx"
21 #include "XMLIndexTemplateContext.hxx"
22 #include <xmloff/xmlimp.hxx>
23 #include <xmloff/txtimp.hxx>
24 #include <xmloff/namespacemap.hxx>
25 #include <xmloff/xmlnamespace.hxx>
26 #include <xmloff/xmltoken.hxx>
27 #include <xmloff/xmluconv.hxx>
28 #include <xmloff/xmlement.hxx>
29 #include <com/sun/star/text/BibliographyDataField.hpp>
30 #include <sal/log.hxx>
31 
32 
33 using namespace ::com::sun::star::text;
34 using namespace ::xmloff::token;
35 
36 using ::com::sun::star::beans::PropertyValue;
37 using ::com::sun::star::uno::Reference;
38 using ::com::sun::star::uno::Sequence;
39 using ::com::sun::star::xml::sax::XAttributeList;
40 
41 
XMLIndexBibliographyEntryContext(SvXMLImport & rImport,XMLIndexTemplateContext & rTemplate)42 XMLIndexBibliographyEntryContext::XMLIndexBibliographyEntryContext(
43     SvXMLImport& rImport,
44     XMLIndexTemplateContext& rTemplate ) :
45         XMLIndexSimpleEntryContext(rImport,
46                                    "TokenBibliographyDataField",
47                                    rTemplate),
48         nBibliographyInfo(BibliographyDataField::IDENTIFIER),
49         bBibliographyInfoOK(false)
50 {
51 }
52 
~XMLIndexBibliographyEntryContext()53 XMLIndexBibliographyEntryContext::~XMLIndexBibliographyEntryContext()
54 {
55 }
56 
57 const SvXMLEnumMapEntry<sal_uInt16> aBibliographyDataFieldMap[] =
58 {
59     { XML_ADDRESS,              BibliographyDataField::ADDRESS },
60     { XML_ANNOTE,               BibliographyDataField::ANNOTE },
61     { XML_AUTHOR,               BibliographyDataField::AUTHOR },
62     { XML_BIBLIOGRAPHY_TYPE,    BibliographyDataField::BIBILIOGRAPHIC_TYPE },
63     // #96658#: also read old documents (bib*i*liographic...)
64     { XML_BIBILIOGRAPHIC_TYPE,  BibliographyDataField::BIBILIOGRAPHIC_TYPE },
65     { XML_BOOKTITLE,            BibliographyDataField::BOOKTITLE },
66     { XML_CHAPTER,              BibliographyDataField::CHAPTER },
67     { XML_CUSTOM1,              BibliographyDataField::CUSTOM1 },
68     { XML_CUSTOM2,              BibliographyDataField::CUSTOM2 },
69     { XML_CUSTOM3,              BibliographyDataField::CUSTOM3 },
70     { XML_CUSTOM4,              BibliographyDataField::CUSTOM4 },
71     { XML_CUSTOM5,              BibliographyDataField::CUSTOM5 },
72     { XML_EDITION,              BibliographyDataField::EDITION },
73     { XML_EDITOR,               BibliographyDataField::EDITOR },
74     { XML_HOWPUBLISHED,         BibliographyDataField::HOWPUBLISHED },
75     { XML_IDENTIFIER,           BibliographyDataField::IDENTIFIER },
76     { XML_INSTITUTION,          BibliographyDataField::INSTITUTION },
77     { XML_ISBN,                 BibliographyDataField::ISBN },
78     { XML_JOURNAL,              BibliographyDataField::JOURNAL },
79     { XML_MONTH,                BibliographyDataField::MONTH },
80     { XML_NOTE,                 BibliographyDataField::NOTE },
81     { XML_NUMBER,               BibliographyDataField::NUMBER },
82     { XML_ORGANIZATIONS,        BibliographyDataField::ORGANIZATIONS },
83     { XML_PAGES,                BibliographyDataField::PAGES },
84     { XML_PUBLISHER,            BibliographyDataField::PUBLISHER },
85     { XML_REPORT_TYPE,          BibliographyDataField::REPORT_TYPE },
86     { XML_SCHOOL,               BibliographyDataField::SCHOOL },
87     { XML_SERIES,               BibliographyDataField::SERIES },
88     { XML_TITLE,                BibliographyDataField::TITLE },
89     { XML_URL,                  BibliographyDataField::URL },
90     { XML_VOLUME,               BibliographyDataField::VOLUME },
91     { XML_YEAR,                 BibliographyDataField::YEAR },
92     { XML_TOKEN_INVALID, 0 }
93 };
94 
startFastElement(sal_Int32,const css::uno::Reference<css::xml::sax::XFastAttributeList> & xAttrList)95 void XMLIndexBibliographyEntryContext::startFastElement(
96     sal_Int32 /*nElement*/,
97     const css::uno::Reference< css::xml::sax::XFastAttributeList >& xAttrList )
98 {
99     // handle both, style name and bibliography info
100     for( auto& aIter : sax_fastparser::castToFastAttributeList(xAttrList) )
101     {
102         switch(aIter.getToken())
103         {
104             case XML_ELEMENT(TEXT, XML_STYLE_NAME):
105             {
106                 m_sCharStyleName = aIter.toString();
107                 m_bCharStyleNameOK = true;
108                 break;
109             }
110             case XML_ELEMENT(TEXT, XML_BIBLIOGRAPHY_DATA_FIELD):
111             {
112                 sal_uInt16 nTmp;
113                 if (SvXMLUnitConverter::convertEnum(nTmp, aIter.toView(), aBibliographyDataFieldMap))
114                 {
115                     nBibliographyInfo = nTmp;
116                     bBibliographyInfoOK = true;
117                 }
118                 break;
119             }
120             default:
121                 XMLOFF_WARN_UNKNOWN("xmloff", aIter);
122         }
123     }
124 
125     // if we have a style name, set it!
126     if (m_bCharStyleNameOK)
127     {
128         m_nValues++;
129     }
130 
131     // always bibliography; else element is not valid
132     m_nValues++;
133 }
134 
endFastElement(sal_Int32 nElement)135 void XMLIndexBibliographyEntryContext::endFastElement(sal_Int32 nElement)
136 {
137     // only valid, if we have bibliography info
138     if (bBibliographyInfoOK)
139     {
140         XMLIndexSimpleEntryContext::endFastElement(nElement);
141     }
142 }
143 
FillPropertyValues(css::uno::Sequence<css::beans::PropertyValue> & rValues)144 void XMLIndexBibliographyEntryContext::FillPropertyValues(
145     css::uno::Sequence<css::beans::PropertyValue> & rValues)
146 {
147     // entry name and (optionally) style name in parent class
148     XMLIndexSimpleEntryContext::FillPropertyValues(rValues);
149 
150     // bibliography data field
151     sal_Int32 nIndex = m_bCharStyleNameOK ? 2 : 1;
152     rValues[nIndex].Name = "BibliographyDataField";
153     rValues[nIndex].Value <<= nBibliographyInfo;
154 }
155 
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
157