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 #ifndef _XMLOFF_TRANSFORMER_BASE_HXX
25 #define _XMLOFF_TRANSFORMER_BASE_HXX
26 
27 #include <com/sun/star/xml/sax/SAXParseException.hpp>
28 #include <com/sun/star/xml/sax/SAXException.hpp>
29 #include <com/sun/star/xml/sax/XAttributeList.hpp>
30 #include <com/sun/star/xml/sax/XLocator.hpp>
31 #include <com/sun/star/beans/XPropertySet.hpp>
32 #include <com/sun/star/frame/XModel.hpp>
33 #include <xmloff/xmltoken.hxx>
34 
35 #ifndef _XMLOFF_TRANSFORMER_HXX_
36 #include "Transformer.hxx"
37 #endif
38 
39 namespace rtl { class OUString; }
40 namespace com { namespace sun { namespace star {
41 	namespace i18n { class XCharacterClassification; }
42 }}}
43 
44 class SvXMLNamespaceMap;
45 class XMLTransformerContext;
46 class XMLTransformerContextVector;
47 class XMLTransformerActions;
48 struct XMLTransformerActionInit;
49 struct TransformerAction_Impl;
50 class XMLMutableAttributeList;
51 class XMLTransformerTokenMap;
52 
53 const sal_uInt16 INVALID_ACTIONS = 0xffff;
54 
55 class XMLTransformerBase : public XMLTransformer
56 {
57 	friend class XMLTransformerContext;
58 
59 	::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator >
60 		m_xLocator;
61 
62 	::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler > 			m_xHandler;		// the handlers
63 	::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XExtendedDocumentHandler > 	m_xExtHandler;
64 	::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > m_xPropSet;
65 	::com::sun::star::uno::Reference<
66 		::com::sun::star::i18n::XCharacterClassification > xCharClass;
67 
68 	::rtl::OUString m_aExtPathPrefix;
69 	::rtl::OUString m_aClass;
70 
71 	SvXMLNamespaceMap 			*m_pNamespaceMap;
72 	SvXMLNamespaceMap 			*m_pReplaceNamespaceMap;
73 	XMLTransformerContextVector *m_pContexts;
74 	XMLTransformerActions		*m_pElemActions;
75 	XMLTransformerTokenMap		*m_pTokenMap;
76 
77 protected:
78 	::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >		mxModel;
79 
80 	// This method is called after the namespace map has been updated, but
81 	// before a context for the current element has been pushed.
82 	XMLTransformerContext *CreateContext( sal_uInt16 nPrefix,
83 									  const ::rtl::OUString& rLocalName,
84 									  const ::rtl::OUString& rQName );
85 
86 	// this method may return an empty reference when the transformer service
87 	// was created outside the xmloff environment.
88 	// It is strictly forbiden to use this as a write access to the model!
89 	const ::com::sun::star::uno::Reference< ::com::sun::star::frame::XModel >& getModel() const { return mxModel; }
90 
91 public:
92 	XMLTransformerBase( XMLTransformerActionInit *pInit=0,
93 		   				::xmloff::token::XMLTokenEnum *pTKMapInit=0 ) throw();
94 	virtual ~XMLTransformerBase() throw();
95 
96 	// ::com::sun::star::xml::sax::XDocumentHandler
97 	virtual void SAL_CALL startDocument(void)
98 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
99 	virtual void SAL_CALL endDocument(void)
100 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
101 	virtual void SAL_CALL startElement(const ::rtl::OUString& aName,
102 							  const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XAttributeList > & xAttribs)
103 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
104 	virtual void SAL_CALL endElement(const ::rtl::OUString& aName)
105 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
106 	virtual void SAL_CALL characters(const ::rtl::OUString& aChars)
107 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
108 	virtual void SAL_CALL ignorableWhitespace(const ::rtl::OUString& aWhitespaces)
109 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
110 	virtual void SAL_CALL processingInstruction(const ::rtl::OUString& aTarget,
111 									   const ::rtl::OUString& aData)
112 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
113 	virtual void SAL_CALL setDocumentLocator(const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XLocator > & xLocator)
114 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
115 
116 	// ::com::sun::star::xml::sax::XExtendedDocumentHandler
117 	virtual void SAL_CALL startCDATA(void) throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
118 	virtual void SAL_CALL endCDATA(void) throw( ::com::sun::star::uno::RuntimeException );
119 	virtual void SAL_CALL comment(const ::rtl::OUString& sComment)
120 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
121 	virtual void SAL_CALL allowLineBreak(void)
122 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
123 	virtual void SAL_CALL unknown(const ::rtl::OUString& sString)
124 		throw( ::com::sun::star::xml::sax::SAXException, ::com::sun::star::uno::RuntimeException );
125 
126 	// XInitialization
127     virtual void SAL_CALL initialize( const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >& aArguments ) throw(::com::sun::star::uno::Exception, ::com::sun::star::uno::RuntimeException);
128 
129 	// C++
130 	const ::com::sun::star::uno::Reference< ::com::sun::star::xml::sax::XDocumentHandler > & GetDocHandler() { return m_xHandler; }
131 
132 	const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySet > & GetPropertySet() { return m_xPropSet; }
133 
134 
135 	SvXMLNamespaceMap& GetNamespaceMap() { return *m_pNamespaceMap; }
136 	const SvXMLNamespaceMap& GetNamespaceMap() const { return *m_pNamespaceMap; }
137 	SvXMLNamespaceMap& GetReplaceNamespaceMap() { return *m_pReplaceNamespaceMap; }
138 
139 	XMLTransformerActions& GetElemActions() { return *m_pElemActions; }
140 	virtual XMLTransformerActions *GetUserDefinedActions( sal_uInt16 n );
141 	virtual XMLTransformerContext *CreateUserDefinedContext(
142 									  const TransformerAction_Impl& rAction,
143 									  const ::rtl::OUString& rQName,
144 		   							  sal_Bool bPersistent=sal_False ) = 0;
145 	virtual ::rtl::OUString GetEventName( const ::rtl::OUString& rName,
146 		   								  sal_Bool bForm = sal_False ) = 0;
147 
148 
149 	XMLMutableAttributeList *ProcessAttrList( ::com::sun::star::uno::Reference<
150 				::com::sun::star::xml::sax::XAttributeList >& rAttrList,
151 						 sal_uInt16 nActionMap, sal_Bool bClone );
152 
153 	static sal_Bool ReplaceSingleInchWithIn( ::rtl::OUString& rValue );
154 	static sal_Bool ReplaceSingleInWithInch( ::rtl::OUString& rValue );
155 	static sal_Bool ReplaceInchWithIn( ::rtl::OUString& rValue );
156 	static sal_Bool ReplaceInWithInch( ::rtl::OUString& rValue );
157 
158 	sal_Bool EncodeStyleName( ::rtl::OUString& rName ) const;
159 	static sal_Bool DecodeStyleName( ::rtl::OUString& rName );
160 	static sal_Bool NegPercent( ::rtl::OUString& rValue );
161 
162 	sal_Bool AddNamespacePrefix( ::rtl::OUString& rName,
163 								 sal_uInt16 nPrefix ) const;
164 	sal_Bool RemoveNamespacePrefix( ::rtl::OUString& rName,
165 									sal_uInt16 nPrefixOnly=0xffffU ) const;
166 
167 	sal_Bool ConvertURIToOASIS( ::rtl::OUString& rURI,
168 								sal_Bool bSupportPackage=sal_False ) const;
169 	sal_Bool ConvertURIToOOo( ::rtl::OUString& rURI,
170 								sal_Bool bSupportPackage=sal_False ) const;
171 
172     /** renames the given rOutAttributeValue if one of the parameters contains a
173         matching token in its lower 16 bits.  The value is converted to the
174         token that is given in the upper 16 bits of the matching parameter.
175      */
176     sal_Bool RenameAttributeValue( ::rtl::OUString& rOutAttributeValue,
177                                    sal_Int32 nParam1,
178                                    sal_Int32 nParam2,
179                                    sal_Int32 nParam3 );
180 
181     /** converts the '.' that separates fractions of seconds in a dateTime
182         string into a ',' that was used in the OOo format
183 
184         @param rDateTime
185             A dateTime string that will be parsed and changed in case a match
186             was found.
187         @return <TRUE/> if the given string was changed
188      */
189     static bool ConvertRNGDateTimeToISO( ::rtl::OUString& rDateTime );
190 
191     ::xmloff::token::XMLTokenEnum GetToken( const ::rtl::OUString& rStr ) const;
192 
193 	const XMLTransformerContext *GetCurrentContext() const;
194 	const XMLTransformerContext *GetAncestorContext( sal_uInt32 i ) const;
195 
196 	// C++
197 	inline void SetClass( const ::rtl::OUString& r ) { m_aClass = r; }
198 	inline const ::rtl::OUString& GetClass() const { return m_aClass; }
199 
200 	bool isWriter() const;
201 
202 };
203 
204 #endif	//  _XMLOFF_TRANSFORMER_BASE_HXX
205