xmerl_scan.erl

来自「OTP是开放电信平台的简称」· ERL 代码 · 共 438 行 · 第 1/2 页

ERL
438
字号
%%% The contents of this file are subject to the Erlang Public License,%%% Version 1.0, (the "License"); you may not use this file except in%%% compliance with the License. You may obtain a copy of the License at%%% http://www.erlang.org/license/EPL1_0.txt%%%%%% Software distributed under the License is distributed on an "AS IS"%%% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See%%% the License for the specific language governing rights and limitations%%% under the License.%%%%%% The Original Code is xmerl-0.15%%%%%% The Initial Developer of the Original Code is Ericsson Telecom%%% AB. Portions created by Ericsson are Copyright (C), 1998, Ericsson%%% Telecom AB. All Rights Reserved.%%%%%% Contributor(s):%%%    Mickael Remond <mickael.remond@IDEALX.com>:%%%    Johan Blom <johan.blom@mobilearts.se>%%%    Richard Carlsson%%%    Fredrik Linder%%%%%%----------------------------------------------------------------------%%% #0.    BASIC INFORMATION%%%----------------------------------------------------------------------%%% File:       xmerl_scan.erl%%% Author       : Ulf Wiger <ulf.wiger@ericsson.com>%%% Description  : Simgle-pass XML scanner. See xmerl.hrl for data defs.%%% %%% Modules used : ets, file, filename, io, lists, xmerl_ucs, xmerl_uri%%% %%%----------------------------------------------------------------------%% @doc This module is the interface to the XML parser, it handles XML 1.0.%%     The XML parser is activated through %%     <tt>xmerl_scan:string/[1,2]</tt> or %%     <tt>xmerl_scan:file/[1,2]</tt>.%%     It returns records of the type defined in xmerl.hrl.%% See also <a href="xmerl_examples.html">tutorial</a> on customization%% functions.%% @type global_state(). <p>%% The global state of the scanner, represented by the #xmerl_scanner{} record.%% </p>%% @type option_list(). <p>Options allow to customize the behaviour of the%%     scanner.%% See also <a href="xmerl_examples.html">tutorial</a> on customization%% functions.%% </p>%% Possible options are:%% <dl>%%  <dt><code>{acc_fun, Fun}</code></dt>%%    <dd>Call back function to accumulate contents of entity.</dd>%%  <dt><code>{continuation_fun, Fun} |%%            {continuation_fun, Fun, ContinuationState}</code></dt>%%    <dd>Call back function to decide what to do if the scanner runs into EOF%%     before the document is complete.</dd>%%  <dt><code>{event_fun, Fun} |%%            {event_fun, Fun, EventState}</code></dt>%%    <dd>Call back function to handle scanner events.</dd>%%  <dt><code>{fetch_fun, Fun} |%%            {fetch_fun, Fun, FetchState}</code></dt>%%    <dd>Call back function to fetch an external resource.</dd>%%  <dt><code>{hook_fun, Fun} |%%            {hook_fun, Fun, HookState}</code></dt>%%    <dd>Call back function to process the document entities once%%     identified.</dd>%%  <dt><code>{close_fun, Fun}</code></dt>%%    <dd>Called when document has been completely parsed.</dd>%%  <dt><code>{rules, ReadFun, WriteFun, RulesState} |%%            {rules, Rules}</code></dt>%%    <dd>Handles storing of scanner information when parsing.</dd>%%  <dt><code>{user_state, UserState}</code></dt>%%    <dd>Global state variable accessible from all customization functions</dd>%%%%  <dt><code>{fetch_path, PathList}</code></dt>%%    <dd>PathList is a list of%%     directories to search when fetching files. If the file in question%%     is not in the fetch_path, the URI will be used as a file%%     name.</dd>%%  <dt><code>{space, Flag}</code></dt>%%    <dd>'preserve' (default) to preserve spaces, 'normalize' to%%    accumulate consecutive whitespace and replace it with one space.</dd>%%  <dt><code>{line, Line}</code></dt>%%    <dd>To specify starting line for scanning in document which contains%%    fragments of XML.</dd>%%  <dt><code>{namespace_conformant, Flag}</code></dt>%%    <dd>Controls whether to behave as a namespace conformant XML parser,%%    'false' (default) to not otherwise 'true'.</dd>%%  <dt><code>{validation, Flag}</code></dt>%%    <dd>Controls whether to process as a validating XML parser:%%    'off' (default) no validation, or validation 'dtd' by DTD or 'schema'%%    by XML Schema. 'false' and 'true' options are obsolete %%    (i.e. they may be removed in a future release), if used 'false' %%    equals 'off' and 'true' equals 'dtd'.</dd>%%  <dt><code>{schemaLocation, [{Namespace,Link}|...]}</code></dt>%%    <dd>Tells explicitly which XML Schema documents to use to validate %%    the XML document. Used together with the %%    <code>{validation,schema}</code> option.</dd>%%  <dt><code>{quiet, Flag}</code></dt>%%    <dd>Set to 'true' if xmerl should behave quietly and not output any %%    information to standard output (default 'false').</dd>%%  <dt><code>{doctype_DTD, DTD}</code></dt>%%    <dd>Allows to specify DTD name when it isn't available in the XML%%    document. This option has effect only together with%%    <code>{validation,'dtd'</code> option.</dd>%%  <dt><code>{xmlbase, Dir}</code></dt>%%    <dd>XML Base directory. If using string/1 default is current directory.%%    If using file/1 default is directory of given file.</dd>%%  <dt><code>{encoding, Enc}</code></dt>%%    <dd>Set default character set used (default UTF-8).%%    This character set is used only if not explicitly given by the XML%%    declaration. </dd>%% </dl>-module(xmerl_scan).-vsn('0.20').-date('03-09-16').%% main API-export([string/1, string/2,	 file/1, file/2]).%% access functions for various states-export([user_state/1, user_state/2,	 event_state/1, event_state/2,	 hook_state/1, hook_state/2,	 rules_state/1, rules_state/2,	 fetch_state/1, fetch_state/2,	 cont_state/1, cont_state/2]).%% helper functions. To xmerl_lib ??-export([accumulate_whitespace/4]).%-define(debug, 1).-include("xmerl.hrl").		% record def, macros-include_lib("kernel/include/file.hrl").-define(fatal(Reason, S),	if	    S#xmerl_scanner.quiet ->		ok;	    true ->		ok=io:format("~p- fatal: ~p~n", [?LINE, Reason])	end,	fatal(Reason, S)).-define(ustate(U, S), S#xmerl_scanner{user_state = U}).%% Functions to access the various states%%% @spec user_state(S::global_state()) -> global_state()%%% @equiv user_state(UserState,S)user_state(#xmerl_scanner{user_state = S}) -> S.%%% @spec event_state(S::global_state()) -> global_state()%%% @equiv event_state(EventState,S)event_state(#xmerl_scanner{fun_states = #xmerl_fun_states{event = S}}) -> S.%%% @spec hook_state(S::global_state()) -> global_state()%%% @equiv hook_state(HookState,S)hook_state(#xmerl_scanner{fun_states = #xmerl_fun_states{hook = S}}) -> S.%%% @spec rules_state(S::global_state()) -> global_state()%%% @equiv rules_state(RulesState,S)rules_state(#xmerl_scanner{fun_states = #xmerl_fun_states{rules = S}}) -> S.%%% @spec fetch_state(S::global_state()) -> global_state()%%% @equiv fetch_state(FetchState,S)fetch_state(#xmerl_scanner{fun_states = #xmerl_fun_states{fetch = S}}) -> S.%%% @spec cont_state(S::global_state()) -> global_state()%%% @equiv cont_state(ContinuationState,S)cont_state(#xmerl_scanner{fun_states = #xmerl_fun_states{cont = S}}) -> S.%%%% Functions to modify the various states%%% @spec user_state(UserState, S::global_state()) -> global_state()%%% @doc For controlling the UserState, to be used in a user function.%%% See <a href="xmerl_examples.html">tutorial</a> on customization functions.user_state(X, S) ->    S#xmerl_scanner{user_state = X}.%%% @spec event_state(EventState, S::global_state()) -> global_state()%%% @doc For controlling the EventState, to be used in an event%%% function, and called at the beginning and at the end of a parsed entity.%%% See <a href="xmerl_examples.html">tutorial</a> on customization functions.event_state(X, S=#xmerl_scanner{fun_states = FS}) ->    FS1 = FS#xmerl_fun_states{event = X},    S#xmerl_scanner{fun_states = FS1}.%%% @spec hook_state(HookState, S::global_state()) -> global_state()%%% @doc For controlling the HookState, to be used in a hook%%% function, and called when the parser has parsed a complete entity.%%% See <a href="xmerl_examples.html">tutorial</a> on customization functions.hook_state(X, S=#xmerl_scanner{fun_states = FS}) ->    FS1 = FS#xmerl_fun_states{hook = X},    S#xmerl_scanner{fun_states = FS1}.%%% @spec rules_state(RulesState, S::global_state()) -> global_state()%%% @doc For controlling the RulesState, to be used in a rules%%% function, and called when the parser store scanner information in a rules%%% database.%%% See <a href="xmerl_examples.html">tutorial</a> on customization functions.rules_state(X, S=#xmerl_scanner{fun_states = FS}) ->    FS1 = FS#xmerl_fun_states{rules = X},    S#xmerl_scanner{fun_states = FS1}.%%% @spec fetch_state(FetchState, S::global_state()) -> global_state()%%% @doc For controlling the FetchState, to be used in a fetch%%% function, and called when the parser fetch an external resource (eg. a DTD).%%% See <a href="xmerl_examples.html">tutorial</a> on customization functions.fetch_state(X, S=#xmerl_scanner{fun_states = FS}) ->    FS1 = FS#xmerl_fun_states{fetch = X},    S#xmerl_scanner{fun_states = FS1}.

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?