- Timestamp:
- 11/30/09 22:16:31 (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Padre-Plugin-wxGlade/lib/Padre/Plugin/wxGlade/WXG.pm
r9353 r9355 3 3 use strict; 4 4 use warnings; 5 use Params::Util qw{ _STRING _HASH }; 5 6 # use XML::Tiny (); 6 7 7 8 our $VERSION = '0.01'; 9 10 11 12 13 14 ###################################################################### 15 # Constructor and Accessors 8 16 9 17 sub new { … … 15 23 my $document = XML::Tiny::parsefile( $file ); 16 24 17 # Extract application properties 18 my $application = $document->[0]; 25 # Validate the file 26 unless ( _HASH($document->[0]) and $document->[0]->{name} eq 'application' ) { 27 die("Invalid or unsupported wxGlade XML"); 28 } 19 29 20 30 # Create the WXG object 21 my $self = bless { }, $class; 31 my $self = $document->[0]; 32 bless $self, $class; 33 34 # Save the path to the wxg file 35 $self->{wxg} = $file; 22 36 23 37 return $self; 24 38 } 25 39 40 sub wxg { 41 $_[0]->{wxg}; 42 } 43 44 sub language { 45 $_[0]->{attrib}->{language}; 46 } 47 48 sub for_version { 49 $_[0]->{attrib}->{for_version}; 50 } 51 52 sub path { 53 $_[0]->{attrib}->{path}; 54 } 55 56 # Smarter equivalent for path 57 sub file { 58 my $self = shift; 59 60 # Handle null cases 61 unless ( defined _STRING($self->path) ) { 62 return $self->path; 63 } 64 65 # Handle the trivial positive case 66 if ( -f $self->path ) { 67 return $self->path; 68 } 69 70 # Because wxGlade saves absolute paths, they don't transport well. 71 # If the literal path doesn't exist, add support for the generated 72 # file being in the same directory as the WXG file itself. 73 die( "CODE INCOMPLETE" ); 74 } 75 76 # Valid usable top level objects 77 sub objects { 78 grep { 79 $_->{type} eq 'e' 80 and 81 $_->{name} eq 'object' 82 and 83 $_->{attrib}->{name} 84 and 85 $_->{attrib}->{class} 86 } @{$_[0]->{content}}; 87 } 88 89 # The list of top level window names in the application 90 sub windows { 91 grep { 92 defined _STRING($_) 93 } map { 94 $_->{attrib}->{name} 95 } $_[0]->objects 96 } 97 98 # Fetch a window tag 99 sub window { 100 my $self = shift; 101 my $name = shift; 102 foreach ( $self->objects ) { 103 next unless $_->{attrib}->{name} eq $name; 104 return $_; 105 } 106 return; 107 } 108 109 # Fetch the top window 110 sub top_window { 111 my $self = shift; 112 $self->window( $self->{attrib}->{top_window} ); 113 } 114 115 116 117 118 119 ###################################################################### 120 # Main Methods 121 122 sub supported { 123 my $self = shift; 124 unless ( $self->language and $self->language eq 'perl' ) { 125 die("The wxGlade application is not targetting Perl"); 126 } 127 unless ( $self->for_version and $self->for_version eq '2.8' ) { 128 die("The wxGlade application is not build for wxGlade 2.8"); 129 } 130 my $path = $self->path; 131 unless ( $path and -f $path ) { 132 die("The wxGlade output path '$path' does not exist"); 133 } 134 return 1; 135 } 136 137 # Loads the Perl code for a single named window 138 sub extract { 139 my $self = shift; 140 my $window = shift; 141 142 # Load the Perl file and localize newlines 143 my $file = $self->file; 144 my $perl = _lslurp($file); 145 146 # Extract the class from the overall file 147 my $package = $window->{attrib}->{class}; 148 my @code = $$perl =~ /\n(package $package;.+?# end of class $package\n+1;\n)/sg; 149 unless ( @code ) { 150 die("Failed to find package '$package' in file '$file'"); 151 } 152 unless ( @code == 1 ) { 153 die("Found more than one package '$package' in file '$file'"); 154 } 155 156 return $code[0]; 157 } 158 159 # Provide a simple _slurp implementation (copied from PPI::Util) 160 # Avoids a 1 meg File::Slurp load. 161 sub _lslurp { 162 my $file = shift; 163 local $/ = undef; 164 local *FILE; 165 open( FILE, '<', $file ) or die("open($file) failed: $!"); 166 my $source = <FILE>; 167 close( FILE ) or die("close($file) failed: $!"); 168 $source =~ s/(?:\015{1,2}\012|\015|\012)/\n/sg; 169 return \$source; 170 } 171 26 172 1;
Note: See TracChangeset
for help on using the changeset viewer.
