Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gov/lanl/COAS/URI2Blob.java


1   /*--- formatted by Jindent 2.1, (www.c-lab.de/~jindent) ---*/
2   
3   // URI2Blob.java
4   
5   /**
6    * *******************
7    * Copyright Notice
8    * Copyright (c) 1999, Regents of the University of California. All rights reserved.
9    * 
10   * DISCLAIMER
11   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS
12   * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
13   * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
14   * SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
15   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
16   * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
17   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
18   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
19   * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
20   * DAMAGE.
21   * ************************************
22   */
23  package gov.lanl.COAS;
24  
25  import org.apache.log4j.Logger;
26  import java.util.Properties;
27  import java.io.*;
28  import java.net.*;
29  import org.xml.sax.SAXException;
30  import org.w3c.dom.*;
31  import org.apache.xerces.parsers.DOMParser;
32  import gov.lanl.Utility.ConfigProperties;
33  import org.omg.DsObservationAccess.*;
34  import org.omg.DsObservationValue.*;
35  
36  // import org.xbill.DNS.utils.base64;
37  import gov.lanl.COAS.MultimediaIteratorImpl;
38  
39  /**
40   * This class traverses an ObservationDataStruct tree, searches for URIs in Images
41   * and converts these URIs into Base64 decoded blobs.
42   * 
43   * Looks for 'DNS:telemed.lanl.gov/TraitCode/URI' code as a subtree of
44   * 'DNS:telemed.lanl.gov/TraitCode/ImageStudy/ImageSeries/Image/FullImageData' and
45   * 'DNS:telemed.lanl.gov/TraitCode/ImageStudy/ImageSeries/Image/ThumbImageData'
46   * reads in the specified file by the URI, converts it into a blob and
47   * replaces it with Multimedia 'DNS:telemed.lanl.gov/TraitCode/Multimedia'.
48   * 
49   * @author Sascha A. Koenig
50   * @version %I%, %G%
51   */
52  public class URI2Blob {
53    private static Logger cat = Logger.getLogger(URI2Blob.class.getName());
54  
55    // the name of the class
56    // private static final String cn = "URI2Blob: ";
57  
58    /**
59     * an orb for creating Anys
60     */
61    private org.omg.CORBA.ORB orb;
62  
63    /**
64     * an array of observation data to operate on
65     */
66    private ObservationDataStruct[] obsDataSeq;
67  
68    /**
69     * Constructor declaration
70     * 
71     * @param obsData the observation data, whose URIs should be converted into Blobs
72     * @deprecated
73     */
74    public URI2Blob(ObservationDataStruct obsData, org.omg.CORBA.ORB the_orb) {
75      orb = the_orb;
76      obsDataSeq = new ObservationDataStruct[1];
77      obsDataSeq[0] = obsData;
78    }
79  
80    /**
81     * Constructor declaration
82     * 
83     * 
84     * @param obsDataSeq
85     * 
86     * @see
87     */
88    public URI2Blob(ObservationDataStruct[] obsDataSeq, org.omg.CORBA.ORB the_orb) {
89      orb = the_orb;
90      this.obsDataSeq = obsDataSeq;
91    }
92  
93    /**
94     * starts the conversion and returns an observation data object
95     * 
96     * @return the converted observation data object
97     */
98    public ObservationDataStruct getObservationData() {
99  
100     // traverse the tree
101     findNode(obsDataSeq[0], 
102          "DNS:telemed.lanl.gov/TraitCode/ImageStudy/ImageSeries/Image/FullImageData");
103     findNode(obsDataSeq[0], 
104          "DNS:telemed.lanl.gov/TraitCode/ImageStudy/ImageSeries/Image/ThumbImageData");
105 
106     return obsDataSeq[0];
107   } 
108 
109   /**
110    * starts the conversion and returns a sequence of observation data objects
111    */
112   public ObservationDataStruct[] getObservationDataSeq() {
113     for (int i = 0; i < obsDataSeq.length; i++) {
114 
115       // traverse the tree
116       findNode(obsDataSeq[i], 
117            "DNS:telemed.lanl.gov/TraitCode/ImageStudy/ImageSeries/Image/FullImageData");
118       findNode(obsDataSeq[i], 
119            "DNS:telemed.lanl.gov/TraitCode/ImageStudy/ImageSeries/Image/ThumbImageData");
120     } 
121 
122     return obsDataSeq;
123   } 
124 
125   /**
126    * internal method for finding the a specific node
127    * 
128    * @param obsData
129    * @param qualCodeStr
130    */
131   void findNode(ObservationDataStruct obsData, String qualCodeStr) {
132     if (!obsData.code.equals(qualCodeStr)) {
133       for (int i = 0; i < obsData.composite.length; i++) {
134         findNode(obsData.composite[i], qualCodeStr);
135       } 
136     } else {
137       for (int i = 0; i < obsData.composite.length; i++) {
138         if (obsData.composite[i].code.equals("DNS:telemed.lanl.gov/TraitCode/URI")) {
139           ObservationDataStruct multimediaNode = 
140             getMultimediaObsData(obsData.composite[i]);
141 
142           obsData.composite = new ObservationDataStruct[1];
143           obsData.composite[0] = multimediaNode;
144 
145           break;
146         } 
147       } 
148     } 
149   } 
150 
151   /**
152    * internal method for creating a new multimedia object
153    * 
154    * @param uriNode
155    */
156   ObservationDataStruct getMultimediaObsData(ObservationDataStruct uriNode) {
157     UniversalResourceIdentifier uri = 
158       UniversalResourceIdentifierHelper.extract(uriNode.value[0]);
159     String location = uri.address;
160 
161     try {
162       URL url = new URL(location);
163       InputStream inputStream = new BufferedInputStream(url.openStream());
164       int len = inputStream.available();
165       byte[] bytes = new byte[len];
166 
167       inputStream.read(bytes, 0, len);
168       inputStream.close();
169 
170       // use the file extension as other_mime_header_field
171       String filename = url.getFile();
172       int pos = filename.lastIndexOf(".");
173       String ext;
174 
175       if (pos > 0) {
176         ext = filename.substring(pos + 1);
177       } else {
178         ext = "";
179       } 
180 
181       Multimedia multimedia = new Multimedia("image", ext, bytes, len, null);
182 
183       // (new gov.lanl.COAS.MultimediaIteratorImpl())._this());
184       org.omg.CORBA.Any[] any = {
185         orb.create_any()
186       };
187 
188       MultimediaHelper.insert(any[0], multimedia);
189 
190       ObservationDataStruct multimediaNode = 
191         new ObservationDataStruct("DNS:telemed.lanl.gov/TraitCode/Multimedia", 
192                       new ObservationDataStruct[0], 
193                       new ObservationDataStruct[0], any);
194 
195       return multimediaNode;
196     } catch (java.io.IOException e) {
197       cat.error("Can't open URL " + location + " ; error " + e);
198 
199       return uriNode;
200     } 
201   } 
202 
203 }
204 
205 
206 
207 /*--- formatting done in "OpenEMed Convention" style on 10-18-2001 ---*/
208