KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > webEditor > interfaces > imageMode


1 /* $Id: imageMode.java,v 1.7 2001/04/25 22:17:15 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.interfaces;
16
17 import java.io.*;
18 import java.util.Hashtable JavaDoc;
19 import java.lang.reflect.*;
20
21 import javax.servlet.*;
22 import javax.servlet.http.*;
23 import org.w3c.dom.*;
24
25 import webEditor.core.*;
26
27 /**
28  * Image interface; This class manage the associated images edition
29  * @author <a HREF="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
30  */

31
32 public class imageMode {
33
34    /**
35     * Initial configuration values
36     */

37    private configuration initValues;
38
39    /**
40     * Name of the document to edit
41     */

42    private String JavaDoc myDocName;
43
44    /**
45     * Presentation management
46     */

47    private presentation myEditor= null;
48
49    /**
50     * Images management
51     */

52    private images myImage= null;
53
54    /**
55     * Articles management
56     */

57    private article myDocument= null;
58
59    /**
60     * Class constructor
61     * @param initParam Initial values
62     */

63     public imageMode (configuration initParam) {
64         // We are going to use the values in the hash table when we
65
// create another kind of core object
66
this.initValues = initParam;
67     }
68     
69    /**
70     * Interface for the edition operations
71     * @param request HTTP request
72     * @param out Servlet Output
73     * @return void
74     */

75     public void edition (HttpServletRequest request, PrintWriter out)
76     throws ServletException,
77            IOException {
78
79         String JavaDoc outputString = null;
80     this.myEditor = new presentation (this.initValues);
81     this.myDocument = new article (this.initValues);
82     this.myImage = new images (this.initValues);
83
84     this.myDocName = this.initValues.readValue("HTTP Parameters", "docID");
85     String JavaDoc opCode = this.initValues.readValue ("HTTP Parameters", "code");
86     
87     if (this.myDocName == null) {
88         // We are editing a new document
89
this.myDocName = "document.xml";
90     }
91     // We check the 'Write' param (it's an image form component)
92
String JavaDoc imgWrite = this.initValues.readValue
93                 ("HTTP Parameters", "Write.x");
94     if (imgWrite != null) {
95         opCode = "Write";
96     }
97
98     if (opCode != null) {
99 try {
100     // We use reflection to invoque the proper method
101
Method m = getClass().getMethod(opCode, new Class JavaDoc[0]);
102     m.invoke(this,new Object JavaDoc[0]);
103 }
104 catch (Exception JavaDoc e) {
105     out.println (opCode + " : Code not supported");
106     e.printStackTrace();
107 }
108     }
109
110     Document doc = this.myDocument.docRead (this.myDocName);
111     Document imgDoc = this.myDocument.readSubtree (doc, "image");
112     if (imgDoc == null) {
113         // The article doesn't have any image, so we read the
114
// "plain" image template
115
imgDoc = this.myDocument.docRead ("image.xml");
116     }
117     imgDoc = this.myImage.changeDocID (imgDoc, this.myDocName);
118     outputString = this.myEditor.showImgEditor (imgDoc , "");
119
120     out.println (outputString);
121     }
122
123    /**
124     * Modifies the given image
125     * @return void
126     */

127     public void Write ()
128     throws ServletException, IOException
129     {
130     Document doc = this.myDocument.docRead (this.myDocName);
131
132     // We load into the config hash the http parameters
133
Hashtable JavaDoc http_params = this.initValues.readCategory ("HTTP Parameters");
134     Document imageTemplate = this.myDocument.docRead ("image.xml");
135     // And then, we write the new http values ...
136
imageTemplate = this.myDocument.writeDoc (http_params, imageTemplate, "image_content");
137
138     imageTemplate = this.myImage.changeDocID (imageTemplate, this.myDocName);
139     // With this we move the physical image file to the article directory
140
String JavaDoc imagePath = this.myImage.writeImage (this.myDocName);
141     if ( imagePath != null ) {
142         imageTemplate = this.myImage.changeImageURL (imageTemplate, imagePath);
143     }
144     else {
145         imagePath = this.myImage.getImageURL (doc);
146         if (imagePath != null) {
147             //we try to restore the original image
148
imageTemplate = this.myImage.changeImageURL (imageTemplate, imagePath);
149         }
150     }
151
152     // Insert the new document into the article structure
153
String JavaDoc position = this.myImage.getImagePosition (imageTemplate);
154         
155     // Before inserting the image, we delete the previous image instances
156
String JavaDoc imagePosition = this.initValues.readValue
157                         ("HTTP Parameters", "position").trim();
158     if ( (!imagePosition.equals("")) && (!position.equals (imagePosition)) ) {
159         doc = this.myDocument.deleteImgDoc (doc, imagePosition);
160     }
161         
162     if ( !position.trim().equals("") ) {
163         // If imagePosition is empty, means that you try to insert a image without position
164
doc = this.myDocument.writeImgDoc (imageTemplate, doc, position);
165         this.myDocument.saveFile (this.myDocName, doc);
166     }
167     }
168
169    /**
170     * Deletes the given image
171     * @return void
172     */

173     public void delete ()
174     throws ServletException, IOException
175     {
176     String JavaDoc imagePosition = this.initValues.readValue
177                 ("HTTP Parameters", "position").trim();
178     if ( !imagePosition.equals("") ) {
179         Document doc = this.myDocument.docRead (this.myDocName);
180         doc = this.myDocument.deleteImgDoc (doc, imagePosition);
181         this.myDocument.saveFile (this.myDocName, doc);
182     }
183
184     }
185
186 }
187
Popular Tags