xref: /trunk/main/svtools/source/svhtml/htmlsupp.cxx (revision cdf0e10c)
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_svtools.hxx"
30 
31 #include <ctype.h>
32 #include <stdio.h>
33 #include <tools/urlobj.hxx>
34 #ifndef _SVSTDARR_HXX
35 #define _SVSTDARR_ULONGS
36 #include <svl/svstdarr.hxx>
37 #endif
38 
39 #include <svtools/parhtml.hxx>
40 #include <svtools/htmltokn.h>
41 #include <svtools/htmlkywd.hxx>
42 
43 /*  */
44 
45 // Tabellen zum Umwandeln von Options-Werten in Strings
46 
47 static HTMLOptionEnum __READONLY_DATA aScriptLangOptEnums[] =
48 {
49 	{ OOO_STRING_SVTOOLS_HTML_LG_starbasic,	HTML_SL_STARBASIC	},
50 	{ OOO_STRING_SVTOOLS_HTML_LG_javascript,	HTML_SL_JAVASCRIPT	},
51 	{ OOO_STRING_SVTOOLS_HTML_LG_javascript11,HTML_SL_JAVASCRIPT	},
52 	{ OOO_STRING_SVTOOLS_HTML_LG_livescript,	HTML_SL_JAVASCRIPT	},
53 //	{ OOO_STRING_SVTOOLS_HTML_LG_unused_javascript, HTML_SL_UNUSEDJS },
54 //	{ OOO_STRING_SVTOOLS_HTML_LG_vbscript,	HTML_SL_VBSCRIPT	},
55 //	{ OOO_STRING_SVTOOLS_HTML_LG_starone,		HTML_SL_STARONE		},
56 	{ 0,					0					}
57 };
58 
59 sal_Bool HTMLParser::ParseScriptOptions( String& rLangString, const String& rBaseURL,
60 									 HTMLScriptLanguage& rLang,
61 									 String& rSrc,
62 									 String& rLibrary,
63 									 String& rModule )
64 {
65 	const HTMLOptions *pScriptOptions = GetOptions();
66 
67 	rLangString.Erase();
68 	rLang = HTML_SL_JAVASCRIPT;
69 	rSrc.Erase();
70 	rLibrary.Erase();
71 	rModule.Erase();
72 
73 	for( sal_uInt16 i = pScriptOptions->Count(); i; )
74 	{
75 		const HTMLOption *pOption = (*pScriptOptions)[ --i ];
76 		switch( pOption->GetToken() )
77 		{
78 		case HTML_O_LANGUAGE:
79 			{
80 				rLangString = pOption->GetString();
81 				sal_uInt16 nLang;
82 				if( pOption->GetEnum( nLang, aScriptLangOptEnums ) )
83 					rLang = (HTMLScriptLanguage)nLang;
84 				else
85 					rLang = HTML_SL_UNKNOWN;
86 			}
87 			break;
88 
89 		case HTML_O_SRC:
90             rSrc = INetURLObject::GetAbsURL( rBaseURL, pOption->GetString() );
91 			break;
92 		case HTML_O_SDLIBRARY:
93 			rLibrary = pOption->GetString();
94 			break;
95 
96 		case HTML_O_SDMODULE:
97 			rModule = pOption->GetString();
98 			break;
99 		}
100 	}
101 
102 	return sal_True;
103 }
104 
105 void HTMLParser::RemoveSGMLComment( String &rString, sal_Bool bFull )
106 {
107 	sal_Unicode c = 0;
108 	while( rString.Len() &&
109 		   ( ' '==(c=rString.GetChar(0)) || '\t'==c || '\r'==c || '\n'==c ) )
110 		rString.Erase( 0, 1 );
111 
112 	while( rString.Len() &&
113 		   ( ' '==(c=rString.GetChar( rString.Len()-1))
114 		   || '\t'==c || '\r'==c || '\n'==c ) )
115 		rString.Erase( rString.Len()-1 );
116 
117 
118 	// SGML-Kommentare entfernen
119 	if( rString.Len() >= 4 &&
120 		rString.CompareToAscii( "<!--", 4 ) == COMPARE_EQUAL )
121 	{
122 		xub_StrLen nPos = 3;
123 		if( bFull )
124 		{
125 			// die gesamte Zeile !
126 			nPos = 4;
127 			while( nPos < rString.Len() &&
128 				( ( c = rString.GetChar( nPos )) != '\r' && c != '\n' ) )
129 				++nPos;
130 			if( c == '\r' && nPos+1 < rString.Len() &&
131 				'\n' == rString.GetChar( nPos+1 ))
132 				++nPos;
133 			else if( c != '\n' )
134 				nPos = 3;
135 		}
136 		rString.Erase( 0, ++nPos );
137 	}
138 
139 	if( rString.Len() >=3 &&
140 		rString.Copy(rString.Len()-3).CompareToAscii("-->")
141 			== COMPARE_EQUAL )
142 	{
143 		rString.Erase( rString.Len()-3 );
144 		if( bFull )
145 		{
146 			// auch noch ein "//" oder "'" und ggf CR/LF davor
147 			rString.EraseTrailingChars();
148 			xub_StrLen nDel = 0, nLen = rString.Len();
149 			if( nLen >= 2 &&
150 				rString.Copy(nLen-2).CompareToAscii("//") == COMPARE_EQUAL )
151 			{
152 				nDel = 2;
153 			}
154 			else if( nLen && '\'' == rString.GetChar(nLen-1) )
155 			{
156 				nDel = 1;
157 			}
158 			if( nDel && nLen >= nDel+1 )
159 			{
160 				c = rString.GetChar( nLen-(nDel+1) );
161 				if( '\r'==c || '\n'==c )
162 				{
163 					nDel++;
164 					if( '\n'==c && nLen >= nDel+1 &&
165 						'\r'==rString.GetChar( nLen-(nDel+1) ) )
166 						nDel++;
167 				}
168 			}
169 			rString.Erase( nLen-nDel );
170 		}
171 	}
172 }
173 
174