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 #include <WW8StreamImpl.hxx>
25 
26 #include <com/sun/star/uno/Reference.h>
27 #include <com/sun/star/io/XSeekable.hpp>
28 #include <com/sun/star/io/XStream.hpp>
29 #include <com/sun/star/lang/XMultiComponentFactory.hpp>
30 
31 #include <doctokLoggers.hxx>
32 
33 namespace writerfilter {
34 namespace doctok
35 {
36 using namespace ::com::sun::star;
37 
38 #ifdef DEBUG
39 TagLogger::Pointer_t debug_logger(TagLogger::getInstance("DEBUG"));
40 #endif
41 
~WW8Stream()42 WW8Stream::~WW8Stream()
43 {
44 }
45 
WW8StreamImpl(uno::Reference<uno::XComponentContext> rContext,uno::Reference<io::XInputStream> rStream)46 WW8StreamImpl::WW8StreamImpl(uno::Reference<uno::XComponentContext> rContext,
47               uno::Reference<io::XInputStream> rStream)
48 : mrComponentContext(rContext), mrStream(rStream)
49 {
50     xFactory = uno::Reference<lang::XMultiComponentFactory>
51         (mrComponentContext->getServiceManager());
52 
53     uno::Sequence<uno::Any> aArgs( 1 );
54     aArgs[0] <<= mrStream;
55 
56     xOLESimpleStorage = uno::Reference<container::XNameContainer>
57         (xFactory->createInstanceWithArgumentsAndContext
58          (::rtl::OUString::createFromAscii
59           ("com.sun.star.embed.OLESimpleStorage"),
60           aArgs, mrComponentContext ),
61          uno::UNO_QUERY );
62 
63 }
64 
~WW8StreamImpl()65 WW8StreamImpl::~WW8StreamImpl()
66 {
67 }
68 
get(sal_uInt32 nOffset,sal_uInt32 nCount) const69 WW8Stream::Sequence WW8StreamImpl::get(sal_uInt32 nOffset,
70                                        sal_uInt32 nCount) const
71 {
72     uno::Sequence<sal_Int8> aSequence;
73 
74     if (nCount > 0)
75     {
76         uno::Reference< io::XSeekable > xSeek( mrStream, uno::UNO_QUERY_THROW );
77 
78         xSeek->seek(nOffset);
79 
80         sal_Int32 nRead = mrStream->readBytes(aSequence, nCount);
81 
82         Sequence aReturnSequence(const_cast<const sal_uInt8 *>
83                                  (reinterpret_cast<sal_uInt8 *>
84                                   (&(aSequence[0]))), nRead);
85 
86         return aReturnSequence;
87     }
88 
89     return WW8Stream::Sequence();
90 }
91 
getSubStream(const::rtl::OUString & sId)92 WW8Stream::Pointer_t WW8StreamImpl::getSubStream(const ::rtl::OUString & sId)
93 {
94     WW8Stream::Pointer_t pResult;
95 
96     try
97     {
98         if (xOLESimpleStorage.is())
99         {
100             if (xOLESimpleStorage->hasByName(sId))
101             {
102                 uno::Reference<io::XStream> xNewStream;
103                 {
104                     uno::Any aValue = xOLESimpleStorage->getByName(sId);
105                     aValue >>= xNewStream;
106                 }
107 
108                 if (xNewStream.is())
109                 {
110                     WW8Stream::Pointer_t
111                         pNew(new WW8StreamImpl(mrComponentContext,
112                                                xNewStream->getInputStream()));
113 
114                     pResult = pNew;
115                 }
116             }
117         }
118     }
119     catch (...)
120     {
121     }
122 
123     if (pResult.get() == NULL)
124         throw ExceptionNotFound("Stream not found");
125 
126     return pResult;
127 }
128 
getSubStreamNames() const129 string WW8StreamImpl::getSubStreamNames() const
130 {
131     string sResult;
132 
133     if (xOLESimpleStorage.is())
134     {
135         uno::Sequence<rtl::OUString> aSeq = xOLESimpleStorage->getElementNames();
136 
137         for (sal_uInt32 n = 0;
138              n < sal::static_int_cast<sal_uInt32>(aSeq.getLength()); ++n)
139         {
140             rtl::OUString aOUStr = aSeq[n];
141 
142             if (n > 0)
143                 sResult += ", ";
144 
145 #if 0
146             rtl::OString aOStr;
147             aOUStr.convertToString(&aOStr, RTL_TEXTENCODING_ASCII_US,
148                                     OUSTRING_TO_OSTRING_CVTFLAGS);
149 
150 
151             sResult += aOStr.getStr();
152 #endif
153             char sBuffer[256];
154             for (sal_uInt32 j = 0;
155                  j < sal::static_int_cast<sal_uInt32>(aOUStr.getLength()); ++j)
156             {
157                 if (isprint(aOUStr[j]))
158                 {
159                     sal_Unicode nC = aOUStr[j];
160 
161                     if (nC < 255)
162                         sResult += sal::static_int_cast<char>(nC);
163                     else
164                         sResult += ".";
165                 }
166                 else
167                 {
168                     snprintf(sBuffer, sizeof(sBuffer), "\\u%x", aOUStr[j]);
169                     sResult += sBuffer;
170                 }
171             }
172         }
173     }
174 
175     return sResult;
176 }
177 
getSubStreamUNames() const178 uno::Sequence<rtl::OUString> WW8StreamImpl::getSubStreamUNames() const
179 {
180     return xOLESimpleStorage->getElementNames();
181 }
182 
dump(OutputWithDepth<string> & o) const183 void WW8StreamImpl::dump(OutputWithDepth<string> & o) const
184 {
185     o.addItem("<stream>");
186 
187     Sequence aSeq;
188     sal_uInt32 nOffset = 0;
189     sal_uInt32 nStep = 16;
190 
191     do
192     {
193         aSeq = get(nOffset, nStep);
194         dumpLine(o, aSeq, nOffset, nStep);
195 
196         nOffset += nStep;
197     }
198     while (aSeq.getCount() == nStep);
199 
200     o.addItem("</stream>");
201 }
202 
203 }}
204