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: page.java,v 1.3 2001/04/20 21:09:09 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 java.util.*;

import org.w3c.dom.*;
import org.apache.xerces.dom.*;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.*;

import webEditor.util.FileAccess;

/**
 * Home Page content operations
 *
 * @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia
 */
public class page 
{
   /**
    * Config Parameters
    */
   private configuration configParams;

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

   /**
    * Path for the data configuration files
    */
   private String dataDir;
   
   /**
    * Home Page file
    */
   private String homePageFile;

  /**
    * Server path for the exported documents
    */
   private String docRoot;


   public page (configuration initParam) 
   {
   	this.configParams = initParam;
	String categoryName = "directories";

	this.wEd_root   = initParam.readValue ("webEditor_Root","wEd_root");
	this.dataDir    = initParam.readValue (categoryName,"dataDir");
	this.dataDir    = this.wEd_root + "/" + this.dataDir;

	this.homePageFile    = initParam.readValue (categoryName,"homePage");

	this.docRoot  = "/" + initParam.readValue (categoryName,"docRoot");
	this.docRoot  = initParam.readValue (categoryName,"serverRoot") + this.docRoot;

   }
   
   
  /**
    * Expands the content of the home page into a master document
    * @return Document	Expanded Home Page
    */
   public Document homePageList () 
   {
	Element item;
	DocumentImpl expandedDoc = null;
	article myPage = new article (this.configParams);

	DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile);
	// Once we have the document DOM tree, whe look for the file content
try {
	Element root = doc.getDocumentElement();
	String rootTag = root.getTagName();
	NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();

	// We are going to store in expandedDoc the content of all the news in
	// the homepage. For this, we create a document of the same type
	DocumentType docType = doc.getDoctype();
	expandedDoc = new DocumentImpl (docType);
	Element expandedRoot = expandedDoc.createElement (rootTag); 	
	String size = root.getAttributes().getNamedItem("size").getNodeValue();
	expandedRoot.setAttribute ("size", size);

	// For every child add the codes and the words
	for (int i=0; i < list.getLength(); i++) {
		Node myNode = list.item(i);
		if (myNode.getNodeName().equals("document")) {
			String order = myNode.getAttributes().getNamedItem("order").getNodeValue();
			String newsFile = myNode.getFirstChild().getNodeValue();
			
			// For every document in the home page, we read the file content
			Document newsDoc = myPage.docRead (newsFile);
			item = expandedDoc.createElement ("docContent");
			item.setAttribute ("order", order);

			item.appendChild (expandedDoc.importNode (newsDoc.getDocumentElement(), true));
			expandedRoot.appendChild (item);
        	}   
      	} 
	expandedDoc.appendChild (expandedRoot);	
}
catch (Exception e) {
	e.printStackTrace ();
}
	return (expandedDoc);
   }


   /**
    * Insert the given document on the top of the Home Page
    * @param docName	doc ID to insert
    * @return void
    */
   public void insertDoc (String docName) 
   {
   	Element item;
   	String intValue = null;
	article myPage = new article (this.configParams);

	DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile);
	// Once we have the document DOM tree, whe look for the file content
try {
	Element root = doc.getDocumentElement();
	String rootTag = root.getTagName();
	// We create a new element with the "docName" value and order 1
	item = doc.createElement ("document");
	item.setAttribute ("order", "1");
	item.appendChild (doc.createTextNode(docName));
	// And then, we try to insert this element as the first child of the root
	Node firstChild = doc.getElementsByTagName(rootTag).item(0).getFirstChild();
	root.insertBefore (item, firstChild);	

	NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();

	// Ok, now that we have the new node in the first place, we just need to 
	// change the order attribute on the rest of the nodes
	int j = 1;
	for (int i=0; i < list.getLength(); i++) {
		Node myNode = list.item(i);
		if (myNode.getNodeName().equals("document")) {
			myNode.getAttributes().getNamedItem("order").setNodeValue(intValue.valueOf(j));

			String order = myNode.getAttributes().getNamedItem("order").getNodeValue();
			String newsFile = myNode.getFirstChild().getNodeValue();
			j++;
		}
	}
	// Now that we have the new index master document, we write it to disk
	String size = root.getAttributes().getNamedItem("size").getNodeValue();
	Integer intSize = new Integer (size);
	int mySize = intSize.intValue() + 1;
	size = size.valueOf(mySize);
	root.setAttribute ("size", size);
	myPage.saveFile (this.homePageFile, doc);

}
catch (Exception e) {
	e.printStackTrace ();
}

  }


  /**
    * Delete the given document from the Home Page
    * @param docName	doc ID to delete
    * @return void
    */
   public void removeDoc (String docName) 
   {
   	Element item;
	DocumentImpl resultDoc = null;
   	String intValue = null;
	article myPage = new article (this.configParams);

	DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile);
	// Once we have the document DOM tree, whe look for the file content
