alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*  $Id: imageMode.java,v 1.7 2001/04/25 22:17:15 agarcia3 Exp $ 
    webEditor. The new way in content management
    Copyright (C) 2001  Alfredo Garcia

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
*/

package webEditor.interfaces;

import java.io.*;
import java.util.Hashtable;
import java.lang.reflect.*;

import javax.servlet.*;
import javax.servlet.http.*;
import org.w3c.dom.*;

import webEditor.core.*;

/**
 * Image interface; This class manage the associated images edition
 * @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia
 */

public class imageMode {

   /**
    * Initial configuration values 
    */
   private configuration initValues;

   /**
    * Name of the document to edit
    */
   private String myDocName;

   /**
    * Presentation management
    */
   private presentation myEditor= null;

   /**
    * Images management
    */
   private images myImage= null;

   /**
    * Articles management
    */
   private article myDocument= null;

   /**
    * Class constructor
    * @param initParam		Initial values
    */
    public imageMode (configuration initParam) { 
    	// We are going to use the values in the hash table when we
	// create another kind of core object
   	this.initValues = initParam;
    }
    
   /**
    * Interface for the edition operations
    * @param request		HTTP request
    * @param out		Servlet Output
    * @return void
    */ 
    public void edition (HttpServletRequest request, PrintWriter out) 
    throws ServletException, 
    	   IOException {

    	String outputString = null;
	this.myEditor = new presentation (this.initValues);
	this.myDocument = new article (this.initValues);
	this.myImage = new images (this.initValues);

	this.myDocName = this.initValues.readValue("HTTP Parameters", "docID");
	String opCode = this.initValues.readValue ("HTTP Parameters", "code");
	
	if (this.myDocName == null) {
		// We are editing a new document
		this.myDocName = "document.xml";
	}
	// We check the 'Write' param (it's an image form component)
	String imgWrite = this.initValues.readValue 
				("HTTP Parameters", "Write.x");
	if (imgWrite != null)  {
		opCode = "Write";	
	}

	if (opCode != null) {
try { 
	// We use reflection to invoque the proper method
	Method m = getClass().getMethod(opCode, new Class[0]); 
	m.invoke(this,new Object[0]); 
} 
catch (Exception e) { 
	out.println (opCode + " : Code not supported");
	e.printStackTrace();
} 
	}

	Document doc = this.myDocument.docRead (this.myDocName);
	Document imgDoc = this.myDocument.readSubtree (doc, "image");
	if  (imgDoc == null) {
		// The article doesn't have any image, so we read the 
		// "plain" image template
		imgDoc = this.myDocument.docRead ("image.xml");
	}
	imgDoc = this.myImage.changeDocID (imgDoc, this.myDocName);
	outputString = this.myEditor.showImgEditor (imgDoc , "");

	out.println (outputString);
    }

   /**
    * Modifies the given image
    * @return void
    */
    public void Write ()
	throws ServletException, IOException 
    {
	Document doc = this.myDocument.docRead (this.myDocName);

	// We load into the config hash the http parameters
	Hashtable http_params = this.initValues.readCategory ("HTTP Parameters");
	Document imageTemplate = this.myDocument.docRead ("image.xml");
	// And then, we write the new http values ...
	imageTemplate = this.myDocument.writeDoc (http_params, imageTemplate, "image_content");

	imageTemplate = this.myImage.changeDocID (imageTemplate, this.myDocName);
	// With this we move the physical image file to the article directory
	String imagePath = this.myImage.writeImage (this.myDocName);
	if ( imagePath != null ) {
		imageTemplate = this.myImage.changeImageURL (imageTemplate, imagePath);
	}
	else {
		imagePath = this.myImage.getImageURL (doc);
		if (imagePath != null) {
			//we try to restore the original image
			imageTemplate = this.myImage.changeImageURL (imageTemplate, imagePath);
		}
	}

	// Insert the new document into the article structure
	String position = this.myImage.getImagePosition (imageTemplate);
		
	// Before inserting the image, we delete the previous image instances
	String imagePosition = this.initValues.readValue 
						("HTTP Parameters", "position").trim();
	if ( (!imagePosition.equals("")) && (!position.equals (imagePosition)) ) {
		doc = this.myDocument.deleteImgDoc (doc, imagePosition);
	}
		
	if ( !position.trim().equals("") ) {
		// If imagePosition is empty, means that you try to insert a image without position
		doc = this.myDocument.writeImgDoc (imageTemplate, doc, position);
		this.myDocument.saveFile (this.myDocName, doc);
	}
    }

   /**
    * Deletes the given image
    * @return void
    */
    public void delete ()
	throws ServletException, IOException 
    {
	String imagePosition = this.initValues.readValue 
				("HTTP Parameters", "position").trim();
	if ( !imagePosition.equals("") ) {
		Document doc = this.myDocument.docRead (this.myDocName);
		doc = this.myDocument.deleteImgDoc (doc, imagePosition);
		this.myDocument.saveFile (this.myDocName, doc);
	}

    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.