Coverage Report - net.wotonomy.web.WOImage
 
Classes in this File Line Coverage Branch Coverage Complexity
WOImage
0% 
0% 
2.2
 
 1  
 
 2  
 package net.wotonomy.web;
 3  
 
 4  
 import net.wotonomy.foundation.NSArray;
 5  
 import net.wotonomy.foundation.NSData;
 6  
 import net.wotonomy.foundation.NSDictionary;
 7  
 
 8  
 /**
 9  
 * WOImage renders a dynamically generated IMG tag. The URL for the image SRC can be
 10  
 * static, or it can be generated dynamically to return a NSData object (with a mime-type)
 11  
 * or even return the contents of a file (not implemented yet).
 12  
 *
 13  
 * Bindings are:
 14  
 * <UL><LI>src: A static URL for the image source.</li>
 15  
 * <li>data: A NSData object with the image content. Must be used with mimeType.</li>
 16  
 * <li>mimeType: The MIME type for the image data. Can be used with filename or data bindings.</li>
 17  
 * <li>filename: The path to a file containing an image.</li>
 18  
 * <li>framework: The optional framework from whence the image should be retrieved (used in conjunction with filename).</li>
 19  
 *
 20  
 * @author ezamudio@nasoft.com
 21  
 * @author $Author: cgruber $
 22  
 * @version $Revision: 905 $
 23  
 */
 24  
 public class WOImage extends WODynamicElement {
 25  
 
 26  
     protected String src;
 27  
     protected String filename;
 28  
     protected String framework;
 29  
     protected NSData data;
 30  
     protected String mimeType;
 31  
 
 32  
     protected WOImage() {
 33  0
         super();
 34  0
     }
 35  
 
 36  
     public WOImage(String aName, NSDictionary aMap, WOElement template) {
 37  0
         super(aName, aMap, template);
 38  0
     }
 39  
 
 40  
     public void setSrc(String value) {
 41  0
         src = value;
 42  0
     }
 43  
     public String src() {
 44  0
         return src;
 45  
     }
 46  
 
 47  
     public void setFilename(String value) {
 48  0
         filename = value;
 49  0
     }
 50  
 
 51  
     public String filename() {
 52  0
         return filename;
 53  
     }
 54  
 
 55  
     public void setFramework(String value) {
 56  0
         framework = value;
 57  0
     }
 58  
     public String framework() {
 59  0
         return framework;
 60  
     }
 61  
 
 62  
     public void setData(NSData value) {
 63  0
         data = value;
 64  0
     }
 65  
     public NSData data() {
 66  0
         return data;
 67  
     }
 68  
 
 69  
     public void setMimeType(String value) {
 70  0
         mimeType = value;
 71  0
     }
 72  
     public String mimeType() {
 73  0
         return mimeType();
 74  
     }
 75  
 
 76  
     public String sourceURL(WOContext c) {
 77  0
         if (associations.objectForKey("src") != null)
 78  0
             return (String)valueForProperty("src", c.component());
 79  0
         if (associations.objectForKey("data") != null) {
 80  0
             return c.componentActionURL();
 81  
         }
 82  0
         if (associations.objectForKey("filename") != null) {
 83  0
              WOComponent component = c.component();
 84  
 
 85  0
              String framework = stringForProperty("framework", component);
 86  0
              String filename = stringForProperty( "filename", component );
 87  0
              if ( filename != null && framework == null )
 88  
              {
 89  0
                  if ( filename.startsWith("/" ) )
 90  
                  {
 91  0
                      int i = filename.lastIndexOf( "/" );
 92  0
                      if ( i > 0 ) 
 93  
                      {
 94  0
                          framework = filename.substring( 0, i );
 95  0
                          if ( i < filename.length() )
 96  
                          {
 97  0
                              filename = filename.substring( i+1 );
 98  
                          }
 99  
                      }
 100  0
                  }
 101  
                  else
 102  
                  {
 103  
                      // just until we figure out how we're handling bundles/localization
 104  0
                      framework = component.frameworkName();
 105  0
                      if ( framework != null )
 106  
                      {
 107  0
                          framework = framework + '/' + component.name() + ".wo";
 108  0
                      }
 109  
                      else
 110  
                      {
 111  0
                          framework = '/' + component.name() + ".wo";
 112  
                      }
 113  
                  }
 114  
              }
 115  0
              return WOApplication.application().resourceManager().urlForResourceNamed(
 116  0
                  filename, framework, c.request().browserLanguages(), c.request() ); 
 117  
         }
 118  0
         return "NO SOURCE";
 119  
     }
 120  
     
 121  
     public void appendToResponse(WOResponse r, WOContext c) {
 122  0
         r.appendContentString("<IMG SRC=\"");
 123  0
         r.appendContentString(sourceURL(c));
 124  0
         r.appendContentString("\"");
 125  0
         r.appendContentString(additionalHTMLProperties(c.component(), new NSArray(new Object[]{
 126  0
             "src", "filename", "framework", "data", "mimeType" })));
 127  0
         r.appendContentString(">");
 128  0
     }
 129  
 
 130  
     public WOActionResults invokeAction(WORequest r, WOContext c) {
 131  0
         if (c.senderID().equals(c.elementID())) {
 132  0
             Object data = valueForProperty("data", c.component());
 133  0
             if (data instanceof byte[]) data = new NSData( (byte[]) data );
 134  0
             if (data instanceof NSData) {
 135  0
                 String mt = stringForProperty("mimeType", c.component());
 136  0
                 if (mt == null)
 137  0
                         throw new IllegalArgumentException("WOImage: No mimeType specified for data.");
 138  0
                 WOResponse img = new WOResponse();
 139  0
                 img.setContent((NSData)data);
 140  0
                 img.setHeader(mt, "content-type");
 141  0
                 return img;
 142  0
             } else if (filename() != null) {
 143  
                 //will this thing use frameworks, or regular JAR files with resources? 
 144  
                 //  mp: both/either, depending on which resource manager implementation 
 145  
             }
 146  
         }
 147  0
         return null;
 148  
     }
 149  
 
 150  
 }