try {
	Element root = doc.getDocumentElement();
	String rootTag = root.getTagName();
	NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();

	// We are going to store in resultDoc all the docs, but the given doc.
	DocumentType docType = doc.getDoctype();
	resultDoc = new DocumentImpl (docType);
	Element resultRoot = resultDoc.createElement (rootTag); 	

	int j = 1;
	boolean find = false;
	// We copy the old structure until we find the given doc.
	for (int i=0; i < list.getLength(); i++) {
		Node myNode = list.item(i);
		if (myNode.getNodeName().equals("document")) {
			String newsFile = myNode.getFirstChild().getNodeValue();
			
			if ( newsFile.equals (docName) ) {
				// So we simply go to the next iteration
				find = true;
				continue;
			}
			else {
				item = resultDoc.createElement ("document");
				item.setAttribute ("order", intValue.valueOf(j));
				item.appendChild (resultDoc.createTextNode(newsFile));
				resultRoot.appendChild (item);
			   	j++;
			}
		}
	}
	// Now that we have the new index master document, we write it to disk
	if ( find ) {
		String size = root.getAttributes().getNamedItem("size").getNodeValue();
		Integer intSize = new Integer (size);
		int mySize = intSize.intValue() - 1;
		size = size.valueOf(mySize);
		resultRoot.setAttribute ("size", size);

		resultDoc.appendChild (resultRoot);	
		myPage.saveFile (this.homePageFile, resultDoc);
	}
}
catch (Exception e) {
	e.printStackTrace ();
}

  }

  /**
    * Promotes the given document one position into the Home Page
    * @param docName	doc ID to promote
    * @return void
    */
   public void riseDoc (String docName) 
   {
   	Element item;
   	Element storedItem = null;
	DocumentImpl resultDoc = null;
   	String intValue = null;
	article myPage = new article (this.configParams);

	DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile);
	// Once we have the document DOM tree, whe look for the file content
