initial import
[vuplus_webkit] / Source / WebCore / html / track / WebVTTToken.h
1 /*
2  * Copyright (C) 2011 Google Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #ifndef WebVTTToken_h
32 #define WebVTTToken_h
33
34 #if ENABLE(VIDEO_TRACK)
35
36 #include "MarkupTokenBase.h"
37
38 namespace WebCore {
39
40 class WebVTTTokenTypes {
41 public:
42     enum Type {
43         Uninitialized,
44         Character,
45         StartTag,
46         EndTag,
47         TimestampTag,
48         EndOfFile,
49     };
50 };
51
52 class WebVTTToken : public MarkupTokenBase<WebVTTTokenTypes> {
53 public:
54     void appendToName(UChar character)
55     {
56         ASSERT(m_type == WebVTTTokenTypes::StartTag || m_type == WebVTTTokenTypes::EndTag);
57         MarkupTokenBase<WebVTTTokenTypes>::appendToName(character);
58     }
59     
60     const DataVector& name() const
61     {
62         return MarkupTokenBase<WebVTTTokenTypes>::name();
63     }
64     
65     const DataVector& characters() const
66     {
67         ASSERT(m_type == Type::Character || m_type == Type::TimestampTag);
68         return m_data;
69     }
70     
71     void beginEmptyStartTag()
72     {
73         ASSERT(m_type == Type::Uninitialized);
74         m_type = Type::StartTag;
75         m_currentAttribute = 0;
76         m_attributes.clear();
77         m_data.clear();
78     }
79  
80     void beginTimestampTag(UChar character)
81     {
82         ASSERT(character);
83         ASSERT(m_type == Type::Uninitialized);
84         m_type = Type::TimestampTag;
85         m_selfClosing = true;
86         m_data.append(character);
87     }
88     
89     void appendToTimestamp(UChar character)
90     {
91         ASSERT(character);
92         ASSERT(m_type == Type::TimestampTag);
93         m_data.append(character);
94     }
95
96     void appendToClass(UChar character)
97     {
98         appendToStartType(character);
99     }
100
101     void addNewClass()
102     {
103         ASSERT(m_type == Type::StartTag);
104         if (!m_classes.isEmpty())
105             m_classes.append(' ');
106         m_classes.append(m_currentBuffer);
107         m_currentBuffer.clear();
108     }
109     
110     const DataVector& classes() const
111     {
112         return m_classes;
113     }
114
115     void appendToAnnotation(UChar character)
116     {
117         appendToStartType(character);
118     }
119         
120     void addNewAnnotation()
121     {
122         ASSERT(m_type == Type::StartTag);
123         m_annotation.clear();
124         m_annotation.append(m_currentBuffer);
125         m_currentBuffer.clear();
126     }
127     
128     const DataVector& annotation() const
129     {
130         return m_annotation;
131     }
132     
133     void clear()
134     {
135         m_annotation.clear();
136         m_classes.clear();
137         m_currentBuffer.clear();
138         MarkupTokenBase<WebVTTTokenTypes>::clear();
139     }
140
141 private:
142     void appendToStartType(UChar character)
143     {
144         ASSERT(character);
145         ASSERT(m_type == Type::StartTag);
146         m_currentBuffer.append(character);
147     }
148
149     DataVector m_annotation;
150     DataVector m_classes;
151     DataVector m_currentBuffer;
152 };
153
154 }
155
156 #endif
157 #endif