2 * Copyright 2001-2005 Internet2
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 /* ScopedAttribute.cpp - eduPerson scoped attribute base class
26 #include <xercesc/util/regx/RegularExpression.hpp>
28 using namespace shibboleth::logging;
29 using namespace shibboleth;
33 const XMLCh ScopedAttribute::Scope[] = { chLatin_S, chLatin_c, chLatin_o, chLatin_p, chLatin_e, chNull };
35 ScopedAttribute::ScopedAttribute(
38 const saml::QName* type,
40 const saml::Iterator<const XMLCh*>& scopes,
41 const saml::Iterator<const XMLCh*>& values
42 ) : SAMLAttribute(name,ns,type,lifetime,values)
44 RTTI(ScopedAttribute);
45 if (scopes.size()!=values.size())
46 throw MalformedException("ScopedAttribute() requires the number of scopes to equal the number of values");
48 while (scopes.hasNext())
49 m_scopes.push_back(saml::XML::assign(scopes.next()));
52 ScopedAttribute::ScopedAttribute(DOMElement* e) : SAMLAttribute(e,false)
54 RTTI(ScopedAttribute);
58 ScopedAttribute::ScopedAttribute(istream& in) : SAMLAttribute(in,false)
60 RTTI(ScopedAttribute);
61 fromDOM(m_document->getDocumentElement());
64 ScopedAttribute::~ScopedAttribute()
67 for (vector<const XMLCh*>::iterator i=m_scopes.begin(); i!=m_scopes.end(); i++) {
68 XMLCh* p = const_cast<XMLCh*>(*i);
69 XMLString::release(&p);
73 // We always own any scoped values we've built.
74 for (vector<const XMLCh*>::iterator i=m_scopedValues.begin(); i!=m_scopedValues.end(); i++) {
75 XMLCh* p = const_cast<XMLCh*>(*i);
76 XMLString::release(&p);
80 void ScopedAttribute::ownStrings()
83 for (vector<const XMLCh*>::iterator i=m_scopes.begin(); i!=m_scopes.end(); i++)
84 (*i)=saml::XML::assign(*i);
85 SAMLAttribute::ownStrings();
89 Iterator<const XMLCh*> ScopedAttribute::getValues() const
91 static XMLCh at[]={chAt, chNull};
93 if (m_scopedValues.empty()) {
94 vector<const XMLCh*>::const_iterator j=m_scopes.begin();
95 for (vector<const XMLCh*>::const_iterator i=m_values.begin(); i!=m_values.end(); i++, j++) {
96 XMLCh* temp=new XMLCh[XMLString::stringLen(*i) + XMLString::stringLen(*j) + 2];
98 XMLString::catString(temp,*i);
99 XMLString::catString(temp,at);
100 XMLString::catString(temp,*j);
101 m_scopedValues.push_back(temp);
104 return m_scopedValues;
107 Iterator<string> ScopedAttribute::getSingleByteValues() const
110 if (m_sbValues.empty()) {
111 for (vector<const XMLCh*>::const_iterator i=m_scopedValues.begin(); i!=m_scopedValues.end(); i++) {
112 auto_ptr<char> temp(toUTF8(*i));
114 m_sbValues.push_back(temp.get());
120 void ScopedAttribute::setValues(const Iterator<const XMLCh*>& values)
122 throw SAMLException("unsupported operation");
125 void ScopedAttribute::addValue(const XMLCh* value)
127 throw SAMLException("unsupported operation");
130 void ScopedAttribute::removeValue(unsigned long index)
133 XMLCh* p=const_cast<XMLCh*>(m_scopes[index]);
134 XMLString::release(&p);
136 m_scopes.erase(m_scopes.begin()+index);
138 if (!m_scopedValues.empty()) {
139 XMLCh* p=const_cast<XMLCh*>(m_scopedValues[index]);
140 XMLString::release(&p);
141 m_scopedValues.erase(m_scopedValues.begin()+index);
144 SAMLAttribute::removeValue(index);
147 void ScopedAttribute::valueFromDOM(DOMElement* e)
149 SAMLAttribute::valueFromDOM(e);
150 m_scopes.push_back(e->getAttributeNS(NULL,Scope));
153 void ScopedAttribute::valueToDOM(unsigned int index, DOMElement* e) const
155 SAMLAttribute::valueToDOM(index,e);
156 const XMLCh* scope=m_scopes[index];
157 if (!saml::XML::isEmpty(scope))
158 e->setAttributeNS(NULL,Scope,m_scopes[index]);
161 SAMLObject* ScopedAttribute::clone() const
163 return new ScopedAttribute(m_name,m_namespace,m_type,m_lifetime,m_scopes,m_values);