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 #include <precomp.h>
23 #include <toolkit/htmlfile.hxx>
24 
25 // NOT FULLY DECLARED SERVICES
26 #include <cosv/file.hxx>
27 #include <udm/html/htmlitem.hxx>
28 
29 namespace
30 {
31 bool            bUse_OOoFrameDiv = true;
32 const String    C_sOOoFrameDiv_IdlId("adc-idlref");
33 }
34 
35 using namespace csi;
36 using csi::xml::AnAttribute;
37 
DocuFile_Html()38 DocuFile_Html::DocuFile_Html()
39     :   sFilePath(),
40         sTitle(),
41         sLocation(),
42         sStyle(),
43         sCssFile(),
44         sCopyright(),
45         aBodyData(),
46         aBuffer(60000) // Grows dynamically, when necessary.
47 {
48 }
49 
50 void
SetLocation(const csv::ploc::Path & i_rFilePath)51 DocuFile_Html::SetLocation( const csv::ploc::Path & i_rFilePath )
52 {
53     StreamLock sPath(1000);
54     i_rFilePath.Get( sPath() );
55 
56     sFilePath = sPath().c_str();
57 }
58 
59 void
SetTitle(const char * i_sTitle)60 DocuFile_Html::SetTitle( const char * i_sTitle )
61 {
62     sTitle = i_sTitle;
63 }
64 
65 void
SetRelativeCssPath(const char * i_sCssFile_relativePath)66 DocuFile_Html::SetRelativeCssPath( const char * i_sCssFile_relativePath )
67 {
68     sCssFile = i_sCssFile_relativePath;
69 }
70 
71 void
SetCopyright(const char * i_sCopyright)72 DocuFile_Html::SetCopyright( const char * i_sCopyright )
73 {
74     sCopyright = i_sCopyright;
75 }
76 
77 void
EmptyBody()78 DocuFile_Html::EmptyBody()
79 {
80     aBodyData.SetContent(0);
81 
82  	if (bUse_OOoFrameDiv)
83  	{
84         // Insert <div> tag to allow better formatting for OOo.
85         aBodyData
86             << new xml::XmlCode("<div id=\"")
87             << new xml::XmlCode(C_sOOoFrameDiv_IdlId)
88             << new xml::XmlCode("\">\n\n");
89  	}
90 
91     aBodyData
92         >> *new html::Label( "_top_" )
93         << " ";
94 }
95 
96 bool
CreateFile()97 DocuFile_Html::CreateFile()
98 {
99     csv::File aFile(sFilePath, csv::CFM_CREATE);
100     if (NOT aFile.open())
101     {
102         Cerr() << "Can't create file " << sFilePath << "." << Endl();
103         return false;
104     }
105 
106     WriteHeader(aFile);
107     WriteBody(aFile);
108 
109     // Write end
110     static const char sCompletion[] = "\n</html>\n";
111     aFile.write( sCompletion );
112 
113     aFile.close();
114     Cout() << '.' << Flush();
115     return true;
116 }
117 
118 
119 void
WriteHeader(csv::File & io_aFile)120 DocuFile_Html::WriteHeader( csv::File & io_aFile )
121 {
122     aBuffer.reset();
123 
124     static const char s1[] =
125         "<html>\n<head>\n<title>";
126     static const char s2[] =
127         "</title>\n"
128         "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n";
129 
130     aBuffer.write( s1 );
131     aBuffer.write( sTitle );
132     aBuffer.write( s2 );
133 
134 
135     if (NOT sCssFile.empty())
136     {
137         static const char s3[] =
138             "<link rel=\"stylesheet\" type=\"text/css\" href=\"";
139         static const char s4[] =
140             "\">\n";
141 
142         aBuffer.write(s3);
143         aBuffer.write(sCssFile);
144         aBuffer.write(s4);
145     }
146 
147     if (NOT sStyle.empty())
148     {
149         static const char s5[] =
150             "<style>";
151         static const char s6[] =
152             "</style>\n";
153 
154         aBuffer.write(s5);
155         aBuffer.write(sStyle);
156         aBuffer.write(s6);
157     }
158 
159     static const char s7[] =
160         "</head>\n";
161     aBuffer.write(s7);
162 
163     io_aFile.write(aBuffer.c_str(), aBuffer.size());
164 }
165 
166 void
WriteBody(csv::File & io_aFile)167 DocuFile_Html::WriteBody( csv::File & io_aFile )
168 {
169     aBuffer.reset();
170 
171     aBodyData
172         >> *new html::Link( "#_top_" )
173                 << "Top of Page";
174 
175     if ( sCopyright.length() > 0 )
176     {
177         aBodyData
178             << new xml::XmlCode("<hr size=\"3\">");
179 
180         aBodyData
181             >> *new html::Paragraph
182                     << new html::ClassAttr( "copyright" )
183                     << new xml::AnAttribute( "align", "center" )
184                     << new xml::XmlCode(sCopyright);
185     }
186 
187  	if (bUse_OOoFrameDiv)
188     {
189         // Insert <div> tag to allow better formatting for OOo.
190     	aBodyData
191             << new xml::XmlCode("\n</div> <!-- id=\"")
192             << new xml::XmlCode(C_sOOoFrameDiv_IdlId)
193             << new xml::XmlCode("\" -->\n");
194     }
195 
196     aBodyData.WriteOut(aBuffer);
197     io_aFile.write(aBuffer.c_str(), aBuffer.size());
198 }
199 
200 
201 
202 
203 
204 
205 
206