[Bins] Re: should not *require* each child element in an XML file

Martin Michlmayr tbm at cyrius.com
Fri Nov 4 18:59:32 GMT 2005


tags 308338 + patch pending
thanks

* Martin Michlmayr <tbm at cyrius.com> [2005-05-09 16:33]:
> The documentation says that you can create a image.jpg.xml with three
> child elements and something similar applies to album.xml.  I don't
> have to specify anything in <bins> and <exif>, I only want to fill in
> <description>.  However, BINS fails if you don't add these elements,
> even if they are just empty.  It would be nice if the program would
> not require that each of these elements are there.

Here's a patch for this.  It makes the whole XML handling a bit nicer:
it actually checks whether the XML files appear to be valid bins files
(i.e., contain <image> or <album> elements) - at the moment, you just
get a very cryptic error if this is not the case.  Additionally, all
elements are not optional.



(the large change at the end is only whitespace change)

--- /usr/bin/bins	2005-10-15 16:02:07.000000000 +0100
+++ bins	2005-11-04 18:54:34.000000000 +0000
@@ -3881,10 +3881,14 @@
     if (-e $descFile) {
         beVerboseN("  Reading desc file $descFile.", 3);
         $document = getXMLAsGrove($descFile);
-        %descHash   = getDescXML($document, $configHash);
-        #$descHash{descFileName} =  uri_escape($descFile, '^-A-Za-z0-9/_\.');
-        $configHash = getConfigXML($document, '/image/bins', $configHash);
-        %exifHash   = getExifXML($document, \@priorityList);
+        if (! $document->at_path("/image")) {
+            beVerboseN("Description file $descFile has no <image> element, ignoring.", 1);
+        } else {
+            %descHash   = getDescXML($document, $configHash) if $document->at_path("/image/description");
+            #$descHash{descFileName} =  uri_escape($descFile, '^-A-Za-z0-9/_\.');
+            $configHash = getConfigXML($document, '/image/bins', $configHash) if $document->at_path("/image/bins");
+            %exifHash   = getExifXML($document, \@priorityList) if $document->at_path("/image/exif");
+        }
     } else {
         $descHash{descFileName} =  "";
     }
@@ -4523,6 +4527,10 @@
     beVerboseN("Reading album description file '$descFile'...", 3);
 
     my $document = getXMLAsGrove($descFile);
+    if (! $document->at_path("/album")) {
+        beVerboseN("Description file $descFile has no <album> element, ignoring.", 1);
+        return $configHash;
+    }
     # I have to do that, don't ask me why...
 
     #$XML::UM::ENCDIR="/usr/lib/perl5/XML/Parser/";
@@ -4530,37 +4538,40 @@
     #             Encoding => 'ISO-8859-9',
     #             EncodeUnmapped => \&XML::UM::encode_unmapped_dec);
 
-    $configHash = getConfigXML($document, "/album/bins", $configHash);
+    $configHash = getConfigXML($document, "/album/bins", $configHash) if
+        $document->at_path("/album/bins");
 
-    foreach my $element
-        (@{$document->at_path('/album/description')->{Contents}}) {
-            if (UNIVERSAL::isa($element, 'XML::Grove::Element') && $element->{Name} eq "field") {
-                $fieldName = $element->{Attributes}{'name'};
-                $fieldValue = "";
-                if (grep (/^$fieldName$/, @AlbumFieldNames)) {
-                    beVerbose("    Reading field '$fieldName':", 3);
-                    foreach my $characters (@{$element->{Contents}}) {
-                        $fieldValue .= $characters->as_canon_xml();
-                    }
-                    #if ($fieldName ne "shortdesc" && $fieldName ne "longdesc"){
-                    #  $fieldValue = decode_entities($fieldValue);
-                    #}
-                    if ($fieldName eq "sampleimage"){
-                        $fieldValue =
-                            trimWhiteSpace(decode_entities($fieldValue));
-                        beVerbose("'".$fieldValue."'\n", 3);
-                    }else{
-                        $fieldValue =
-                            trimWhiteSpace(decode_entities(xml2html($fieldValue)));
-                        beVerbose("'".$fieldValue."'\n", 3);
+    if ($document->at_path("/album/description")) {
+        foreach my $element
+            (@{$document->at_path('/album/description')->{Contents}}) {
+                if (UNIVERSAL::isa($element, 'XML::Grove::Element') && $element->{Name} eq "field") {
+                    $fieldName = $element->{Attributes}{'name'};
+                    $fieldValue = "";
+                    if (grep (/^$fieldName$/, @AlbumFieldNames)) {
+                        beVerbose("    Reading field '$fieldName':", 3);
+                        foreach my $characters (@{$element->{Contents}}) {
+                            $fieldValue .= $characters->as_canon_xml();
+                        }
+                        #if ($fieldName ne "shortdesc" && $fieldName ne "longdesc"){
+                        #  $fieldValue = decode_entities($fieldValue);
+                        #}
+                        if ($fieldName eq "sampleimage"){
+                            $fieldValue =
+                                trimWhiteSpace(decode_entities($fieldValue));
+                            beVerbose("'".$fieldValue."'\n", 3);
+                        }else{
+                            $fieldValue =
+                                trimWhiteSpace(decode_entities(xml2html($fieldValue)));
+                            beVerbose("'".$fieldValue."'\n", 3);
+                        }
+                        #       $fieldValue = $encode->(trimWhiteSpace($fieldValue));
+                        $hash->{$fieldName} = $fieldValue;
+                    } else {
+                        beVerboseN("    Ignoring unknown field '$fieldName'.", 3);
                     }
-                    #       $fieldValue = $encode->(trimWhiteSpace($fieldValue));
-                    $hash->{$fieldName} = $fieldValue;
-                } else {
-                    beVerboseN("    Ignoring unknown field '$fieldName'.", 3);
                 }
             }
-        }
+    }
     return $configHash;
 }
 

-- 
Martin Michlmayr
http://www.cyrius.com/


More information about the Bins mailing list