Home | History | Annotate | Download | only in track
      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 namespace WebCore {
     35 
     36 class WebVTTTokenTypes {
     37 public:
     38     enum Type {
     39         Uninitialized,
     40         Character,
     41         StartTag,
     42         EndTag,
     43         TimestampTag,
     44         EndOfFile,
     45     };
     46 };
     47 
     48 class WebVTTToken {
     49     WTF_MAKE_NONCOPYABLE(WebVTTToken);
     50     WTF_MAKE_FAST_ALLOCATED;
     51 public:
     52     typedef WebVTTTokenTypes Type;
     53     typedef WTF::Vector<UChar, 1024> DataVector; // FIXME: Is this too large for WebVTT?
     54 
     55     WebVTTToken() { clear(); }
     56 
     57     void appendToName(UChar character)
     58     {
     59         ASSERT(m_type == WebVTTTokenTypes::StartTag || m_type == WebVTTTokenTypes::EndTag);
     60         ASSERT(character);
     61         m_data.append(character);
     62     }
     63 
     64     Type::Type type() const { return m_type; }
     65 
     66     const DataVector& name() const
     67     {
     68         return m_data;
     69     }
     70 
     71     const DataVector& characters() const
     72     {
     73         ASSERT(m_type == Type::Character || m_type == Type::TimestampTag);
     74         return m_data;
     75     }
     76 
     77     // Starting a character token works slightly differently than starting
     78     // other types of tokens because we want to save a per-character branch.
     79     void ensureIsCharacterToken()
     80     {
     81         ASSERT(m_type == Type::Uninitialized || m_type == Type::Character);
     82         m_type = Type::Character;
     83     }
     84 
     85     void appendToCharacter(char character)
     86     {
     87         ASSERT(m_type == Type::Character);
     88         m_data.append(character);
     89     }
     90 
     91     void appendToCharacter(UChar character)
     92     {
     93         ASSERT(m_type == Type::Character);
     94         m_data.append(character);
     95     }
     96 
     97     void appendToCharacter(const Vector<LChar, 32>& characters)
     98     {
     99         ASSERT(m_type == Type::Character);
    100         m_data.appendVector(characters);
    101     }
    102 
    103     void beginEmptyStartTag()
    104     {
    105         ASSERT(m_type == Type::Uninitialized);
    106         m_type = Type::StartTag;
    107         m_data.clear();
    108     }
    109 
    110     void beginStartTag(UChar character)
    111     {
    112         ASSERT(character);
    113         ASSERT(m_type == Type::Uninitialized);
    114         m_type = Type::StartTag;
    115         m_data.append(character);
    116     }
    117 
    118     void beginEndTag(LChar character)
    119     {
    120         ASSERT(m_type == Type::Uninitialized);
    121         m_type = Type::EndTag;
    122         m_data.append(character);
    123     }
    124 
    125     void beginTimestampTag(UChar character)
    126     {
    127         ASSERT(character);
    128         ASSERT(m_type == Type::Uninitialized);
    129         m_type = Type::TimestampTag;
    130         m_data.append(character);
    131     }
    132 
    133     void appendToTimestamp(UChar character)
    134     {
    135         ASSERT(character);
    136         ASSERT(m_type == Type::TimestampTag);
    137         m_data.append(character);
    138     }
    139 
    140     void appendToClass(UChar character)
    141     {
    142         appendToStartType(character);
    143     }
    144 
    145     void addNewClass()
    146     {
    147         ASSERT(m_type == Type::StartTag);
    148         if (!m_classes.isEmpty())
    149             m_classes.append(' ');
    150         m_classes.append(m_currentBuffer);
    151         m_currentBuffer.clear();
    152     }
    153 
    154     const DataVector& classes() const
    155     {
    156         return m_classes;
    157     }
    158 
    159     void appendToAnnotation(UChar character)
    160     {
    161         appendToStartType(character);
    162     }
    163 
    164     void addNewAnnotation()
    165     {
    166         ASSERT(m_type == Type::StartTag);
    167         m_annotation.clear();
    168         m_annotation.append(m_currentBuffer);
    169         m_currentBuffer.clear();
    170     }
    171 
    172     const DataVector& annotation() const
    173     {
    174         return m_annotation;
    175     }
    176 
    177     void makeEndOfFile()
    178     {
    179         ASSERT(m_type == Type::Uninitialized);
    180         m_type = Type::EndOfFile;
    181     }
    182 
    183     void clear()
    184     {
    185         m_type = Type::Uninitialized;
    186         m_data.clear();
    187         m_annotation.clear();
    188         m_classes.clear();
    189         m_currentBuffer.clear();
    190     }
    191 
    192 private:
    193     void appendToStartType(UChar character)
    194     {
    195         ASSERT(character);
    196         ASSERT(m_type == Type::StartTag);
    197         m_currentBuffer.append(character);
    198     }
    199 
    200     Type::Type m_type;
    201     DataVector m_data;
    202     DataVector m_annotation;
    203     DataVector m_classes;
    204     DataVector m_currentBuffer;
    205 };
    206 
    207 }
    208 
    209 #endif
    210