try {
	Element root = doc.getDocumentElement();
	String rootTag = root.getTagName();
	NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();

	// We are going to store in resultDoc the new document order.
	DocumentType docType = doc.getDoctype();
	resultDoc = new DocumentImpl (docType);
	Element resultRoot = resultDoc.createElement (rootTag); 	
	// Oh, yes, don't forget the "size" attribute
	String size = root.getAttributes().getNamedItem("size").getNodeValue();
	resultRoot.setAttribute ("size", size);


	// Main loop
	int j=1;
	boolean find = false;
	for (int i = 0; i < list.getLength(); i++) {
		Node myNode = list.item(i);
		if (myNode.getNodeName().equals("document")) {
			String newsFile = myNode.getFirstChild().getNodeValue();
			String order = myNode.getAttributes().getNamedItem("order").getNodeValue();
			if ( !docName.equals (newsFile) ) {
			   // We copy this node
			   item = resultDoc.createElement ("document");
			   item.setAttribute ("order", order);
			   item.appendChild (resultDoc.createTextNode(newsFile));
			   resultRoot.appendChild (item);
			}
			else {
			   if ( j == 1 ) {
			   	// Nothing to do (we are in the first doc)
				break;
			   }
			   else {
			   	// So we have found the given doc, and it's not the
				// first... Great! at last real work ;-)
			   	item = resultDoc.createElement ("document");
			   	item.setAttribute ("order", intValue.valueOf(j-1));
			   	item.appendChild (resultDoc.createTextNode(newsFile));

				Node prevNode = resultRoot.getLastChild();
			   	resultRoot.insertBefore (item, prevNode);

				// And then, we replace the previous node with the new order
				String file = prevNode.getFirstChild().getNodeValue();
			   	item = resultDoc.createElement ("document");
			   	item.setAttribute ("order", intValue.valueOf(j));
			   	item.appendChild (resultDoc.createTextNode(file));

			   	resultRoot.replaceChild (item, prevNode);
				find = true;
			   }
			}
			j++;
		}
	}

	
	// Now that we have the new index master document, we write it to disk
	if ( find ) {
		resultDoc.appendChild (resultRoot);	
		myPage.saveFile (this.homePageFile, resultDoc);
	}
}
catch (Exception e) {
	e.printStackTrace ();
}

  }

  /**
    * Lowers the given document one position into the Home Page
    * @param docName	doc ID to lower
    * @return void
    */
   public void lowerDoc (String docName) 
   {
   	Element item;
   	Element storedItem = null;
	DocumentImpl resultDoc = null;
   	String intValue = null;
	article myPage = new article (this.configParams);

	DocumentImpl doc = (DocumentImpl) myPage.docRead (this.homePageFile);
	// Once we have the document DOM tree, whe look for the file content
try {
	Element root = doc.getDocumentElement();
	String rootTag = root.getTagName();
	NodeList list = doc.getElementsByTagName(rootTag).item(0).getChildNodes();

	// We are going to store in resultDoc the new document order.
	DocumentType docType = doc.getDoctype();
	resultDoc = new DocumentImpl (docType);
	Element resultRoot = resultDoc.createElement (rootTag); 	
	// Oh, yes, don't forget the "size" attribute
	String size = root.getAttributes().getNamedItem("size").getNodeValue();
	resultRoot.setAttribute ("size", size);

	Integer IntSize = new Integer (size);
	int intSize = IntSize.intValue();
	
	// Main loop
	int j=1;
	boolean find = false;
	boolean write = false;
	Element prevNode = null;
	for (int i = 0; i < list.getLength(); i++) {
		Node myNode = list.item(i);
		if (myNode.getNodeName().equals("document")) {
			String newsFile = myNode.getFirstChild().getNodeValue();
			String order = myNode.getAttributes().getNamedItem("order").getNodeValue();
			if ( !docName.equals (newsFile) ) {
			   if ( find ) {
				order = intValue.valueOf(j-1);
			   }
			   // We copy this node
			   item = resultDoc.createElement ("document");
			   item.setAttribute ("order", order);
			   item.appendChild (resultDoc.createTextNode(newsFile));
			   resultRoot.appendChild (item);
			   if ( find ) {
			   	// We do this, because we want to preserve the order secuence
			   	resultRoot.appendChild (prevNode);
				find = false;
			   }
			}
			else {
			   if ( j == intSize ) {
		   	      // Ok, we are in the last doc, so we have nothing to do
			      break;
			   }
			   else {
			   	// We have found the given doc
			   	prevNode = resultDoc.createElement ("document");
			   	prevNode.setAttribute ("order", intValue.valueOf(j+1));
			   	prevNode.appendChild (resultDoc.createTextNode(newsFile));

				write = true;
				find = true;
			   }
			}
			j++;
		}
	}

	
	// Now that we have the new index master document, we write it to disk
	if ( write ) {
		resultDoc.appendChild (resultRoot);	
		myPage.saveFile (this.homePageFile, resultDoc);
	}
}
catch (Exception e) {
	e.printStackTrace ();
}

  }

  /**
    * Saves the string with the HTML content into a file 
    * @param content	String with the HTML home page
    * @return void
    */
   public void saveHtmlFile (String content) 
   {
try {
	FileAccess  fileHandler = new FileAccess ();

	String filePath = this.docRoot + "/index.html";
	// We need to delete the file before the write operation
	File fd = new File (filePath);	
	if ( fd.exists() ) {
		fd.delete();
	}
	RandomAccessFile myFile = new RandomAccessFile (filePath, "rw");
	myFile.write (content.getBytes());
	myFile.close();
}
catch (Exception e) {
	e.printStackTrace();
}
   }

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