OSSIM - Open Source Software Image Map  Version 1.9.0 (20180803)
ossimXmlAttribute.cpp
Go to the documentation of this file.
1 //*******************************************************************
2 // Copyright (C) 2001 ImageLinks Inc. All rights reserved.
3 //
4 // License: See top level LICENSE.txt file.
5 //
6 // Author: Oscar Kramer (ossim port by D. Burken)
7 //
8 // Description:
9 //
10 // Contains definition of class ossimXmlAttribute.
11 //
12 //*****************************************************************************
13 // $Id: ossimXmlAttribute.cpp 23503 2015-09-08 07:07:51Z rashadkm $
14 
15 #include <iostream>
16 #include <sstream>
17 
20 
22 
23 static std::istream& xmlskipws(std::istream& in)
24 {
25  int c = in.peek();
26  while((!in.fail())&&
27  ((c == ' ') ||
28  (c == '\t') ||
29  (c == '\n')||
30  (c == '\r')))
31  {
32  in.ignore(1);
33  c = in.peek();
34  }
35 
36  return in;
37 }
38 
40 {
41  std::stringstream in(spec);
42 
43  read(in);
44 }
45 
47  :theName(src.theName),
48  theValue(src.theValue)
49 {
50 }
51 
53 {
54  xmlskipws(in);
55  if(in.fail()) return false;
56  if(readName(in))
57  {
58  xmlskipws(in);
59  if((in.peek() != '=')||
60  (in.fail()))
61  {
63  return false;
64  }
65  in.ignore(1);
66  if(readValue(in))
67  {
68  return true;
69  }
70  else
71  {
73  return false;
74  }
75  }
76  return false;
77 
78 #if 0
79  //
80  // Pull out name:
81  //
82  theName = spec.before('=');
83  theName = theName.trim();
84  if (theName.empty())
85  {
86  ossimNotify(ossimNotifyLevel_FATAL) << "ossimXmlAttribute::ossimXmlAttribute \n"
87  << "Bad attribute format encountered near:\n\""<< spec<<"\"\n"
88  << "Parsing aborted...\n";
90 
91  return;
92  }
93  spec = spec.after('=');
94 
95  //***
96  // Read value:
97  //***
98  char quote_char = spec[0];
99  spec = spec.after(quote_char); // after first quote
100  theValue = spec.before(quote_char); // before second quote
101 
102  //
103  // Reposition attribute specification to the start of next attribute or end
104  // of tag:
105  //
106  spec = spec.after(quote_char); // after second quote
107  ossimString next_entry ("-?[+0-9A-Za-z<>]+");
108  spec = spec.fromRegExp(next_entry.c_str());
109 #endif
110 }
111 
113 {
114 }
115 
117 {
118 }
119 
121  const ossimString& value)
122 {
123  setNameValue(name, value);
124 }
125 
127 {
128  return theName;
129 }
130 
132 {
133  return theValue;
134 }
135 
137  const ossimString& value)
138 {
139  theName = name;
140  theValue = value;
141 }
142 
144 {
145  theName = name;
146 }
147 
149 {
150  theValue = value;
151 }
152 
154 {
155  os << " " << xml_attr->theName << "=\"" << xml_attr->theValue << "\"";
156 
157  return os;
158 }
159 
160 
162 {
163  xmlskipws(in);
164  theName = "";
165  char c = in.peek();
166  while((c != ' ')&&
167  (c != '\n')&&
168  (c != '\r')&&
169  (c != '\t')&&
170  (c != '=')&&
171  (c != '<')&&
172  (c != '/')&&
173  (c != '>')&&
174  (!in.fail()))
175  {
176  theName += (char)in.get();
177  c = in.peek();
178  }
179 
180  return ((!in.fail())&&
181  (theName != ""));
182 }
183 
185 {
186  xmlskipws(in);
187  if(in.fail()) return false;
188  theValue = "";
189  char c = in.peek();
190  bool done = false;
191  char startQuote = '\0';
192  if((c == '\'')||
193  (c == '"'))
194  {
195  startQuote = c;
196  theValue += in.get();
197  while(!done&&!in.fail())
198  {
199  c = in.peek();
200  if(c==startQuote)
201  {
202  theValue += c;
203  done = true;
204  in.ignore(1);
205  }
206  else if(c == '\n')
207  {
208  done = true;
209  }
210  else
211  {
212  theValue += in.get();
213  }
214  }
215  }
216 
217  bool is_empty = false;
218  std::string::size_type p = 0;
219  //then this could be empty with two qoutes
220  if(theValue.size() == 2 && theValue[p] == startQuote && theValue[p+1] == startQuote)
221  {
222  theValue = "";
223  is_empty = true;
224  }
225  if(theValue != "")
226  {
227  std::string::iterator startIter = theValue.begin();
228  std::string::iterator endIter = theValue.end();
229  --endIter;
230  if(*startIter == startQuote)
231  {
232  ++startIter;
233  }
234  else
235  {
236  return false;
237  setErrorStatus();
238  }
239  if(*endIter != startQuote)
240  {
241  return false;
242  setErrorStatus();
243  }
244  theValue = ossimString(startIter, endIter);
245  }
246  return ((!in.bad())&& (is_empty || theValue !=""));
247 }
ossimString before(const ossimString &str, std::string::size_type pos=0) const
METHOD: before(str, pos) Returns string beginning at pos and ending one before the token str If strin...
bool read(std::istream &in)
const ossimString & getName() const
std::basic_stringstream< char > stringstream
Class for char mixed input and output memory streams.
Definition: ossimIosFwd.h:38
void setNameValue(const ossimString &name, const ossimString &value)
const ossimString & getValue() const
bool readValue(std::istream &in)
std::string::iterator end()
Definition: ossimString.h:423
void setValue(const ossimString &value)
#define RTTI_DEF2(cls, name, b1, b2)
Definition: ossimRtti.h:493
std::string::size_type size() const
Definition: ossimString.h:405
std::string::iterator begin()
Definition: ossimString.h:420
ossimString trim(const ossimString &valueToTrim=ossimString(" \\)) const
this will strip lead and trailing character passed in.
std::basic_istream< char > istream
Base class for char input streams.
Definition: ossimIosFwd.h:20
std::ostream & operator<<(std::ostream &os, const ossimXmlAttribute *xml_attr)
void setName(const ossimString &name)
const char * c_str() const
Returns a pointer to a null-terminated array of characters representing the string&#39;s contents...
Definition: ossimString.h:396
bool empty() const
Definition: ossimString.h:411
ossimString after(const ossimString &str, std::string::size_type pos=0) const
METHOD: after(str, pos) Returns string immediately after the token str.
OSSIMDLLEXPORT std::ostream & ossimNotify(ossimNotifyLevel level=ossimNotifyLevel_WARN)
std::basic_ostream< char > ostream
Base class for char output streams.
Definition: ossimIosFwd.h:23
bool readName(std::istream &in)