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_svx.hxx" 26 27 #ifndef SVX_SOURCE_FORM_FMDOCUMENTCLASSIFICATION_HXX 28 #include "fmdocumentclassification.hxx" 29 #endif 30 #include "svx/dbtoolsclient.hxx" 31 32 /** === begin UNO includes === **/ 33 #include <com/sun/star/container/XChild.hpp> 34 #include <com/sun/star/lang/XServiceInfo.hpp> 35 #include <com/sun/star/xforms/XFormsSupplier.hpp> 36 #include <com/sun/star/sdbc/XConnection.hpp> 37 #include <com/sun/star/frame/XModule.hpp> 38 /** === end UNO includes === **/ 39 40 #include <tools/diagnose_ex.h> 41 42 //........................................................................ 43 namespace svxform 44 { 45 //........................................................................ 46 47 namespace 48 { 49 using ::com::sun::star::uno::Reference; 50 using ::com::sun::star::uno::XInterface; 51 using ::com::sun::star::container::XChild; 52 using ::com::sun::star::frame::XModel; 53 using ::com::sun::star::uno::UNO_QUERY; 54 using ::com::sun::star::frame::XModule; 55 56 //.................................................................... 57 template< class TYPE > getTypedModelNode(const Reference<XInterface> & _rxModelNode)58 Reference< TYPE > getTypedModelNode( const Reference< XInterface >& _rxModelNode ) 59 { 60 Reference< TYPE > xTypedNode( _rxModelNode, UNO_QUERY ); 61 if ( xTypedNode.is() ) 62 return xTypedNode; 63 else 64 { 65 Reference< XChild > xChild( _rxModelNode, UNO_QUERY ); 66 if ( xChild.is() ) 67 return getTypedModelNode< TYPE >( xChild->getParent() ); 68 else 69 return Reference< TYPE >(); 70 } 71 } 72 73 //.................................................................... getDocument(const Reference<XInterface> & _rxModelNode)74 Reference< XModel > getDocument( const Reference< XInterface >& _rxModelNode ) 75 { 76 return getTypedModelNode< XModel >( _rxModelNode ); 77 } 78 } 79 80 using namespace ::com::sun::star::uno; 81 using namespace ::com::sun::star::frame; 82 using namespace ::com::sun::star::lang; 83 using namespace ::com::sun::star::xforms; 84 using namespace ::com::sun::star::container; 85 using namespace ::com::sun::star::sdbc; 86 87 //==================================================================== 88 //==================================================================== 89 namespace 90 { 91 //---------------------------------------------------------------- 92 struct ModuleInfo 93 { 94 const sal_Char* pAsciiModuleOrServiceName; 95 DocumentType eType; 96 }; 97 98 //---------------------------------------------------------------- lcl_getModuleInfo()99 const ModuleInfo* lcl_getModuleInfo() 100 { 101 static const ModuleInfo aModuleInfo[] = 102 { 103 { "com.sun.star.text.TextDocument", eTextDocument }, 104 { "com.sun.star.text.WebDocument", eWebDocument }, 105 { "com.sun.star.sheet.SpreadsheetDocument", eSpreadsheetDocument }, 106 { "com.sun.star.drawing.DrawingDocument", eDrawingDocument }, 107 { "com.sun.star.presentation.PresentationDocument", ePresentationDocument }, 108 { "com.sun.star.xforms.XMLFormDocument", eEnhancedForm }, 109 { "com.sun.star.sdb.FormDesign", eDatabaseForm }, 110 { "com.sun.star.sdb.TextReportDesign", eDatabaseReport }, 111 { "com.sun.star.text.GlobalDocument", eTextDocument }, 112 { NULL, eUnknownDocumentType } 113 }; 114 return aModuleInfo; 115 } 116 } 117 118 //==================================================================== 119 //= DocumentClassification 120 //==================================================================== 121 //-------------------------------------------------------------------- classifyDocument(const Reference<XModel> & _rxDocumentModel)122 DocumentType DocumentClassification::classifyDocument( const Reference< XModel >& _rxDocumentModel ) SAL_THROW(()) 123 { 124 DocumentType eType( eUnknownDocumentType ); 125 126 OSL_ENSURE( _rxDocumentModel.is(), "DocumentClassification::classifyDocument: invalid document!" ); 127 if ( !_rxDocumentModel.is() ) 128 return eType; 129 130 try 131 { 132 // first, check whether the document has a ModuleIdentifier which we know 133 ::rtl::OUString sModuleIdentifier; 134 Reference< XModule > xModule( _rxDocumentModel, UNO_QUERY ); 135 if ( xModule.is() ) 136 eType = getDocumentTypeForModuleIdentifier( xModule->getIdentifier() ); 137 if ( eType != eUnknownDocumentType ) 138 return eType; 139 140 // second, check whether it supports one of the services we know 141 Reference< XServiceInfo > xSI( _rxDocumentModel, UNO_QUERY_THROW ); 142 const ModuleInfo* pModuleInfo = lcl_getModuleInfo(); 143 while ( pModuleInfo->pAsciiModuleOrServiceName ) 144 { 145 if ( xSI->supportsService( ::rtl::OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName ) ) ) 146 return pModuleInfo->eType; 147 ++pModuleInfo; 148 } 149 150 // last: uhm, there is no last resort 151 OSL_ENSURE( false, "DocumentClassification::classifyDocument: unknown document!" ); 152 } 153 catch( const Exception& ) 154 { 155 DBG_UNHANDLED_EXCEPTION(); 156 } 157 158 return eType; 159 } 160 161 //-------------------------------------------------------------------- classifyHostDocument(const Reference<XInterface> & _rxFormComponent)162 DocumentType DocumentClassification::classifyHostDocument( const Reference< XInterface >& _rxFormComponent ) SAL_THROW(()) 163 { 164 DocumentType eType( eUnknownDocumentType ); 165 166 try 167 { 168 Reference< XModel > xDocument( getDocument( _rxFormComponent.get() ) ); 169 if ( !xDocument.is() ) 170 return eUnknownDocumentType; 171 eType = classifyDocument( xDocument ); 172 } 173 catch( const Exception& ) 174 { 175 OSL_ENSURE( sal_False, "DocumentClassification::classifyHostDocument: caught an exception!" ); 176 } 177 178 return eType; 179 } 180 181 //-------------------------------------------------------------------- getDocumentTypeForModuleIdentifier(const::rtl::OUString & _rModuleIdentifier)182 DocumentType DocumentClassification::getDocumentTypeForModuleIdentifier( const ::rtl::OUString& _rModuleIdentifier ) 183 { 184 const ModuleInfo* pModuleInfo = lcl_getModuleInfo(); 185 while ( pModuleInfo->pAsciiModuleOrServiceName ) 186 { 187 if ( _rModuleIdentifier.equalsAscii( pModuleInfo->pAsciiModuleOrServiceName ) ) 188 return pModuleInfo->eType; 189 ++pModuleInfo; 190 } 191 return eUnknownDocumentType; 192 } 193 194 //-------------------------------------------------------------------- getModuleIdentifierForDocumentType(DocumentType _eType)195 ::rtl::OUString DocumentClassification::getModuleIdentifierForDocumentType( DocumentType _eType ) 196 { 197 const ModuleInfo* pModuleInfo = lcl_getModuleInfo(); 198 while ( pModuleInfo->pAsciiModuleOrServiceName ) 199 { 200 if ( pModuleInfo->eType == _eType ) 201 return ::rtl::OUString::createFromAscii( pModuleInfo->pAsciiModuleOrServiceName ); 202 ++pModuleInfo; 203 } 204 return ::rtl::OUString(); 205 } 206 207 //........................................................................ 208 } // namespace svxform 209 //........................................................................ 210 211