KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > core > images


1 /* $Id: images.java,v 1.4 2001/03/10 18:09:25 agarcia3 Exp $
2     webEditor. The new way in content management
3     Copyright (C) 2001 Alfredo Garcia
4
5     This program is free software; you can redistribute it and/or modify
6     it under the terms of the GNU General Public License as published by
7     the Free Software Foundation.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12     GNU General Public License for more details.
13 */

14
15 package webEditor.core;
16
17 import java.io.*;
18  
19 import javax.servlet.*;
20 import javax.servlet.http.*;
21 import org.apache.regexp.RE;
22 import org.w3c.dom.*;
23 import org.apache.xerces.parsers.DOMParser;
24 import org.apache.xml.serialize.*;
25
26 import webEditor.util.*;
27
28 /**
29  * Manage the graphic content of the news
30  *
31  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
32  */

33 public class images
34 {
35    /**
36     * Configuration values
37     */

38    private configuration initValues;
39
40    /**
41     * Editor root
42     */

43    private String JavaDoc wEd_root;
44
45    /**
46     * Path for the image documents in the web server
47     */

48    private String JavaDoc imgDir;
49
50    /**
51     * URL of the image documents
52     */

53    private String JavaDoc imgURL;
54
55    /**
56     * Path for the temporal documents
57     */

58    private String JavaDoc tmpDir;
59
60    public images (configuration initParam)
61    {
62     this.initValues = initParam;
63     String JavaDoc categoryName = "directories";
64
65     this.wEd_root = initParam.readValue ("webEditor_Root","wEd_root");
66     this.imgURL = "/" + initParam.readValue (categoryName,"imgDir");
67     this.imgDir = initParam.readValue (categoryName,"serverRoot") + this.imgURL;
68
69     this.tmpDir = initParam.readValue (categoryName,"tmpDir");
70    }
71
72    
73    /**
74     * Moves the upload file to the destination directory
75     * @param docID ID of the associated XML document
76     * @return String Complete path for the image
77     */

78    public String JavaDoc writeImage(String JavaDoc docID)
79    {
80     String JavaDoc imgPath = null;
81     String JavaDoc uploadImg = null;
82 try {
83     FileAccess fileHandler = new FileAccess ();
84
85     String JavaDoc filePath = this.imgDir + "/" + docID;
86     // Check if the directory structure exists...
87
fileHandler.createDocDir (docID, this.imgDir + "/");
88
89     // We are going to store the image with the same directory structure
90
// of the document
91
RE r = new RE("([:alnum:]*).xml$");
92     imgPath = r.subst (docID,"");
93
94     uploadImg = this.initValues.readValue ("HTTP Parameters", "art_image");
95     if ( uploadImg == null ) {
96         // Nothing downloaded...
97
return (null);
98     }
99
100     File uploadedFile = new File (this.tmpDir, uploadImg);
101     File destinationFile = new File (this.imgDir + "/" + imgPath, uploadImg);
102
103     uploadedFile.renameTo (destinationFile);
104 }
105 catch (Exception JavaDoc e) {
106     e.printStackTrace();
107 }
108
109     return (this.imgURL + "/" + imgPath + uploadImg);
110    }
111
112   /**
113     * Modifies the docID tag
114     * @param imageDoc DOM tree with the content of the image
115     * @param docID New document ID
116     * @return Document New DOM tree
117     */

118    public Document changeDocID (Document imageDoc, String JavaDoc docID)
119    {
120     Node sourceIDNode = (Node) imageDoc.getElementsByTagName("docID").item(0);
121
122     Node newIDNode = sourceIDNode.cloneNode(true);
123     newIDNode.getFirstChild().setNodeValue (docID);
124
125     Node parent = sourceIDNode.getParentNode ();
126     parent.replaceChild (newIDNode, sourceIDNode);
127     
128     return (imageDoc);
129    }
130
131    /**
132     * Reads the document information about image position
133     * @param imageDoc DOM tree with the content of the image
134     * @return String Image position
135     */

136    public String JavaDoc getImagePosition (Document imageDoc)
137    {
138     String JavaDoc position = null;
139
140     Node imagePosition = imageDoc.getElementsByTagName("art_image_position").item(0).getFirstChild();
141     if (imagePosition == null) {
142         return ("none");
143     }
144     else {
145         position = imagePosition.getNodeValue();
146     }
147     return (position);
148    }
149
150   /**
151     * Writes the full URL for the image file
152     * @param imageDoc DOM tree with the content of the image
153     * @param imgRL New image URL
154     * @return Document New DOM tree
155     */

156    public Document changeImageURL (Document imageDoc, String JavaDoc imgURL)
157    {
158     Node sourceIDNode = (Node) imageDoc.getElementsByTagName("art_image").item(0);
159
160     Node newIDNode = sourceIDNode.cloneNode(true);
161     newIDNode.getFirstChild().setNodeValue (imgURL);
162
163     Node parent = sourceIDNode.getParentNode ();
164     parent.replaceChild (newIDNode, sourceIDNode);
165     
166     return (imageDoc);
167    }
168
169   /**
170     * Reads the document information about image file URL
171     * @param imageDoc DOM tree with the content of the image
172     * @return String Image URL
173     */

174    public String JavaDoc getImageURL (Document imageDoc)
175    {
176     String JavaDoc url = null;
177
178     Node imageURL = imageDoc.getElementsByTagName("art_image").item(0).getFirstChild();
179     if (imageURL == null) {
180         return (null);
181     }
182     else {
183         url = imageURL.getNodeValue();
184     }
185     return (url);
186    }
187
188
189 }
190
Popular Tags