1%- 2% Copyright (c) 2012-2014 Yakaz 3% Copyright (c) 2016-2018 Jean-Sébastien Pédron <jean-sebastien.pedron@dumbbell.fr> 4% All rights reserved. 5% 6% Redistribution and use in source and binary forms, with or without 7% modification, are permitted provided that the following conditions 8% are met: 9% 1. Redistributions of source code must retain the above copyright 10% notice, this list of conditions and the following disclaimer. 11% 2. Redistributions in binary form must reproduce the above copyright 12% notice, this list of conditions and the following disclaimer in the 13% documentation and/or other materials provided with the distribution. 14% 15% THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18% ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19% FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20% DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21% OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22% HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23% LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24% OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25% SUCH DAMAGE. 26 27%% @private 28 29-module(yamerl_node_str). 30 31-include("yamerl_errors.hrl"). 32-include("yamerl_tokens.hrl"). 33-include("yamerl_nodes.hrl"). 34-include("internal/yamerl_constr.hrl"). 35 36%% Public API. 37-export([ 38 tags/0, 39 try_construct_token/3, 40 construct_token/3, 41 node_pres/1 42 ]). 43 44-define(TAG, "tag:yaml.org,2002:str"). 45 46%% ------------------------------------------------------------------- 47%% Public API. 48%% ------------------------------------------------------------------- 49 50tags() -> [?TAG]. 51 52try_construct_token(Constr, Node, #yamerl_scalar{} = Token) -> 53 construct_token(Constr, Node, Token); 54try_construct_token(_, _, _) -> 55 unrecognized. 56 57construct_token(#yamerl_constr{detailed_constr = false, ext_options = Options}, 58 undefined, #yamerl_scalar{text = Text}) -> 59 Node = case proplists:get_value(str_node_as_binary, Options, false) of 60 false -> Text; 61 true -> unicode:characters_to_binary(Text); 62 Encoding -> unicode:characters_to_binary(Text, unicode, Encoding) 63 end, 64 {finished, Node}; 65construct_token(#yamerl_constr{detailed_constr = true, ext_options = Options}, 66 undefined, #yamerl_scalar{text = Text} = Token) -> 67 Text1 = case proplists:get_value(str_node_as_binary, Options, false) of 68 false -> Text; 69 true -> unicode:characters_to_binary(Text); 70 Encoding -> unicode:characters_to_binary(Text, unicode, Encoding) 71 end, 72 Pres = yamerl_constr:get_pres_details(Token), 73 Node = #yamerl_str{ 74 module = ?MODULE, 75 tag = ?TAG, 76 pres = Pres, 77 text = Text1 78 }, 79 {finished, Node}; 80 81construct_token(_, _, Token) -> 82 Error = #yamerl_parsing_error{ 83 name = not_a_string, 84 token = Token, 85 text = "Invalid string", 86 line = ?TOKEN_LINE(Token), 87 column = ?TOKEN_COLUMN(Token) 88 }, 89 throw(Error). 90 91node_pres(Node) -> 92 ?NODE_PRES(Node). 93