1 /* -*- mode: C++; tab-width: 4; c-basic-offset: 4; -*- */
2 
3 /* AbiSource
4  *
5  * Copyright (C) 2007 Philippe Milot <PhilMilot@gmail.com>
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20  * 02110-1301 USA.
21  */
22 
23 // Class definition include
24 #include "OXML_ObjectWithAttrProp.h"
25 
OXML_ObjectWithAttrProp()26 OXML_ObjectWithAttrProp::OXML_ObjectWithAttrProp() :
27 	m_pAttributes(new PP_AttrProp())
28 {
29 }
30 
~OXML_ObjectWithAttrProp()31 OXML_ObjectWithAttrProp::~OXML_ObjectWithAttrProp()
32 {
33 	DELETEP(m_pAttributes);
34 }
35 
36 
setAttribute(const gchar * szName,const gchar * szValue)37 UT_Error OXML_ObjectWithAttrProp::setAttribute(const gchar * szName, const gchar * szValue)
38 {
39 	return m_pAttributes->setAttribute(szName, szValue) ? UT_OK : UT_ERROR;
40 }
41 
setProperty(const gchar * szName,const gchar * szValue)42 UT_Error OXML_ObjectWithAttrProp::setProperty(const gchar * szName, const gchar * szValue)
43 {
44 	return m_pAttributes->setProperty(szName, szValue) ? UT_OK : UT_ERROR;
45 }
46 
setProperty(const std::string & szName,const std::string & szValue)47 UT_Error OXML_ObjectWithAttrProp::setProperty(const std::string & szName, const std::string & szValue)
48 {
49 	return setProperty(szName.c_str(), szValue.c_str());
50 }
51 
getAttribute(const gchar * szName,const gchar * & szValue) const52 UT_Error OXML_ObjectWithAttrProp::getAttribute(const gchar * szName, const gchar *& szValue) const
53 {
54     szValue = NULL;
55 	UT_return_val_if_fail(szName && *szName, UT_ERROR);
56 	if(!m_pAttributes)
57 		return UT_ERROR;
58 
59 	UT_Error ret;
60 	ret = m_pAttributes->getAttribute(szName, szValue) ? UT_OK : UT_ERROR;
61 	if(ret != UT_OK)
62 		return ret;
63 
64 	return (szValue && *szValue) ? UT_OK : UT_ERROR;
65 }
66 
getProperty(const gchar * szName,const gchar * & szValue) const67 UT_Error OXML_ObjectWithAttrProp::getProperty(const gchar * szName, const gchar *& szValue) const
68 {
69     szValue = NULL;
70 	UT_return_val_if_fail(szName && *szName, UT_ERROR);
71 	if(!m_pAttributes)
72 		return UT_ERROR;
73 
74 	UT_Error ret;
75 	ret = m_pAttributes->getProperty(szName, szValue) ? UT_OK : UT_ERROR;
76 	if(ret != UT_OK)
77 		return ret;
78 
79 	return (szValue && *szValue) ? UT_OK : UT_ERROR;
80 }
81 
setAttributes(const gchar ** attributes)82 UT_Error OXML_ObjectWithAttrProp::setAttributes(const gchar ** attributes)
83 {
84 	return m_pAttributes->setAttributes(attributes) ? UT_OK : UT_ERROR;
85 }
86 
setProperties(const gchar ** properties)87 UT_Error OXML_ObjectWithAttrProp::setProperties(const gchar ** properties)
88 {
89 	return m_pAttributes->setProperties(properties) ? UT_OK : UT_ERROR;
90 }
91 
appendAttributes(const gchar ** attributes)92 UT_Error OXML_ObjectWithAttrProp::appendAttributes(const gchar ** attributes)
93 {
94 	UT_return_val_if_fail(attributes != NULL, UT_ERROR);
95 	UT_Error ret;
96 	for (UT_uint32 i = 0; attributes[i] != NULL; i += 2) {
97 		ret = setAttribute(attributes[i], attributes[i+1]);
98 		if (ret != UT_OK) return ret;
99 	}
100 	return UT_OK;
101 }
102 
appendProperties(const gchar ** properties)103 UT_Error OXML_ObjectWithAttrProp::appendProperties(const gchar ** properties)
104 {
105 	UT_return_val_if_fail(properties != NULL, UT_ERROR);
106 	UT_Error ret;
107 	for (UT_uint32 i = 0; properties[i] != NULL; i += 2) {
108 		ret = setProperty(properties[i], properties[i+1]);
109 		if (ret != UT_OK) return ret;
110 	}
111 	return UT_OK;
112 }
113 
getAttributes() const114 const gchar ** OXML_ObjectWithAttrProp::getAttributes() const
115 {
116 	return m_pAttributes->getAttributes();
117 }
118 
getProperties() const119 const gchar ** OXML_ObjectWithAttrProp::getProperties() const
120 {
121 	return m_pAttributes->getProperties();
122 }
123 
getAttributesWithProps()124 const gchar ** OXML_ObjectWithAttrProp::getAttributesWithProps()
125 {
126 	std::string propstring = _generatePropsString();
127 	if (propstring.empty())
128         return getAttributes();
129 
130 	// Use fakeprops here to avoid overwriting props attribute if already exists
131 	UT_return_val_if_fail(UT_OK == setAttribute("fakeprops", propstring.c_str()), NULL);
132 	const gchar ** atts = getAttributes();
133 	for (UT_uint32 i = 0; atts && (atts[i] != NULL); i += 2) {
134 		if (!strcmp(atts[i], "fakeprops"))
135 			atts[i] = PT_PROPS_ATTRIBUTE_NAME;
136 	}
137 	return atts;
138 }
139 
_generatePropsString() const140 std::string OXML_ObjectWithAttrProp::_generatePropsString() const
141 {
142 	const gchar ** props = getProperties();
143 	if (props == NULL)
144         return "";
145 	std::string fmt_props;
146 
147 	for (UT_uint32 i = 0; props[i] != NULL; i += 2) {
148 		fmt_props += props[i];
149 		fmt_props += ":";
150 		fmt_props += props[i+1];
151 		fmt_props += ";";
152 	}
153 	fmt_props.resize(fmt_props.length() - 1); //Shave off the last semicolon, appendFmt doesn't like it
154 	return fmt_props;
155 }
156 
inheritProperties(OXML_ObjectWithAttrProp * parent)157 UT_Error OXML_ObjectWithAttrProp::inheritProperties(OXML_ObjectWithAttrProp* parent)
158 {
159 	if(!parent)
160 		return UT_ERROR;
161 
162 	UT_Error ret = UT_OK;
163 
164 	size_t numProps = parent->getPropertyCount();
165 
166 	const gchar* szName;
167 	const gchar* szValue;
168 
169 	for (size_t i = 0; i<numProps; i++) {
170 
171 		if(!parent->getNthProperty(i, szName, szValue))
172 			break;
173 
174 		const gchar * prop = NULL;
175 		if((getProperty(szName, prop) != UT_OK) || !prop)
176 		{
177 			ret = setProperty(szName, szValue);
178 			if(ret != UT_OK)
179 				return ret;
180 		}
181 	}
182 
183 	return ret;
184 }
185 
getPropertyCount()186 size_t OXML_ObjectWithAttrProp::getPropertyCount()
187 {
188 	return m_pAttributes->getPropertyCount();
189 }
190 
getNthProperty(int i,const gchar * & szName,const gchar * & szValue)191 bool OXML_ObjectWithAttrProp::getNthProperty(int i, const gchar* & szName, const gchar* & szValue)
192 {
193 	return m_pAttributes->getNthProperty(i, szName, szValue);
194 }
195