Tizen 2.1 base
[framework/web/wrt-commons.git] / modules / ace / include / dpl / ace / Attribute.h
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 //
17 //
18 //
19 //  @ Project : Access Control Engine
20 //  @ File Name : Attribute.h
21 //  @ Date : 2009-05-06
22 //  @ Author : Samsung
23 //
24 //
25
26 #if !defined(_ATTRIBUTE_H)
27 #define _ATTRIBUTE_H
28
29 #include <string>
30 #include <iostream>
31 #include <set>
32 #include <list>
33
34 #include <dpl/ace-dao-ro/BaseAttribute.h>
35
36 class Attribute : public AceDB::BaseAttribute
37 {
38   public:
39     /**
40      * Types of match functions
41      */
42     enum class Match { Equal, Glob, Regexp, Error };
43     /**
44      * Types of attribute value modifiers
45      */
46     enum class Modifier { Non, Scheme, Authority, SchemeAuthority, Host, Path };
47     /**
48      * Possible match results
49      */
50     enum class MatchResult { MRUndetermined = -1, MRFalse = 0, MRTrue = 1};
51
52   public:
53
54     /**
55      * New attribute constructor
56      * @param name name of the new attribute
57      * @param matchFunction match function used in the attribute
58      * @param type attribute type
59      */
60     Attribute(const std::string *name,
61               const Match matchFunction,
62               const Type type);
63
64     /**
65      * Constructor used by ACE serializer
66      * @param is Input stream to which the object will be serialized
67      * @deprecated
68      */
69     Attribute(std::istream& is);
70
71     /**
72      * Constructor used to create default attribute ( used for unit tests )
73      * @param nm name of the default attribute
74      */
75     Attribute(const std::string& nm) :
76         matchFunction(Match::Error),
77         modifierFunction(Modifier::Non)
78     {
79         m_name = nm;
80         m_typeId = Type::Subject;
81         m_undetermindState = false;
82     }
83
84     /**
85      * Destructor
86      */
87     virtual ~Attribute();
88
89     bool serialize(std::ostream& os) const;
90
91     std::list<std::string> * getValue() const
92     {
93         return AceDB::BaseAttribute::getValue();
94     }
95     Match getMatchFunction() const
96     {
97         return matchFunction;
98     }
99
100     /*  --- Setters --- */
101     void addValue (const std::string *value);
102
103     MatchResult  matchAttributes(const BaseAttribute *) const;
104
105     /**
106      * Operator used in for attribute set,used to distinguished only attribute names
107      * It cannot take attribute type into consideration
108      */
109     bool operator< (const Attribute & obj) const
110     {
111         int result = this->m_name.compare(*obj.getName());
112         if (result == 0) { //If names are equal check attribute types
113             if (this->m_typeId < obj.getType()) {
114                 result = -1;
115             } else if (this->m_typeId > obj.getType()) {
116                 result = 1;
117             }
118         }
119         //If result is negative that means that 'this' was '<' than obj
120         return result < 0;
121     }
122
123      /** Checks if object type is equal to argument */
124     bool instanceOf(Type type_)
125     {
126         return type_ == m_typeId;
127     }
128
129     friend std::ostream & operator<<(std::ostream & out,
130                                      const Attribute & attr);
131
132   protected:
133
134     bool searchAndCut(const char *);
135
136     /*
137      *  URI definition from rfc2396
138      *
139      *  <scheme>://<authority><path>?<query>
140      *  Each of the components may be absent, apart from the scheme.
141      *  Host is a part of authority as in definition below:
142      *
143      *  authority     = server | reg_name
144      *  server        = [ [ userinfo "@" ] hostport ]
145      *  <userinfo>@<host>:<port>
146      *
147      *  Extract from rfc2396
148      *  The authority component is preceded by a double slash "//" and is
149      *  terminated by the next slash "/", question-mark "?", or by the end of
150      *  the URI.  Within the authority component, the characters ";", ":",
151      * "@", "?", and "/" are reserved.
152      *
153      *  Modifiers should return pointer to empty string if given part of string was empty.
154      *  Modifiers should return NULL if the string to be modified was not an URI.
155      */
156     std::string * uriScheme(const std::string *) const;
157     std::string * uriAuthority(const std::string *) const;
158     std::string * uriSchemeAuthority(const std::string *) const;
159     std::string * uriHost(const std::string *) const;
160     std::string * uriPath(const std::string *) const;
161     std::string * applyModifierFunction(const std::string * val) const;
162
163     bool parse(const std::string *input,
164             std::string *part) const;
165     bool find_error(const std::string *part) const;
166
167     bool checkScheme(const std::string *scheme) const;
168     bool checkAuthority(const std::string *scheme) const;
169     std::string * getHost(const std::string *scheme) const;
170     bool checkPath(const std::string *scheme) const;
171
172     bool isSchemeAllowedCharacter(int c) const;
173     bool isSegmentAllowedCharacter(int c) const;
174     bool isUserInfoAllowedString(const std::string *str) const;
175     bool isHostAllowedString(const std::string *str) const;
176     bool isHostNameAllowedString(const std::string * str) const;
177     bool isIPv4AllowedString(const std::string * str) const;
178     bool isDomainLabelAllowedString(const char * data,
179                                     int lenght) const;
180     bool isTopLabelAllowedString(const char* data,
181                                  int lenght) const;
182
183     bool isUnreserved(int c) const;
184     bool isAlphanum(int c) const;
185     bool isEscaped(const char esc[3]) const;
186     bool isHex(int c) const;
187
188     MatchResult lists_comparator(
189         const std::list<std::string> *first,
190         const std::list<std::string> *second,
191         MatchResult (*comparator)(const std::string *,
192                                   const std::string *)) const;
193
194     /**
195      *  Map used to check if character is a 'mark'
196      */
197     static const bool mark[256];
198     /**
199      *  Map used to check if character is a 'digit'
200      *
201      */
202     static const bool digit[256];
203     /**
204      * Map used to check if character is an 'alphanumeric' value
205      *
206      */
207     static const bool alpha[256];
208
209   protected:
210     Match matchFunction;
211     Modifier modifierFunction;
212 };
213
214 typedef AceDB::BaseAttributeSet AttributeSet;
215
216 //TODO remove later or ifdef debug methods
217 void printAttributes(const AttributeSet& attrs);
218 void printAttributes(const std::list<Attribute> & attrs);
219
220 #endif  //_ATTRIBUTE_H