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 "XMLSymbolImageContext.hxx"
31 #include <xmloff/xmltoken.hxx>
32 #include <xmloff/xmltkmap.hxx>
33 #include "xmloff/xmlnmspe.hxx"
34 #include <xmloff/xmlimp.hxx>
35 #include <xmloff/nmspmap.hxx>
36 #include <xmloff/XMLBase64ImportContext.hxx>
37 #include <com/sun/star/io/XOutputStream.hpp>
38 
39 TYPEINIT1( XMLSymbolImageContext, XMLElementPropertyContext );
40 
41 using namespace ::com::sun::star;
42 
43 enum SvXMLTokenMapAttrs
44 {
45 	XML_TOK_SYMBOL_IMAGE_HREF,
46 	XML_TOK_SYMBOL_IMAGE_TYPE,
47 	XML_TOK_SYMBOL_IMAGE_ACTUATE,
48     XML_TOK_SYMBOL_IMAGE_SHOW,
49 	XML_TOK_SYMBOL_IMAGE_END = XML_TOK_UNKNOWN
50 };
51 
52 static __FAR_DATA SvXMLTokenMapEntry aSymbolImageAttrTokenMap[] =
53 {
54 	{ XML_NAMESPACE_XLINK,  ::xmloff::token::XML_HREF,     XML_TOK_SYMBOL_IMAGE_HREF    },
55 	{ XML_NAMESPACE_XLINK,  ::xmloff::token::XML_TYPE,     XML_TOK_SYMBOL_IMAGE_TYPE    },
56 	{ XML_NAMESPACE_XLINK,  ::xmloff::token::XML_ACTUATE,  XML_TOK_SYMBOL_IMAGE_ACTUATE },
57 	{ XML_NAMESPACE_XLINK,  ::xmloff::token::XML_SHOW,     XML_TOK_SYMBOL_IMAGE_SHOW },
58 	XML_TOKEN_MAP_END
59 };
60 
61 XMLSymbolImageContext::XMLSymbolImageContext(
62     SvXMLImport& rImport, sal_uInt16 nPrfx,
63     const ::rtl::OUString& rLName,
64     const XMLPropertyState& rProp,
65     ::std::vector< XMLPropertyState > &rProps ) :
66         XMLElementPropertyContext(
67             rImport, nPrfx, rLName, rProp, rProps )
68 {
69 }
70 
71 XMLSymbolImageContext::~XMLSymbolImageContext()
72 {}
73 
74 void XMLSymbolImageContext::StartElement( const uno::Reference< xml::sax::XAttributeList >& xAttrList )
75 {
76 	SvXMLTokenMap aTokenMap( aSymbolImageAttrTokenMap );
77     ::rtl::OUString aLocalName;
78 
79     sal_Int16 nAttrCount = xAttrList.is() ? xAttrList->getLength() : 0;
80     for( sal_Int16 i = 0; i < nAttrCount; i++ )
81     {
82 		const ::rtl::OUString& rAttrName = xAttrList->getNameByIndex( i );
83 		sal_uInt16 nPrefix =
84 			GetImport().GetNamespaceMap().GetKeyByAttrName( rAttrName,
85 															&aLocalName );
86 		const ::rtl::OUString& rValue = xAttrList->getValueByIndex( i );
87 
88         switch( aTokenMap.Get( nPrefix, aLocalName ) )
89         {
90             case XML_TOK_SYMBOL_IMAGE_HREF:
91                 msURL = rValue;
92                 break;
93             case XML_TOK_SYMBOL_IMAGE_ACTUATE:
94             case XML_TOK_SYMBOL_IMAGE_TYPE:
95             case XML_TOK_SYMBOL_IMAGE_SHOW:
96                 // these values are currently not interpreted
97                 // it is always assumed 'actuate=onLoad', 'type=simple', 'show=embed'
98                 break;
99         }
100     }
101 }
102 
103 SvXMLImportContext* XMLSymbolImageContext::CreateChildContext(
104     sal_uInt16 nPrefix, const ::rtl::OUString& rLocalName,
105     const uno::Reference< xml::sax::XAttributeList > & xAttrList )
106 {
107 	SvXMLImportContext* pContext = NULL;
108 	if( xmloff::token::IsXMLToken( rLocalName,
109                                    xmloff::token::XML_BINARY_DATA ) )
110 	{
111 		if( ! msURL.getLength() && ! mxBase64Stream.is() )
112 		{
113 			mxBase64Stream = GetImport().GetStreamForGraphicObjectURLFromBase64();
114 			if( mxBase64Stream.is() )
115 				pContext = new XMLBase64ImportContext( GetImport(), nPrefix,
116                                                        rLocalName, xAttrList,
117                                                        mxBase64Stream );
118 		}
119 	}
120 	if( ! pContext )
121 	{
122 		pContext = new SvXMLImportContext( GetImport(), nPrefix, rLocalName );
123 	}
124 
125 	return pContext;
126 }
127 
128 void XMLSymbolImageContext::EndElement()
129 {
130     ::rtl::OUString sResolvedURL;
131 
132     if( msURL.getLength() )
133 	{
134 		sResolvedURL = GetImport().ResolveGraphicObjectURL( msURL, sal_False );
135 	}
136 	else if( mxBase64Stream.is() )
137 	{
138 		sResolvedURL = GetImport().ResolveGraphicObjectURLFromBase64( mxBase64Stream );
139 		mxBase64Stream = 0;
140 	}
141 
142     if( sResolvedURL.getLength())
143     {
144         // aProp is a member of XMLElementPropertyContext
145         aProp.maValue <<= sResolvedURL;
146         SetInsert( sal_True );
147     }
148 
149 	XMLElementPropertyContext::EndElement();
150 }
151