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 // MARKER(update_precomp.py): autogen include statement, do not remove
23 #include "precompiled_svgio.hxx"
24 
25 #include <svgio/svgreader/svgstylenode.hxx>
26 #include <svgio/svgreader/svgdocument.hxx>
27 
28 //////////////////////////////////////////////////////////////////////////////
29 
30 namespace svgio
31 {
32     namespace svgreader
33     {
34         SvgStyleNode::SvgStyleNode(
35             SvgDocument& rDocument,
36             SvgNode* pParent)
37         :   SvgNode(SVGTokenStyle, rDocument, pParent),
38             maSvgStyleAttributes(),
39             mbTextCss(false)
40         {
41         }
42 
43         SvgStyleNode::~SvgStyleNode()
44         {
45             while(!maSvgStyleAttributes.empty())
46             {
47                 delete *(maSvgStyleAttributes.end() - 1);
48                 maSvgStyleAttributes.pop_back();
49             }
50         }
51 
52         void SvgStyleNode::parseAttribute(const rtl::OUString& rTokenName, SVGToken aSVGToken, const rtl::OUString& aContent)
53         {
54             // call parent
55             SvgNode::parseAttribute(rTokenName, aSVGToken, aContent);
56 
57             // parse own
58             switch(aSVGToken)
59             {
60                 case SVGTokenType:
61                 {
62                     if(aContent.getLength())
63                     {
64                         static rtl::OUString aStrTextCss(rtl::OUString::createFromAscii("text/css"));
65 
66                         if(aContent.match(aStrTextCss))
67                         {
68                             setTextCss(true);
69                         }
70                     }
71                     break;
72                 }
73                 default:
74                 {
75                     break;
76                 }
77             }
78         }
79 
80         void SvgStyleNode::addCssStyleSheet(const rtl::OUString& aContent)
81         {
82             const sal_Int32 nLen(aContent.getLength());
83 
84             if(nLen)
85             {
86                 sal_Int32 nPos(0);
87                 rtl::OUStringBuffer aTokenValue;
88 
89                 while(nPos < nLen)
90                 {
91                     const sal_Int32 nInitPos(nPos);
92                     skip_char(aContent, sal_Unicode(' '), sal_Unicode('#'), nPos, nLen);
93                     copyToLimiter(aContent, sal_Unicode('{'), nPos, aTokenValue, nLen);
94                     const rtl::OUString aStyleName = aTokenValue.makeStringAndClear().trim();
95 
96                     if(aStyleName.getLength() && nPos < nLen)
97                     {
98                         skip_char(aContent, sal_Unicode(' '), sal_Unicode('{'), nPos, nLen);
99                         copyToLimiter(aContent, sal_Unicode('}'), nPos, aTokenValue, nLen);
100                         skip_char(aContent, sal_Unicode(' '), sal_Unicode('}'), nPos, nLen);
101                         const rtl::OUString aStyleContent = aTokenValue.makeStringAndClear().trim();
102 
103                         if(aStyleContent.getLength())
104                         {
105                             // create new style
106                             SvgStyleAttributes* pNewStyle = new SvgStyleAttributes(*this);
107                             maSvgStyleAttributes.push_back(pNewStyle);
108 
109                             // fill with content
110                             pNewStyle->readStyle(aStyleContent);
111 
112                             // register new style at document
113                             const_cast< SvgDocument& >(getDocument()).addSvgStyleAttributesToMapper(aStyleName, *pNewStyle);
114                         }
115                     }
116 
117                     if(nInitPos == nPos)
118                     {
119                         OSL_ENSURE(false, "Could not interpret on current position (!)");
120                         nPos++;
121                     }
122                 }
123             }
124         }
125 
126     } // end of namespace svgreader
127 } // end of namespace svgio
128 
129 //////////////////////////////////////////////////////////////////////////////
130 // eof
131