xref: /aoo41x/main/sw/source/filter/xml/xmlmeta.cxx (revision efeef26f)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_sw.hxx"
26 
27 
28 #include <hintids.hxx>
29 #include <com/sun/star/frame/XModel.hpp>
30 #include <com/sun/star/document/XDocumentPropertiesSupplier.hpp>
31 #include <com/sun/star/text/XTextDocument.hpp>
32 #include <xmloff/xmlnmspe.hxx>
33 #include <xmloff/xmlmetai.hxx>
34 #include <xmloff/xmlmetae.hxx>
35 #include <editeng/langitem.hxx>
36 #include <xmloff/xmluconv.hxx>
37 #include <xmloff/nmspmap.hxx>
38 #include "docstat.hxx"
39 #include "docsh.hxx"
40 #include <doc.hxx>
41 #include "xmlimp.hxx"
42 #include "xmlexp.hxx"
43 
44 
45 using ::rtl::OUString;
46 using ::rtl::OUStringBuffer;
47 
48 using namespace ::com::sun::star;
49 using namespace ::com::sun::star::uno;
50 using namespace ::com::sun::star::lang;
51 using namespace ::com::sun::star::text;
52 using namespace ::xmloff::token;
53 
54 // ---------------------------------------------------------------------
55 
56 uno::Reference<document::XDocumentProperties>
GetDocumentProperties() const57 SwXMLImport::GetDocumentProperties() const
58 {
59     if (IsOrganizerMode() || IsStylesOnlyMode() ||
60         IsBlockMode() || IsInsertMode())
61     {
62         return 0;
63     }
64     uno::Reference<document::XDocumentPropertiesSupplier> const xDPS(
65         GetModel(), UNO_QUERY_THROW);
66     return xDPS->getDocumentProperties();
67 }
68 
CreateMetaContext(const OUString & rLocalName)69 SvXMLImportContext *SwXMLImport::CreateMetaContext(
70 									   const OUString& rLocalName )
71 {
72 	SvXMLImportContext *pContext = 0;
73 
74     if (getImportFlags() & IMPORT_META)
75     {
76         uno::Reference<xml::sax::XDocumentHandler> const xDocBuilder(
77             mxServiceFactory->createInstance(::rtl::OUString::createFromAscii(
78                 "com.sun.star.xml.dom.SAXDocumentBuilder")),
79             uno::UNO_QUERY_THROW);
80         uno::Reference<document::XDocumentProperties> const xDocProps(
81                 GetDocumentProperties());
82         pContext = new SvXMLMetaDocumentContext(*this,
83                     XML_NAMESPACE_OFFICE, rLocalName, xDocProps, xDocBuilder);
84     }
85 
86 	if( !pContext )
87 		pContext = new SvXMLImportContext( *this,
88                         XML_NAMESPACE_OFFICE, rLocalName );
89 
90 	return pContext;
91 }
92 
93 // ---------------------------------------------------------------------
94 
95 enum SvXMLTokenMapAttrs
96 {
97 	XML_TOK_META_STAT_TABLE = 1,
98 	XML_TOK_META_STAT_IMAGE = 2,
99 	XML_TOK_META_STAT_OLE = 4,
100 	XML_TOK_META_STAT_PAGE = 8,
101 	XML_TOK_META_STAT_PARA = 16,
102 	XML_TOK_META_STAT_WORD = 32,
103 	XML_TOK_META_STAT_CHAR = 64,
104 	XML_TOK_META_STAT_END=XML_TOK_UNKNOWN
105 };
106 
107 /*
108 static __FAR_DATA SvXMLTokenMapEntry aMetaStatAttrTokenMap[] =
109 {
110 	{ XML_NAMESPACE_META, XML_TABLE_COUNT,      XML_TOK_META_STAT_TABLE	},
111 	{ XML_NAMESPACE_META, XML_IMAGE_COUNT,      XML_TOK_META_STAT_IMAGE	},
112 	{ XML_NAMESPACE_META, XML_OBJECT_COUNT,     XML_TOK_META_STAT_OLE	},
113 	{ XML_NAMESPACE_META, XML_PARAGRAPH_COUNT,  XML_TOK_META_STAT_PARA	},
114 	{ XML_NAMESPACE_META, XML_PAGE_COUNT,       XML_TOK_META_STAT_PAGE	},
115 	{ XML_NAMESPACE_META, XML_WORD_COUNT,       XML_TOK_META_STAT_WORD	},
116 	{ XML_NAMESPACE_META, XML_CHARACTER_COUNT,  XML_TOK_META_STAT_CHAR	},
117 	XML_TOKEN_MAP_END
118 };
119 */
120 
121 struct statistic {
122     SvXMLTokenMapAttrs token;
123     const char* name;
124     sal_uInt16 SwDocStat::* target16;
125     sal_uLong  SwDocStat::* target32; /* or 64, on LP64 platforms */
126 };
127 
128 static const struct statistic s_stats [] = {
129     { XML_TOK_META_STAT_TABLE, "TableCount",     &SwDocStat::nTbl, 0  },
130     { XML_TOK_META_STAT_IMAGE, "ImageCount",     &SwDocStat::nGrf, 0  },
131     { XML_TOK_META_STAT_OLE,   "ObjectCount",    &SwDocStat::nOLE, 0  },
132     { XML_TOK_META_STAT_PAGE,  "PageCount",      0, &SwDocStat::nPage },
133     { XML_TOK_META_STAT_PARA,  "ParagraphCount", 0, &SwDocStat::nPara },
134     { XML_TOK_META_STAT_WORD,  "WordCount",      0, &SwDocStat::nWord },
135     { XML_TOK_META_STAT_CHAR,  "CharacterCount", 0, &SwDocStat::nChar },
136     { XML_TOK_META_STAT_END,   0,                0, 0                 }
137 };
138 
SetStatistics(const Sequence<beans::NamedValue> & i_rStats)139 void SwXMLImport::SetStatistics(
140         const Sequence< beans::NamedValue > & i_rStats)
141 {
142 	if( IsStylesOnlyMode() || IsInsertMode() )
143 		return;
144 
145     SvXMLImport::SetStatistics(i_rStats);
146 
147 	SwDoc *pDoc = SwImport::GetDocFromXMLImport( *this );
148 	SwDocStat aDocStat( pDoc->GetDocStat() );
149 
150 	sal_uInt32 nTokens = 0;
151 
152     for (sal_Int32 i = 0; i < i_rStats.getLength(); ++i) {
153         for (struct statistic const* pStat = s_stats; pStat->name != 0;
154                 ++pStat) {
155             if (i_rStats[i].Name.equalsAscii(pStat->name)) {
156                 sal_Int32 val = 0;
157                 if (i_rStats[i].Value >>= val) {
158                     if (pStat->target16 != 0) {
159                         aDocStat.*(pStat->target16)
160                             = static_cast<sal_uInt16> (val);
161                     } else {
162                         aDocStat.*(pStat->target32)
163                             = static_cast<sal_uInt32> (val);
164                     }
165                     nTokens |= pStat->token;
166                 } else {
167                     DBG_ERROR("SwXMLImport::SetStatistics: invalid entry");
168                 }
169             }
170         }
171     }
172 
173 	if( 127 == nTokens )
174 		aDocStat.bModified = sal_False;
175 	if( nTokens )
176 		pDoc->SetDocStat( aDocStat );
177 
178 	// set progress bar reference to #paragraphs. If not available,
179 	// use #pages*10, or guesstimate 250 paragraphs. Additionally
180 	// guesstimate PROGRESS_BAR_STEPS each for meta+settings, styles,
181 	// and autostyles.
182 	sal_Int32 nProgressReference = 250;
183 	if( nTokens & XML_TOK_META_STAT_PARA )
184 		nProgressReference = (sal_Int32)aDocStat.nPara;
185 	else if ( nTokens & XML_TOK_META_STAT_PAGE )
186 		nProgressReference = 10 * (sal_Int32)aDocStat.nPage;
187 	ProgressBarHelper* pProgress = GetProgressBarHelper();
188 	pProgress->SetReference( nProgressReference + 3*PROGRESS_BAR_STEP );
189 	pProgress->SetValue( 0 );
190 }
191 
192 // ---------------------------------------------------------------------
193 
_ExportMeta()194 void SwXMLExport::_ExportMeta()
195 {
196 	SvXMLExport::_ExportMeta();
197 
198 	if( !IsBlockMode() )
199 	{
200 
201 		if( IsShowProgress() )
202 		{
203 			ProgressBarHelper *pProgress = GetProgressBarHelper();
204 			pProgress->SetValue( pProgress->GetValue() + 2 );
205 		}
206 	}
207 }
208 
209