# Copyright (c) 2006 bivio Software, Inc. All Rights Reserved. # # Visit http://www.bivio.biz for more info. # # This library is free software; you can redistribute it and/or modify # it under the terms of the GNU Lesser General Public License as # published by the Free Software Foundation; either version 2.1 of the # License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, but # WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; If not, you may get a copy from: # http://www.opensource.org/licenses/lgpl-license.html # # $Id: SyntacticString.pm,v 1.10 2010/11/16 03:56:21 nagler Exp $ package Bivio::Type::SyntacticString; use strict; use Bivio::Base 'Type.String'; our($VERSION) = sprintf('%d.%02d', q$Revision: 1.10 $ =~ /\d+/g); my($_TE) = b_use('Bivio.TypeError'); sub SYNTAX_ERROR { return $_TE->SYNTAX_ERROR; } sub TOO_LONG_ERROR { return $_TE->TOO_LONG; } sub TOO_SHORT_ERROR { return $_TE->TOO_SHORT; } sub from_literal { my($proto, $value) = @_; $value = $proto->internal_pre_from_literal($value) if defined($value); return (undef, undef) if !defined($value) || !length($value); return (undef, $proto->SYNTAX_ERROR) unless $value =~ /^@{[$proto->REGEX]}$/; my($v, $e) = $proto->internal_post_from_literal($value); return (undef, $e) if $e; return _length($proto, $v); } sub get_min_width { return 0; } sub get_width { return 100; } sub internal_post_from_literal { return $_[1]; } sub internal_pre_from_literal { my(undef, $value) = @_; $value =~ s/^\s+|\s+$//g; $value =~ s/\s+/ /g; return $value; } sub _length { my($proto, $value) = @_; return $value if ref($value); return (undef, $proto->TOO_SHORT_ERROR) if length($value) < $proto->get_min_width; return (undef, $proto->TOO_LONG_ERROR) if length($value) > $proto->get_width; return $value; } 1;