blib/lib/Alvis/Document/Encoding.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« blib/lib/Alvis/HTML.pm ↑ Summary blib/lib/Alvis/Convert.pm »
1 package Alvis::Document::Encoding;
2  
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 $Alvis::Document::Encoding::VERSION = '0.1';
7  
8 1 ▶use HTML::Encoding;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Encode::Guess;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 0 ▶use Alvis::Document::Type;
Subroutine BEGIN: called CC=1 SCAR=2.6
13  
14 #############################################################################
15 #
16 # A collection of routines for checking UTF-8 validity, guessing the
17 # encoding of a document etc.
18 #
19 #############################################################################
20  
21 #############################################################################
22 #
23 # Global variables & constants
24 #
25 ##############################################################################
26  
27 my $DEBUG=0;
28  
29 #
30 # Invalid Utf-8 codes (UTF-16 surrogates and non-valid codes)
31 #
32 my %InvalidUtf8Code=(
33 55296=>1, 55297=>1, 55298=>1, 55299=>1, 55300=>1, 55301=>1, 55302=>1,
34 55303=>1, 55304=>1, 55305=>1, 55306=>1, 55307=>1, 55308=>1, 55309=>1,
35 55310=>1, 55311=>1, 55312=>1, 55313=>1, 55314=>1, 55315=>1, 55316=>1,
36 55317=>1, 55318=>1, 55319=>1, 55320=>1, 55321=>1, 55322=>1, 55323=>1,
37 55324=>1, 55325=>1, 55326=>1, 55327=>1, 55328=>1, 55329=>1, 55330=>1,
38 55331=>1, 55332=>1, 55333=>1, 55334=>1, 55335=>1, 55336=>1, 55337=>1,
39 55338=>1, 55339=>1, 55340=>1, 55341=>1, 55342=>1, 55343=>1, 55344=>1,
40 55345=>1, 55346=>1, 55347=>1, 55348=>1, 55349=>1, 55350=>1, 55351=>1,
41 55352=>1, 55353=>1, 55354=>1, 55355=>1, 55356=>1, 55357=>1, 55358=>1,
42 55359=>1, 55360=>1, 55361=>1, 55362=>1, 55363=>1, 55364=>1, 55365=>1,
43 55366=>1, 55367=>1, 55368=>1, 55369=>1, 55370=>1, 55371=>1, 55372=>1,
44 55373=>1, 55374=>1, 55375=>1, 55376=>1, 55377=>1, 55378=>1, 55379=>1,
45 55380=>1, 55381=>1, 55382=>1, 55383=>1, 55384=>1, 55385=>1, 55386=>1,
46 55387=>1, 55388=>1, 55389=>1, 55390=>1, 55391=>1, 55392=>1, 55393=>1,
47 55394=>1, 55395=>1, 55396=>1, 55397=>1, 55398=>1, 55399=>1, 55400=>1,
48 55401=>1, 55402=>1, 55403=>1, 55404=>1, 55405=>1, 55406=>1, 55407=>1,
49 55408=>1, 55409=>1, 55410=>1, 55411=>1, 55412=>1, 55413=>1, 55414=>1,
50 55415=>1, 55416=>1, 55417=>1, 55418=>1, 55419=>1, 55420=>1, 55421=>1,
51 55422=>1, 55423=>1, 55424=>1, 55425=>1, 55426=>1, 55427=>1, 55428=>1,
52 55429=>1, 55430=>1, 55431=>1, 55432=>1, 55433=>1, 55434=>1, 55435=>1,
53 55436=>1, 55437=>1, 55438=>1, 55439=>1, 55440=>1, 55441=>1, 55442=>1,
54 55443=>1, 55444=>1, 55445=>1, 55446=>1, 55447=>1, 55448=>1, 55449=>1,
55 55450=>1, 55451=>1, 55452=>1, 55453=>1, 55454=>1, 55455=>1, 55456=>1,
56 55457=>1, 55458=>1, 55459=>1, 55460=>1, 55461=>1, 55462=>1, 55463=>1,
57 55464=>1, 55465=>1, 55466=>1, 55467=>1, 55468=>1, 55469=>1, 55470=>1,
58 55471=>1, 55472=>1, 55473=>1, 55474=>1, 55475=>1, 55476=>1, 55477=>1,
59 55478=>1, 55479=>1, 55480=>1, 55481=>1, 55482=>1, 55483=>1, 55484=>1,
60 55485=>1, 55486=>1, 55487=>1, 55488=>1, 55489=>1, 55490=>1, 55491=>1,
61 55492=>1, 55493=>1, 55494=>1, 55495=>1, 55496=>1, 55497=>1, 55498=>1,
62 55499=>1, 55500=>1, 55501=>1, 55502=>1, 55503=>1, 55504=>1, 55505=>1,
63 55506=>1, 55507=>1, 55508=>1, 55509=>1, 55510=>1, 55511=>1, 55512=>1,
64 55513=>1, 55514=>1, 55515=>1, 55516=>1, 55517=>1, 55518=>1, 55519=>1,
65 55520=>1, 55521=>1, 55522=>1, 55523=>1, 55524=>1, 55525=>1, 55526=>1,
66 55527=>1, 55528=>1, 55529=>1, 55530=>1, 55531=>1, 55532=>1, 55533=>1,
67 55534=>1, 55535=>1, 55536=>1, 55537=>1, 55538=>1, 55539=>1, 55540=>1,
68 55541=>1, 55542=>1, 55543=>1, 55544=>1, 55545=>1, 55546=>1, 55547=>1,
69 55548=>1, 55549=>1, 55550=>1, 55551=>1, 55552=>1, 55553=>1, 55554=>1,
70 55555=>1, 55556=>1, 55557=>1, 55558=>1, 55559=>1, 55560=>1, 55561=>1,
71 55562=>1, 55563=>1, 55564=>1, 55565=>1, 55566=>1, 55567=>1, 55568=>1,
72 55569=>1, 55570=>1, 55571=>1, 55572=>1, 55573=>1, 55574=>1, 55575=>1,
73 55576=>1, 55577=>1, 55578=>1, 55579=>1, 55580=>1, 55581=>1, 55582=>1,
74 55583=>1, 55584=>1, 55585=>1, 55586=>1, 55587=>1, 55588=>1, 55589=>1,
75 55590=>1, 55591=>1, 55592=>1, 55593=>1, 55594=>1, 55595=>1, 55596=>1,
76 55597=>1, 55598=>1, 55599=>1, 55600=>1, 55601=>1, 55602=>1, 55603=>1,
77 55604=>1, 55605=>1, 55606=>1, 55607=>1, 55608=>1, 55609=>1, 55610=>1,
78 55611=>1, 55612=>1, 55613=>1, 55614=>1, 55615=>1, 55616=>1, 55617=>1,
79 55618=>1, 55619=>1, 55620=>1, 55621=>1, 55622=>1, 55623=>1, 55624=>1,
80 55625=>1, 55626=>1, 55627=>1, 55628=>1, 55629=>1, 55630=>1, 55631=>1,
81 55632=>1, 55633=>1, 55634=>1, 55635=>1, 55636=>1, 55637=>1, 55638=>1,
82 55639=>1, 55640=>1, 55641=>1, 55642=>1, 55643=>1, 55644=>1, 55645=>1,
83 55646=>1, 55647=>1, 55648=>1, 55649=>1, 55650=>1, 55651=>1, 55652=>1,
84 55653=>1, 55654=>1, 55655=>1, 55656=>1, 55657=>1, 55658=>1, 55659=>1,
85 55660=>1, 55661=>1, 55662=>1, 55663=>1, 55664=>1, 55665=>1, 55666=>1,
86 55667=>1, 55668=>1, 55669=>1, 55670=>1, 55671=>1, 55672=>1, 55673=>1,
87 55674=>1, 55675=>1, 55676=>1, 55677=>1, 55678=>1, 55679=>1, 55680=>1,
88 55681=>1, 55682=>1, 55683=>1, 55684=>1, 55685=>1, 55686=>1, 55687=>1,
89 55688=>1, 55689=>1, 55690=>1, 55691=>1, 55692=>1, 55693=>1, 55694=>1,
90 55695=>1, 55696=>1, 55697=>1, 55698=>1, 55699=>1, 55700=>1, 55701=>1,
91 55702=>1, 55703=>1, 55704=>1, 55705=>1, 55706=>1, 55707=>1, 55708=>1,
92 55709=>1, 55710=>1, 55711=>1, 55712=>1, 55713=>1, 55714=>1, 55715=>1,
93 55716=>1, 55717=>1, 55718=>1, 55719=>1, 55720=>1, 55721=>1, 55722=>1,
94 55723=>1, 55724=>1, 55725=>1, 55726=>1, 55727=>1, 55728=>1, 55729=>1,
95 55730=>1, 55731=>1, 55732=>1, 55733=>1, 55734=>1, 55735=>1, 55736=>1,
96 55737=>1, 55738=>1, 55739=>1, 55740=>1, 55741=>1, 55742=>1, 55743=>1,
97 55744=>1, 55745=>1, 55746=>1, 55747=>1, 55748=>1, 55749=>1, 55750=>1,
98 55751=>1, 55752=>1, 55753=>1, 55754=>1, 55755=>1, 55756=>1, 55757=>1,
99 55758=>1, 55759=>1, 55760=>1, 55761=>1, 55762=>1, 55763=>1, 55764=>1,
100 55765=>1, 55766=>1, 55767=>1, 55768=>1, 55769=>1, 55770=>1, 55771=>1,
101 55772=>1, 55773=>1, 55774=>1, 55775=>1, 55776=>1, 55777=>1, 55778=>1,
102 55779=>1, 55780=>1, 55781=>1, 55782=>1, 55783=>1, 55784=>1, 55785=>1,
103 55786=>1, 55787=>1, 55788=>1, 55789=>1, 55790=>1, 55791=>1, 55792=>1,
104 55793=>1, 55794=>1, 55795=>1, 55796=>1, 55797=>1, 55798=>1, 55799=>1,
105 55800=>1, 55801=>1, 55802=>1, 55803=>1, 55804=>1, 55805=>1, 55806=>1,
106 55807=>1, 55808=>1, 55809=>1, 55810=>1, 55811=>1, 55812=>1, 55813=>1,
107 55814=>1, 55815=>1, 55816=>1, 55817=>1, 55818=>1, 55819=>1, 55820=>1,
108 55821=>1, 55822=>1, 55823=>1, 55824=>1, 55825=>1, 55826=>1, 55827=>1,
109 55828=>1, 55829=>1, 55830=>1, 55831=>1, 55832=>1, 55833=>1, 55834=>1,
110 55835=>1, 55836=>1, 55837=>1, 55838=>1, 55839=>1, 55840=>1, 55841=>1,
111 55842=>1, 55843=>1, 55844=>1, 55845=>1, 55846=>1, 55847=>1, 55848=>1,
112 55849=>1, 55850=>1, 55851=>1, 55852=>1, 55853=>1, 55854=>1, 55855=>1,
113 55856=>1, 55857=>1, 55858=>1, 55859=>1, 55860=>1, 55861=>1, 55862=>1,
114 55863=>1, 55864=>1, 55865=>1, 55866=>1, 55867=>1, 55868=>1, 55869=>1,
115 55870=>1, 55871=>1, 55872=>1, 55873=>1, 55874=>1, 55875=>1, 55876=>1,
116 55877=>1, 55878=>1, 55879=>1, 55880=>1, 55881=>1, 55882=>1, 55883=>1,
117 55884=>1, 55885=>1, 55886=>1, 55887=>1, 55888=>1, 55889=>1, 55890=>1,
118 55891=>1, 55892=>1, 55893=>1, 55894=>1, 55895=>1, 55896=>1, 55897=>1,
119 55898=>1, 55899=>1, 55900=>1, 55901=>1, 55902=>1, 55903=>1, 55904=>1,
120 55905=>1, 55906=>1, 55907=>1, 55908=>1, 55909=>1, 55910=>1, 55911=>1,
121 55912=>1, 55913=>1, 55914=>1, 55915=>1, 55916=>1, 55917=>1, 55918=>1,
122 55919=>1, 55920=>1, 55921=>1, 55922=>1, 55923=>1, 55924=>1, 55925=>1,
123 55926=>1, 55927=>1, 55928=>1, 55929=>1, 55930=>1, 55931=>1, 55932=>1,
124 55933=>1, 55934=>1, 55935=>1, 55936=>1, 55937=>1, 55938=>1, 55939=>1,
125 55940=>1, 55941=>1, 55942=>1, 55943=>1, 55944=>1, 55945=>1, 55946=>1,
126 55947=>1, 55948=>1, 55949=>1, 55950=>1, 55951=>1, 55952=>1, 55953=>1,
127 55954=>1, 55955=>1, 55956=>1, 55957=>1, 55958=>1, 55959=>1, 55960=>1,
128 55961=>1, 55962=>1, 55963=>1, 55964=>1, 55965=>1, 55966=>1, 55967=>1,
129 55968=>1, 55969=>1, 55970=>1, 55971=>1, 55972=>1, 55973=>1, 55974=>1,
130 55975=>1, 55976=>1, 55977=>1, 55978=>1, 55979=>1, 55980=>1, 55981=>1,
131 55982=>1, 55983=>1, 55984=>1, 55985=>1, 55986=>1, 55987=>1, 55988=>1,
132 55989=>1, 55990=>1, 55991=>1, 55992=>1, 55993=>1, 55994=>1, 55995=>1,
133 55996=>1, 55997=>1, 55998=>1, 55999=>1, 56000=>1, 56001=>1, 56002=>1,
134 56003=>1, 56004=>1, 56005=>1, 56006=>1, 56007=>1, 56008=>1, 56009=>1,
135 56010=>1, 56011=>1, 56012=>1, 56013=>1, 56014=>1, 56015=>1, 56016=>1,
136 56017=>1, 56018=>1, 56019=>1, 56020=>1, 56021=>1, 56022=>1, 56023=>1,
137 56024=>1, 56025=>1, 56026=>1, 56027=>1, 56028=>1, 56029=>1, 56030=>1,
138 56031=>1, 56032=>1, 56033=>1, 56034=>1, 56035=>1, 56036=>1, 56037=>1,
139 56038=>1, 56039=>1, 56040=>1, 56041=>1, 56042=>1, 56043=>1, 56044=>1,
140 56045=>1, 56046=>1, 56047=>1, 56048=>1, 56049=>1, 56050=>1, 56051=>1,
141 56052=>1, 56053=>1, 56054=>1, 56055=>1, 56056=>1, 56057=>1, 56058=>1,
142 56059=>1, 56060=>1, 56061=>1, 56062=>1, 56063=>1, 56064=>1, 56065=>1,
143 56066=>1, 56067=>1, 56068=>1, 56069=>1, 56070=>1, 56071=>1, 56072=>1,
144 56073=>1, 56074=>1, 56075=>1, 56076=>1, 56077=>1, 56078=>1, 56079=>1,
145 56080=>1, 56081=>1, 56082=>1, 56083=>1, 56084=>1, 56085=>1, 56086=>1,
146 56087=>1, 56088=>1, 56089=>1, 56090=>1, 56091=>1, 56092=>1, 56093=>1,
147 56094=>1, 56095=>1, 56096=>1, 56097=>1, 56098=>1, 56099=>1, 56100=>1,
148 56101=>1, 56102=>1, 56103=>1, 56104=>1, 56105=>1, 56106=>1, 56107=>1,
149 56108=>1, 56109=>1, 56110=>1, 56111=>1, 56112=>1, 56113=>1, 56114=>1,
150 56115=>1, 56116=>1, 56117=>1, 56118=>1, 56119=>1, 56120=>1, 56121=>1,
151 56122=>1, 56123=>1, 56124=>1, 56125=>1, 56126=>1, 56127=>1, 56128=>1,
152 56129=>1, 56130=>1, 56131=>1, 56132=>1, 56133=>1, 56134=>1, 56135=>1,
153 56136=>1, 56137=>1, 56138=>1, 56139=>1, 56140=>1, 56141=>1, 56142=>1,
154 56143=>1, 56144=>1, 56145=>1, 56146=>1, 56147=>1, 56148=>1, 56149=>1,
155 56150=>1, 56151=>1, 56152=>1, 56153=>1, 56154=>1, 56155=>1, 56156=>1,
156 56157=>1, 56158=>1, 56159=>1, 56160=>1, 56161=>1, 56162=>1, 56163=>1,
157 56164=>1, 56165=>1, 56166=>1, 56167=>1, 56168=>1, 56169=>1, 56170=>1,
158 56171=>1, 56172=>1, 56173=>1, 56174=>1, 56175=>1, 56176=>1, 56177=>1,
159 56178=>1, 56179=>1, 56180=>1, 56181=>1, 56182=>1, 56183=>1, 56184=>1,
160 56185=>1, 56186=>1, 56187=>1, 56188=>1, 56189=>1, 56190=>1, 56191=>1,
161 56192=>1, 56193=>1, 56194=>1, 56195=>1, 56196=>1, 56197=>1, 56198=>1,
162 56199=>1, 56200=>1, 56201=>1, 56202=>1, 56203=>1, 56204=>1, 56205=>1,
163 56206=>1, 56207=>1, 56208=>1, 56209=>1, 56210=>1, 56211=>1, 56212=>1,
164 56213=>1, 56214=>1, 56215=>1, 56216=>1, 56217=>1, 56218=>1, 56219=>1,
165 56220=>1, 56221=>1, 56222=>1, 56223=>1, 56224=>1, 56225=>1, 56226=>1,
166 56227=>1, 56228=>1, 56229=>1, 56230=>1, 56231=>1, 56232=>1, 56233=>1,
167 56234=>1, 56235=>1, 56236=>1, 56237=>1, 56238=>1, 56239=>1, 56240=>1,
168 56241=>1, 56242=>1, 56243=>1, 56244=>1, 56245=>1, 56246=>1, 56247=>1,
169 56248=>1, 56249=>1, 56250=>1, 56251=>1, 56252=>1, 56253=>1, 56254=>1,
170 56255=>1, 56256=>1, 56257=>1, 56258=>1, 56259=>1, 56260=>1, 56261=>1,
171 56262=>1, 56263=>1, 56264=>1, 56265=>1, 56266=>1, 56267=>1, 56268=>1,
172 56269=>1, 56270=>1, 56271=>1, 56272=>1, 56273=>1, 56274=>1, 56275=>1,
173 56276=>1, 56277=>1, 56278=>1, 56279=>1, 56280=>1, 56281=>1, 56282=>1,
174 56283=>1, 56284=>1, 56285=>1, 56286=>1, 56287=>1, 56288=>1, 56289=>1,
175 56290=>1, 56291=>1, 56292=>1, 56293=>1, 56294=>1, 56295=>1, 56296=>1,
176 56297=>1, 56298=>1, 56299=>1, 56300=>1, 56301=>1, 56302=>1, 56303=>1,
177 56304=>1, 56305=>1, 56306=>1, 56307=>1, 56308=>1, 56309=>1, 56310=>1,
178 56311=>1, 56312=>1, 56313=>1, 56314=>1, 56315=>1, 56316=>1, 56317=>1,
179 56318=>1, 56319=>1, 56320=>1, 56321=>1, 56322=>1, 56323=>1, 56324=>1,
180 56325=>1, 56326=>1, 56327=>1, 56328=>1, 56329=>1, 56330=>1, 56331=>1,
181 56332=>1, 56333=>1, 56334=>1, 56335=>1, 56336=>1, 56337=>1, 56338=>1,
182 56339=>1, 56340=>1, 56341=>1, 56342=>1, 56343=>1, 56344=>1, 56345=>1,
183 56346=>1, 56347=>1, 56348=>1, 56349=>1, 56350=>1, 56351=>1, 56352=>1,
184 56353=>1, 56354=>1, 56355=>1, 56356=>1, 56357=>1, 56358=>1, 56359=>1,
185 56360=>1, 56361=>1, 56362=>1, 56363=>1, 56364=>1, 56365=>1, 56366=>1,
186 56367=>1, 56368=>1, 56369=>1, 56370=>1, 56371=>1, 56372=>1, 56373=>1,
187 56374=>1, 56375=>1, 56376=>1, 56377=>1, 56378=>1, 56379=>1, 56380=>1,
188 56381=>1, 56382=>1, 56383=>1, 56384=>1, 56385=>1, 56386=>1, 56387=>1,
189 56388=>1, 56389=>1, 56390=>1, 56391=>1, 56392=>1, 56393=>1, 56394=>1,
190 56395=>1, 56396=>1, 56397=>1, 56398=>1, 56399=>1, 56400=>1, 56401=>1,
191 56402=>1, 56403=>1, 56404=>1, 56405=>1, 56406=>1, 56407=>1, 56408=>1,
192 56409=>1, 56410=>1, 56411=>1, 56412=>1, 56413=>1, 56414=>1, 56415=>1,
193 56416=>1, 56417=>1, 56418=>1, 56419=>1, 56420=>1, 56421=>1, 56422=>1,
194 56423=>1, 56424=>1, 56425=>1, 56426=>1, 56427=>1, 56428=>1, 56429=>1,
195 56430=>1, 56431=>1, 56432=>1, 56433=>1, 56434=>1, 56435=>1, 56436=>1,
196 56437=>1, 56438=>1, 56439=>1, 56440=>1, 56441=>1, 56442=>1, 56443=>1,
197 56444=>1, 56445=>1, 56446=>1, 56447=>1, 56448=>1, 56449=>1, 56450=>1,
198 56451=>1, 56452=>1, 56453=>1, 56454=>1, 56455=>1, 56456=>1, 56457=>1,
199 56458=>1, 56459=>1, 56460=>1, 56461=>1, 56462=>1, 56463=>1, 56464=>1,
200 56465=>1, 56466=>1, 56467=>1, 56468=>1, 56469=>1, 56470=>1, 56471=>1,
201 56472=>1, 56473=>1, 56474=>1, 56475=>1, 56476=>1, 56477=>1, 56478=>1,
202 56479=>1, 56480=>1, 56481=>1, 56482=>1, 56483=>1, 56484=>1, 56485=>1,
203 56486=>1, 56487=>1, 56488=>1, 56489=>1, 56490=>1, 56491=>1, 56492=>1,
204 56493=>1, 56494=>1, 56495=>1, 56496=>1, 56497=>1, 56498=>1, 56499=>1,
205 56500=>1, 56501=>1, 56502=>1, 56503=>1, 56504=>1, 56505=>1, 56506=>1,
206 56507=>1, 56508=>1, 56509=>1, 56510=>1, 56511=>1, 56512=>1, 56513=>1,
207 56514=>1, 56515=>1, 56516=>1, 56517=>1, 56518=>1, 56519=>1, 56520=>1,
208 56521=>1, 56522=>1, 56523=>1, 56524=>1, 56525=>1, 56526=>1, 56527=>1,
209 56528=>1, 56529=>1, 56530=>1, 56531=>1, 56532=>1, 56533=>1, 56534=>1,
210 56535=>1, 56536=>1, 56537=>1, 56538=>1, 56539=>1, 56540=>1, 56541=>1,
211 56542=>1, 56543=>1, 56544=>1, 56545=>1, 56546=>1, 56547=>1, 56548=>1,
212 56549=>1, 56550=>1, 56551=>1, 56552=>1, 56553=>1, 56554=>1, 56555=>1,
213 56556=>1, 56557=>1, 56558=>1, 56559=>1, 56560=>1, 56561=>1, 56562=>1,
214 56563=>1, 56564=>1, 56565=>1, 56566=>1, 56567=>1, 56568=>1, 56569=>1,
215 56570=>1, 56571=>1, 56572=>1, 56573=>1, 56574=>1, 56575=>1, 56576=>1,
216 56577=>1, 56578=>1, 56579=>1, 56580=>1, 56581=>1, 56582=>1, 56583=>1,
217 56584=>1, 56585=>1, 56586=>1, 56587=>1, 56588=>1, 56589=>1, 56590=>1,
218 56591=>1, 56592=>1, 56593=>1, 56594=>1, 56595=>1, 56596=>1, 56597=>1,
219 56598=>1, 56599=>1, 56600=>1, 56601=>1, 56602=>1, 56603=>1, 56604=>1,
220 56605=>1, 56606=>1, 56607=>1, 56608=>1, 56609=>1, 56610=>1, 56611=>1,
221 56612=>1, 56613=>1, 56614=>1, 56615=>1, 56616=>1, 56617=>1, 56618=>1,
222 56619=>1, 56620=>1, 56621=>1, 56622=>1, 56623=>1, 56624=>1, 56625=>1,
223 56626=>1, 56627=>1, 56628=>1, 56629=>1, 56630=>1, 56631=>1, 56632=>1,
224 56633=>1, 56634=>1, 56635=>1, 56636=>1, 56637=>1, 56638=>1, 56639=>1,
225 56640=>1, 56641=>1, 56642=>1, 56643=>1, 56644=>1, 56645=>1, 56646=>1,
226 56647=>1, 56648=>1, 56649=>1, 56650=>1, 56651=>1, 56652=>1, 56653=>1,
227 56654=>1, 56655=>1, 56656=>1, 56657=>1, 56658=>1, 56659=>1, 56660=>1,
228 56661=>1, 56662=>1, 56663=>1, 56664=>1, 56665=>1, 56666=>1, 56667=>1,
229 56668=>1, 56669=>1, 56670=>1, 56671=>1, 56672=>1, 56673=>1, 56674=>1,
230 56675=>1, 56676=>1, 56677=>1, 56678=>1, 56679=>1, 56680=>1, 56681=>1,
231 56682=>1, 56683=>1, 56684=>1, 56685=>1, 56686=>1, 56687=>1, 56688=>1,
232 56689=>1, 56690=>1, 56691=>1, 56692=>1, 56693=>1, 56694=>1, 56695=>1,
233 56696=>1, 56697=>1, 56698=>1, 56699=>1, 56700=>1, 56701=>1, 56702=>1,
234 56703=>1, 56704=>1, 56705=>1, 56706=>1, 56707=>1, 56708=>1, 56709=>1,
235 56710=>1, 56711=>1, 56712=>1, 56713=>1, 56714=>1, 56715=>1, 56716=>1,
236 56717=>1, 56718=>1, 56719=>1, 56720=>1, 56721=>1, 56722=>1, 56723=>1,
237 56724=>1, 56725=>1, 56726=>1, 56727=>1, 56728=>1, 56729=>1, 56730=>1,
238 56731=>1, 56732=>1, 56733=>1, 56734=>1, 56735=>1, 56736=>1, 56737=>1,
239 56738=>1, 56739=>1, 56740=>1, 56741=>1, 56742=>1, 56743=>1, 56744=>1,
240 56745=>1, 56746=>1, 56747=>1, 56748=>1, 56749=>1, 56750=>1, 56751=>1,
241 56752=>1, 56753=>1, 56754=>1, 56755=>1, 56756=>1, 56757=>1, 56758=>1,
242 56759=>1, 56760=>1, 56761=>1, 56762=>1, 56763=>1, 56764=>1, 56765=>1,
243 56766=>1, 56767=>1, 56768=>1, 56769=>1, 56770=>1, 56771=>1, 56772=>1,
244 56773=>1, 56774=>1, 56775=>1, 56776=>1, 56777=>1, 56778=>1, 56779=>1,
245 56780=>1, 56781=>1, 56782=>1, 56783=>1, 56784=>1, 56785=>1, 56786=>1,
246 56787=>1, 56788=>1, 56789=>1, 56790=>1, 56791=>1, 56792=>1, 56793=>1,
247 56794=>1, 56795=>1, 56796=>1, 56797=>1, 56798=>1, 56799=>1, 56800=>1,
248 56801=>1, 56802=>1, 56803=>1, 56804=>1, 56805=>1, 56806=>1, 56807=>1,
249 56808=>1, 56809=>1, 56810=>1, 56811=>1, 56812=>1, 56813=>1, 56814=>1,
250 56815=>1, 56816=>1, 56817=>1, 56818=>1, 56819=>1, 56820=>1, 56821=>1,
251 56822=>1, 56823=>1, 56824=>1, 56825=>1, 56826=>1, 56827=>1, 56828=>1,
252 56829=>1, 56830=>1, 56831=>1, 56832=>1, 56833=>1, 56834=>1, 56835=>1,
253 56836=>1, 56837=>1, 56838=>1, 56839=>1, 56840=>1, 56841=>1, 56842=>1,
254 56843=>1, 56844=>1, 56845=>1, 56846=>1, 56847=>1, 56848=>1, 56849=>1,
255 56850=>1, 56851=>1, 56852=>1, 56853=>1, 56854=>1, 56855=>1, 56856=>1,
256 56857=>1, 56858=>1, 56859=>1, 56860=>1, 56861=>1, 56862=>1, 56863=>1,
257 56864=>1, 56865=>1, 56866=>1, 56867=>1, 56868=>1, 56869=>1, 56870=>1,
258 56871=>1, 56872=>1, 56873=>1, 56874=>1, 56875=>1, 56876=>1, 56877=>1,
259 56878=>1, 56879=>1, 56880=>1, 56881=>1, 56882=>1, 56883=>1, 56884=>1,
260 56885=>1, 56886=>1, 56887=>1, 56888=>1, 56889=>1, 56890=>1, 56891=>1,
261 56892=>1, 56893=>1, 56894=>1, 56895=>1, 56896=>1, 56897=>1, 56898=>1,
262 56899=>1, 56900=>1, 56901=>1, 56902=>1, 56903=>1, 56904=>1, 56905=>1,
263 56906=>1, 56907=>1, 56908=>1, 56909=>1, 56910=>1, 56911=>1, 56912=>1,
264 56913=>1, 56914=>1, 56915=>1, 56916=>1, 56917=>1, 56918=>1, 56919=>1,
265 56920=>1, 56921=>1, 56922=>1, 56923=>1, 56924=>1, 56925=>1, 56926=>1,
266 56927=>1, 56928=>1, 56929=>1, 56930=>1, 56931=>1, 56932=>1, 56933=>1,
267 56934=>1, 56935=>1, 56936=>1, 56937=>1, 56938=>1, 56939=>1, 56940=>1,
268 56941=>1, 56942=>1, 56943=>1, 56944=>1, 56945=>1, 56946=>1, 56947=>1,
269 56948=>1, 56949=>1, 56950=>1, 56951=>1, 56952=>1, 56953=>1, 56954=>1,
270 56955=>1, 56956=>1, 56957=>1, 56958=>1, 56959=>1, 56960=>1, 56961=>1,
271 56962=>1, 56963=>1, 56964=>1, 56965=>1, 56966=>1, 56967=>1, 56968=>1,
272 56969=>1, 56970=>1, 56971=>1, 56972=>1, 56973=>1, 56974=>1, 56975=>1,
273 56976=>1, 56977=>1, 56978=>1, 56979=>1, 56980=>1, 56981=>1, 56982=>1,
274 56983=>1, 56984=>1, 56985=>1, 56986=>1, 56987=>1, 56988=>1, 56989=>1,
275 56990=>1, 56991=>1, 56992=>1, 56993=>1, 56994=>1, 56995=>1, 56996=>1,
276 56997=>1, 56998=>1, 56999=>1, 57000=>1, 57001=>1, 57002=>1, 57003=>1,
277 57004=>1, 57005=>1, 57006=>1, 57007=>1, 57008=>1, 57009=>1, 57010=>1,
278 57011=>1, 57012=>1, 57013=>1, 57014=>1, 57015=>1, 57016=>1, 57017=>1,
279 57018=>1, 57019=>1, 57020=>1, 57021=>1, 57022=>1, 57023=>1, 57024=>1,
280 57025=>1, 57026=>1, 57027=>1, 57028=>1, 57029=>1, 57030=>1, 57031=>1,
281 57032=>1, 57033=>1, 57034=>1, 57035=>1, 57036=>1, 57037=>1, 57038=>1,
282 57039=>1, 57040=>1, 57041=>1, 57042=>1, 57043=>1, 57044=>1, 57045=>1,
283 57046=>1, 57047=>1, 57048=>1, 57049=>1, 57050=>1, 57051=>1, 57052=>1,
284 57053=>1, 57054=>1, 57055=>1, 57056=>1, 57057=>1, 57058=>1, 57059=>1,
285 57060=>1, 57061=>1, 57062=>1, 57063=>1, 57064=>1, 57065=>1, 57066=>1,
286 57067=>1, 57068=>1, 57069=>1, 57070=>1, 57071=>1, 57072=>1, 57073=>1,
287 57074=>1, 57075=>1, 57076=>1, 57077=>1, 57078=>1, 57079=>1, 57080=>1,
288 57081=>1, 57082=>1, 57083=>1, 57084=>1, 57085=>1, 57086=>1, 57087=>1,
289 57088=>1, 57089=>1, 57090=>1, 57091=>1, 57092=>1, 57093=>1, 57094=>1,
290 57095=>1, 57096=>1, 57097=>1, 57098=>1, 57099=>1, 57100=>1, 57101=>1,
291 57102=>1, 57103=>1, 57104=>1, 57105=>1, 57106=>1, 57107=>1, 57108=>1,
292 57109=>1, 57110=>1, 57111=>1, 57112=>1, 57113=>1, 57114=>1, 57115=>1,
293 57116=>1, 57117=>1, 57118=>1, 57119=>1, 57120=>1, 57121=>1, 57122=>1,
294 57123=>1, 57124=>1, 57125=>1, 57126=>1, 57127=>1, 57128=>1, 57129=>1,
295 57130=>1, 57131=>1, 57132=>1, 57133=>1, 57134=>1, 57135=>1, 57136=>1,
296 57137=>1, 57138=>1, 57139=>1, 57140=>1, 57141=>1, 57142=>1, 57143=>1,
297 57144=>1, 57145=>1, 57146=>1, 57147=>1, 57148=>1, 57149=>1, 57150=>1,
298 57151=>1, 57152=>1, 57153=>1, 57154=>1, 57155=>1, 57156=>1, 57157=>1,
299 57158=>1, 57159=>1, 57160=>1, 57161=>1, 57162=>1, 57163=>1, 57164=>1,
300 57165=>1, 57166=>1, 57167=>1, 57168=>1, 57169=>1, 57170=>1, 57171=>1,
301 57172=>1, 57173=>1, 57174=>1, 57175=>1, 57176=>1, 57177=>1, 57178=>1,
302 57179=>1, 57180=>1, 57181=>1, 57182=>1, 57183=>1, 57184=>1, 57185=>1,
303 57186=>1, 57187=>1, 57188=>1, 57189=>1, 57190=>1, 57191=>1, 57192=>1,
304 57193=>1, 57194=>1, 57195=>1, 57196=>1, 57197=>1, 57198=>1, 57199=>1,
305 57200=>1, 57201=>1, 57202=>1, 57203=>1, 57204=>1, 57205=>1, 57206=>1,
306 57207=>1, 57208=>1, 57209=>1, 57210=>1, 57211=>1, 57212=>1, 57213=>1,
307 57214=>1, 57215=>1, 57216=>1, 57217=>1, 57218=>1, 57219=>1, 57220=>1,
308 57221=>1, 57222=>1, 57223=>1, 57224=>1, 57225=>1, 57226=>1, 57227=>1,
309 57228=>1, 57229=>1, 57230=>1, 57231=>1, 57232=>1, 57233=>1, 57234=>1,
310 57235=>1, 57236=>1, 57237=>1, 57238=>1, 57239=>1, 57240=>1, 57241=>1,
311 57242=>1, 57243=>1, 57244=>1, 57245=>1, 57246=>1, 57247=>1, 57248=>1,
312 57249=>1, 57250=>1, 57251=>1, 57252=>1, 57253=>1, 57254=>1, 57255=>1,
313 57256=>1, 57257=>1, 57258=>1, 57259=>1, 57260=>1, 57261=>1, 57262=>1,
314 57263=>1, 57264=>1, 57265=>1, 57266=>1, 57267=>1, 57268=>1, 57269=>1,
315 57270=>1, 57271=>1, 57272=>1, 57273=>1, 57274=>1, 57275=>1, 57276=>1,
316 57277=>1, 57278=>1, 57279=>1, 57280=>1, 57281=>1, 57282=>1, 57283=>1,
317 57284=>1, 57285=>1, 57286=>1, 57287=>1, 57288=>1, 57289=>1, 57290=>1,
318 57291=>1, 57292=>1, 57293=>1, 57294=>1, 57295=>1, 57296=>1, 57297=>1,
319 57298=>1, 57299=>1, 57300=>1, 57301=>1, 57302=>1, 57303=>1, 57304=>1,
320 57305=>1, 57306=>1, 57307=>1, 57308=>1, 57309=>1, 57310=>1, 57311=>1,
321 57312=>1, 57313=>1, 57314=>1, 57315=>1, 57316=>1, 57317=>1, 57318=>1,
322 57319=>1, 57320=>1, 57321=>1, 57322=>1, 57323=>1, 57324=>1, 57325=>1,
323 57326=>1, 57327=>1, 57328=>1, 57329=>1, 57330=>1, 57331=>1, 57332=>1,
324 57333=>1, 57334=>1, 57335=>1, 57336=>1, 57337=>1, 57338=>1, 57339=>1,
325 57340=>1, 57341=>1, 57342=>1, 57343=>1, 64976=>1, 64977=>1, 64978=>1,
326 64979=>1, 64980=>1, 64981=>1, 64982=>1, 64983=>1, 64984=>1, 64985=>1,
327 64986=>1, 64987=>1, 64988=>1, 64989=>1, 64990=>1, 64991=>1, 64992=>1,
328 64993=>1, 64994=>1, 64995=>1, 64996=>1, 64997=>1, 64998=>1, 64999=>1,
329 65000=>1, 65001=>1, 65002=>1, 65003=>1, 65004=>1, 65005=>1, 65006=>1,
330 65007=>1, 65534=>1, 65535=>1, 131070=>1, 131071=>1, 196606=>1, 196607=>1,
331 262142=>1, 262143=>1, 327678=>1, 327679=>1, 393214=>1, 393215=>1, 458750=>1,
332 458751=>1, 524286=>1, 524287=>1, 589822=>1, 589823=>1, 655358=>1, 655359=>1,
333 720894=>1, 720895=>1, 786430=>1, 786431=>1, 851966=>1, 851967=>1, 917502=>1,
334 917503=>1, 983038=>1, 983039=>1, 1048574=>1, 1048575=>1, 1114110=>1, 1114111=>1,
335 );
336  
337  
338 #############################################################################
339 #
340 # Error message stuff
341 #
342 #############################################################################
343  
344 my $ErrStr;
345 my ($ERR_OK,
346 $ERR_ILLEGAL_CODE,
347 $ERR_DOC,
348 $ERR_DOC_TYPE,
349 $ERR_DOC_SUB_TYPE,
350 $ERR_BOM,
351 $ERR_FIRST_CHARS,
352 $ERR_META,
353 $ERR_XML,
354 $ERR_GUESS,
355 $ERR_WRONG_GUESS,
356 $ERR_ILLEGAL_CHAR,
357 $ERR_DOC_TYPE_WIZARD,
358 $ERR_TYPE_GUESS,
359 $ERR_ENCODE_GUESS,
360 $ERR_GUESS_AND_CONVERT,
361 $ERR_UNABLE_TO_GUESS
362 )=(0..16);
363 my %ErrMsgs=($ERR_OK=>"",
364 $ERR_ILLEGAL_CODE=>"Illegal UTF-8 code.",
365 $ERR_DOC=>"No document text.",
366 $ERR_DOC_TYPE=>"No document type.",
367 $ERR_DOC_SUB_TYPE=>"No document subtype.",
368 $ERR_BOM=>"Byte order mark recognition failed miserably.",
369 $ERR_FIRST_CHARS=>"Guessing from the first characters " .
370 "failed miserably.",
371 $ERR_META=>"Guessing from the meta information " .
372 "failed miserably.",
373 $ERR_XML=>"Guessing from XML format failed miserably.",
374 $ERR_GUESS=>"Unable to guess at the encoding.",
375 $ERR_WRONG_GUESS=>"This pair does not convert:",
376 $ERR_ILLEGAL_CHAR=>"Illegal character in supposedly UTF-8 " .
377 "result.",
378 $ERR_DOC_TYPE_WIZARD=>"Instantiating Alvis::Document::Type",
379 $ERR_TYPE_GUESS=>"Guessing the document type failed.",
380 $ERR_ENCODE_GUESS=>"Encode::Guess failed.",
381 $ERR_GUESS_AND_CONVERT=>"Guessing an encoding and then " .
382 "converting failed.",
383 $ERR_UNABLE_TO_GUESS=>"Unable to guess at encoding name " .
384 "corrections."
385 );
386  
387 sub _set_err_state
388 {
389 my $self=shift;
390 my $errcode=shift;
391 my $errmsg=shift;
392  
393 if (!defined($errcode))
394 {
395 confess("set_err_state() called with an undefined argument.");
396 }
397  
398 if (exists($ErrMsgs{$errcode}))
399 {
400 if ($errcode==$ERR_OK)
401 {
402 $self->{errstr}="";
403 }
404 else
405 {
406 $self->{errstr}.=" " . $ErrMsgs{$errcode};
407 if (defined($errmsg))
408 {
409 $self->{errstr}.=" " . $errmsg;
410 }
411 }
412 }
413 else
414 {
415 confess("Internal error: set_err_state() called with an " .
416 "unrecognized argument ($errcode).")
417 }
418 }
419  
420 sub errmsg
421 {
422 my $self=shift;
423  
424 return $self->{errstr};
425 }
426  
427 #############################################################################
428 #
429 # Methods
430 #
431 ##############################################################################
432
433 sub new
434 {
435 my $proto=shift;
436  
437 my $class=ref($proto)||$proto;
438 my $parent=ref($proto)&&$proto;
439 my $self={};
440 bless($self,$class);
441  
442 $self->_init(@_);
443  
444 $self->_set_err_state($ERR_OK);
445  
446 $self->{docTypeWizard}=
447 Alvis::Document::Type->new(defaultType=>
448 $self->{defaultDocType},
449 defaultSubType=>
450 $self->{defaultDocSubType});
451 if (!defined($self->{docTypeWizard}))
452 {
453 $self->_set_err_state($ERR_DOC_TYPE_WIZARD);
454 return undef;
455 }
456  
457 Encode::Guess->add_suspects(qw/latin1 iso-8859-2 iso-8859-5 iso-8859-10 iso-8859-7 windows-1252/);
458  
459 return $self;
460 }
461  
462 sub _init
463 {
464 my $self=shift;
465  
466 $self->{defaultDocType}='text';
467 $self->{defaultDocSubType}='html';
468 $self->{defaultEncoding}='iso-8859-1';
469  
470 if (defined(@_))
471 {
472 my %args=@_;
473 @$self{ keys %args }=values(%args);
474 }
475 }
476  
477 ##########################################################################
478 #
479 # Public methods
480 #
481 #########################################################################
482  
483 #
484 # Returns 1 if the (decimal) character code is legal UTF-8
485 #
486 sub code_is_utf8
487 {
488 my $self=shift;
489 my $dec_code=shift;
490  
491 # check for invalid codes
492 if ($dec_code<0 || $dec_code>1114111 || $InvalidUtf8Code{$dec_code})
493 {
494 return 0;
495 }
496  
497 return 1;
498 }
499  
500 #
501 # Returns 1 if all of the characters of the text are legal UTF-8
502 # Else, returns 0 and sets an error message specifying the location
503 # (1..) of the first illegal character code
504 # If you wish to obtain the position and offending code, pass a
505 # hash ref
506 #
507 sub is_utf8
508 {
509 my $self=shift;
510 my $text=shift;
511 my $err=shift;
512  
513 # Go over the text char by char and check for invalid char codes
514 my @chars=split(//,$text);
515 my $i=1;
516 for my $char (@chars)
517 {
518 # We test for valid code
519 #
520 my $code=ord($char);
521 if (!$self->code_is_utf8($code))
522 {
523 $self->_set_err_state($ERR_ILLEGAL_CODE,
524 sprintf("Position: #%d, character code: %#x",
525 $i,$code));
526 if (defined($err) && ref($err) eq 'HASH')
527 {
528 $err->{pos}=$i;
529 $err->{code}=$code;
530 }
531 return 0;
532 }
533 $i++;
534 }
535  
536 return 1;
537 }
538  
539 #
540 # type: The type of the document as one of the recognized types
541 # defined in Alvis::Document::Type (superset of MIME).
542 #
543 sub guess
544 {
545 my $self=shift;
546 my $text=shift;
547 my $type=shift;
548 my $sub_type=shift;
549  
550 $self->_set_err_state($ERR_OK); # clean the slate
551  
552 if (!defined($text))
553 {
554 $self->_set_err_state($ERR_DOC);
555 return undef;
556 }
557 if (!(defined($type) && defined($sub_type)))
558 {
559 ($type,$sub_type)=$self->{docTypeWizard}->guess($text);
560 if (!(defined($type) && defined($sub_type)))
561 {
562 $self->_set_err_state($ERR_TYPE_GUESS,
563 $self->{docTypeWizard}->errmsg());
564 return undef;
565 }
566 }
567  
568 # warn "($type,$sub_type)";
569  
570 my @guesses;
571 if ($type eq 'text')
572 {
573 if ($sub_type eq 'html')
574 {
575 @guesses=$self->_HTML($text);
576 if (scalar(@guesses)==0)
577 {
578 $self->_set_err_state($ERR_GUESS);
579 if (defined($self->{defaultEncoding}))
580 {
581 @guesses=($self->{defaultEncoding});
582 }
583 }
584 }
585 elsif ($sub_type eq 'xhtml')
586 {
587 @guesses=$self->_XHTML($text);
588 if (scalar(@guesses)==0)
589 {
590 $self->_set_err_state($ERR_GUESS);
591 if (defined($self->{defaultEncoding}))
592 {
593 @guesses=($self->{defaultEncoding});
594 }
595 }
596 }
597 elsif ($sub_type eq 'plain')
598 {
599 @guesses=$self->_plain_text($text);
600 if (scalar(@guesses)==0)
601 {
602 $self->_set_err_state($ERR_GUESS);
603 if (defined($self->{defaultEncoding}))
604 {
605 @guesses=($self->{defaultEncoding});
606 }
607 }
608 }
609 }
610  
611 return @guesses;
612 }
613  
614 sub from_to
615 {
616 my $self=shift;
617 my $text=shift;
618 my $source_enc=shift;
619 my $target_enc=shift;
620  
621 {
622 eval
623 {
624 Encode::from_to($text,
625 $source_enc,$target_enc,0);
626 # $source_enc,$target_enc,Encode::FB_QUIET);
627 };
628 if ($@)
629 {
630 my $err=$@;
631 $err=~s/ at .*$//isgo;
632 $self->_set_err_state($ERR_WRONG_GUESS,
633 "source encoding: $source_enc, " .
634 "target encoding: $target_enc. Why? $err.");
635 return undef;
636 }
637 if ($target_enc=~/^\s*utf-?8\s*$/isgo)
638 {
639 # leaves the bl***y UTF-8 flag on
640 Encode::_utf8_on($text);
641 }
642 }
643 return $text;
644 }
645  
646 #
647 # Should always leave the UTF-8 flag on, if target is UTF-8
648 #
649 sub convert
650 {
651 my $self=shift;
652 my $text=shift;
653 my $source_enc=shift;
654 my $target_enc=shift;
655  
656 my %err;
657 if ($source_enc=~/^\s*utf-?8\s*$/isgo)
658 {
659 if (!$self->is_utf8($text,\%err))
660 {
661 $self->_set_err_state($ERR_ILLEGAL_CHAR,
662 " Position: $err{pos}," .
663 "Code:$err{code}");
664 return undef;
665 }
666 }
667  
668 my $try=$self->from_to($text,$source_enc,$target_enc);
669 if (!defined($try))
670 {
671 my @possible_src_typo_fixes=$self->guess_typo_fixes($source_enc);
672 my @possible_trg_typo_fixes=$self->guess_typo_fixes($target_enc);
673  
674 for my $src_enc_guess ($self->guess_typo_fixes($source_enc))
675 {
676 for my $trg_enc_guess ($self->guess_typo_fixes($target_enc))
677 {
678 my $try=$self->from_to($text,$src_enc_guess,$trg_enc_guess);
679 if (defined($try))
680 {
681 return $try;
682 }
683 }
684 }
685
686 $self->_set_err_state($ERR_UNABLE_TO_GUESS);
687 return undef;
688 }
689  
690 return $try;
691 }
692  
693 sub guess_and_convert
694 {
695 my $self=shift;
696 my $text=shift;
697 my $type=shift;
698 my $sub_type=shift;
699 my $target_enc=shift;
700  
701 $self->_set_err_state($ERR_OK);
702  
703 if (!defined($text))
704 {
705 $self->_set_err_state($ERR_DOC);
706 return undef;
707 }
708 if (!(defined($type) && defined($sub_type)))
709 {
710 ($type,$sub_type)=$self->{docTypeWizard}->guess($text);
711 if (!(defined($type) && defined($sub_type)))
712 {
713 $self->_set_err_state($ERR_TYPE_GUESS,
714 $self->{docTypeWizard}->errmsg());
715 return undef;
716 }
717 }
718
719 my @enc_guesses=$self->guess($text,$type,$sub_type);
720 if (scalar(@enc_guesses)==0)
721 {
722 $self->_set_err_state($ERR_GUESS);
723 return undef;
724 }
725  
726 my $result;
727 for my $enc_guess (@enc_guesses)
728 {
729 if ( $target_enc eq "utf8" && ( $enc_guess =~ /utf-?8/i ) ) {
730 return $text;
731 } else {
732 $result=$self->convert($text,$enc_guess,$target_enc);
733 if (defined($result))
734 {
735 return $result;
736 }
737 }
738 }
739 if (!defined($result))
740 {
741 # test if its UTF-8 already
742 &Encode::_utf8_on($text);
743 if ( &Encode::is_utf8($text) ) {
744 return $text;
745 }
746 &Encode::_utf8_off($text);
747 $self->_set_err_state($ERR_GUESS_AND_CONVERT);
748 # print STDERR join("==", @enc_guesses) . " -> $target_enc : undef\n";
749 # print STDERR "\n$text\n\n";
750 return undef;
751 }
752  
753 return $result;
754 }
755  
756 sub guess_typo_fixes
757 {
758 my $self=shift;
759 my $typo=shift;
760  
761 my @possibilities=($typo);
762 if ($typo=~/^\s*(?:utf|uft)-?8\s*$/isgo)
763 {
764 push(@possibilities,'utf8');
765 }
766 if ($typo=~/^\s*(?:utf|uft)-16\s*$/isgo)
767 {
768 push(@possibilities,'UTF-16');
769 }
770 if ($typo=~/^\s*iso-?8559-?1\s*$/isgo)
771 {
772 push(@possibilities,'iso-8859-1');
773 }
774 if ($typo=~/^\s*ecu-?(kr|jp|cn|tw|jisx0213)\s*$/isgo)
775 {
776 push(@possibilities,"euc-$1");
777 }
778 if ($typo=~/^\s*(?:uft|utf)-?32\s*$/isgo)
779 {
780 push(@possibilities,'UTF-32');
781 }
782 if ($typo=~/^\s*(?:acsii|asici)\s*$/isgo)
783 {
784 push(@possibilities,'ascii');
785 }
786 if ($typo=~/^\s*(?:acsii|asici)-?ctrl\s*$/isgo)
787 {
788 push(@possibilities,'ascii-ctrl');
789 }
790 if ($typo=~/^\s*(?:utf|uft)-?7\s*$/isgo)
791 {
792 push(@possibilities,'UTF-7');
793 }
794 if ($typo=~/^\s*macintosh\s*$/isgo)
795 {
796 for (my $i=1; $i<=11; $i++)
797 {
798 push(@possibilities,"iso-8859-$i");
799 }
800 push(@possibilities,'viscii');
801 }
802 if ($typo=~/^\s*iso-8559-(\d)\s*$/isgo)
803 {
804 push(@possibilities,"iso-8859-$1");
805 }
806 if ($typo=~/^\s*iso-8895-(\d)\s*$/isgo)
807 {
808 push(@possibilities,"iso-8859-$1");
809 }
810 if ($typo=~/^\s*(?:utf|uft)-?16be\s*$/isgo)
811 {
812 push(@possibilities,'UTF-16BE');
813 }
814 if ($typo=~/^\s*(?:utf|uft)-?16le\s*$/isgo)
815 {
816 push(@possibilities,'UTF-16LE');
817 }
818  
819 return @possibilities;
820 }
821  
822 ########################################################################3
823 #
824 # Private methods
825 #
826 ##########################################################################
827 #
828 # HTML::Encoding has a nasty bug
829 #
830 sub _HTML
831 {
832 my $self=shift;
833 my $text=shift;
834  
835 if (!defined($text) || length($text)<1)
836 {
837 $self->_set_err_state($ERR_DOC);
838 return ();
839 }
840  
841 my @guesses;
842  
843 eval
844 {
845 @guesses=HTML::Encoding::encoding_from_byte_order_mark($text,xhtml=>0);
846 };
847 if ($@)
848 {
849 $self->_set_err_state($ERR_BOM,"$@");
850 }
851 if (scalar(@guesses))
852 {
853 return @guesses;
854 }
855  
856 # Sanity check to exclude e.g. UTF-32
857 #
858 eval
859 {
860 @guesses=
861 HTML::Encoding::encoding_from_first_chars($text);
862 };
863 if ($@)
864 {
865 $self->_set_err_state($ERR_FIRST_CHARS,"$@");
866 }
867  
868 my @tries;
869 if (scalar(@guesses))
870 {
871 @tries=@guesses;
872 }
873 else
874 {
875 @tries=@{$HTML::Encoding::DEFAULT_ENCODINGS};
876 }
877 foreach my $try (@tries)
878 {
879 if ($try=~/^\s*UTF-(?:16|32)((?:B|L)E)?\s*$/isgo)
880 {
881 # HTML::Encoding is a bit imperfect
882 next;
883 }
884  
885 my @try_results;
886 eval
887 {
888 @try_results=
889 HTML::Encoding::encoding_from_meta_element($text,$try);
890 };
891 if ($@)
892 {
893 $self->_set_err_state($ERR_META,"$@");
894 }
895
896 @guesses=(@try_results,@guesses);
897 }
898  
899 return @guesses;
900 }
901  
902 sub _XHTML
903 {
904 my $self=shift;
905 my $text=shift;
906  
907 if (!defined($text) || length($text)<1)
908 {
909 $self->_set_err_state($ERR_DOC);
910 return ();
911 }
912  
913 my @guesses;
914 eval
915 {
916 @guesses=HTML::Encoding::encoding_from_xml_document($text);
917 };
918 if ($@)
919 {
920 $self->_set_err_state($ERR_XML,"$@");
921 return ();
922 }
923
924 if (scalar(@guesses))
925 {
926 return @guesses;
927 }
928
929 return $self->_HTML($text);
930 }
931  
932 sub _plain_text
933 {
934 my $self=shift;
935 my $text=shift;
936  
937 if (!defined($text) || length($text)<1)
938 {
939 $self->_set_err_state($ERR_DOC);
940 return ();
941 }
942  
943 my $enc=guess_encoding($text);
944 if (ref($enc))
945 {
946 return ($enc->name());
947 }
948 else
949 {
950 $self->_set_err_state($ERR_ENCODE_GUESS,"$@");
951 return ();
952 }
953 }
954  
955 1;
956 __END__
« blib/lib/Alvis/HTML.pm ↑ Summary blib/lib/Alvis/Convert.pm »