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: images.java,v 1.4 2001/03/10 18:09:25 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.core;

import java.io.*;
 
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.regexp.RE;
import org.w3c.dom.*;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.*;

import webEditor.util.*;

/**
 * Manage the graphic content of the news 
 * 
 *  @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia
 */
public class images 
{
   /**
    * Configuration values
    */
   private configuration initValues;

   /**
    * Editor root 
    */
   private String wEd_root;

   /**
    * Path for the image documents in the web server    
    */
   private String imgDir;

   /**
    * URL of the image documents
    */
   private String imgURL;

   /**
    * Path for the temporal documents
    */
   private String tmpDir;

   public images (configuration initParam) 
   {
   	this.initValues = initParam;
	String categoryName = "directories";

	this.wEd_root   = initParam.readValue ("webEditor_Root","wEd_root");
	this.imgURL    = "/" + initParam.readValue (categoryName,"imgDir");
	this.imgDir	= initParam.readValue (categoryName,"serverRoot") + this.imgURL;

	this.tmpDir	= initParam.readValue (categoryName,"tmpDir");
   }

   
   /**
    * Moves the upload file to the destination directory
    * @param docID 		ID of the associated XML document
    * @return String		Complete path for the image
    */
   public String writeImage(String docID) 
   {
   	String imgPath = null;
   	String uploadImg = null;
try {
	FileAccess  fileHandler = new FileAccess ();

	String filePath = this.imgDir + "/" + docID;
	// Check if the directory structure exists...
	fileHandler.createDocDir (docID, this.imgDir + "/");

	// We are going to store the image with the same directory structure
	// of the document
	RE r = new RE("([:alnum:]*).xml$");
	imgPath = r.subst (docID,"");

	uploadImg = this.initValues.readValue ("HTTP Parameters", "art_image");
	if ( uploadImg == null ) {
		// Nothing downloaded...
		return (null);
	}

	File uploadedFile = new File (this.tmpDir, uploadImg);
	File destinationFile = new File (this.imgDir + "/" + imgPath, uploadImg);

	uploadedFile.renameTo (destinationFile);
}
catch (Exception e) {
	e.printStackTrace();
}

	return (this.imgURL + "/" + imgPath + uploadImg);
   }

  /**
    * Modifies the docID tag
    * @param imageDoc		DOM tree with the content of the image
    * @param docID		New document ID
    * @return Document		New DOM tree
    */
   public Document changeDocID (Document imageDoc, String docID) 
   {
	Node sourceIDNode = (Node) imageDoc.getElementsByTagName("docID").item(0);

	Node newIDNode = sourceIDNode.cloneNode(true);
	newIDNode.getFirstChild().setNodeValue (docID);

	Node parent = sourceIDNode.getParentNode ();
	parent.replaceChild (newIDNode, sourceIDNode);	
	
   	return (imageDoc);
   }

   /**
    * Reads the document information about image position
    * @param imageDoc		DOM tree with the content of the image
    * @return String		Image position
    */
   public String getImagePosition (Document imageDoc) 
   {
   	String position = null;

	Node imagePosition = imageDoc.getElementsByTagName("art_image_position").item(0).getFirstChild();
	if (imagePosition == null) {
		return ("none");
	}
	else {
		position = imagePosition.getNodeValue();
	}
   	return (position);
   }

  /**
    * Writes the full URL for the image file
    * @param imageDoc		DOM tree with the content of the image
    * @param imgRL		New image URL
    * @return Document		New DOM tree
    */
   public Document changeImageURL (Document imageDoc, String imgURL) 
   {
	Node sourceIDNode = (Node) imageDoc.getElementsByTagName("art_image").item(0);

	Node newIDNode = sourceIDNode.cloneNode(true);
	newIDNode.getFirstChild().setNodeValue (imgURL);

	Node parent = sourceIDNode.getParentNode ();
	parent.replaceChild (newIDNode, sourceIDNode);	
	
   	return (imageDoc);
   }

  /**
    * Reads the document information about image file URL
    * @param imageDoc		DOM tree with the content of the image
    * @return String		Image URL
    */
   public String getImageURL (Document imageDoc) 
   {
   	String url = null;

	Node imageURL = imageDoc.getElementsByTagName("art_image").item(0).getFirstChild();
	if (imageURL == null) {
		return (null);
	}
	else {
		url = imageURL.getNodeValue();
	}
   	return (url);
   }


}
... 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.