Package webEditor.core

Source Code of webEditor.core.article

/*  $Id: article.java,v 1.13 2001/04/03 21:20: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.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 org.apache.regexp.RE;

import webEditor.util.FileAccess;

/**
* Encapsulate the access to the xml documents.
*
* @author <a href="mailto:agarcia@mundofree.com">Alfredo Garcia</a>
*/
public class article
{
   /**
    * Editor root
    */
   private String wEd_root;

   /**
    * Path for the data configuration files
    */
   private String dataDir;
  
   /**
    * Server path for the exported documents
    */
   private String docRoot;

   public article (configuration 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.docRoot    = "/" + initParam.readValue (categoryName,"docRoot");
  this.docRoot  = initParam.readValue (categoryName,"serverRoot") + this.docRoot;
   }
  
  
   /**
    * Returns a new Document identificator
    * @return String  New doc ID
    */
   public String newDocID ()
   {
     String myDocID = null;

     Date myDate = new Date();
     Calendar myCalendar = Calendar.getInstance();
  myDocID = "" + myCalendar.get(myCalendar.YEAR);
  myDocID = myDocID + "/" + (myCalendar.get(myCalendar.MONTH) + 1);
  myDocID = myDocID + "/" + myCalendar.get(myCalendar.DAY_OF_MONTH);

  // In this version, we get the docID from the unix time.
  myDocID = myDocID + "/" + myDate.getTime() + ".xml";
     return (myDocID);
   }
  
   /**
    * Returns the date of the document file last modification, in a formatted string   
    * @param docName  doc ID
    * @return String  Document formated date
    */
   public String docDate(String docName)
   {
     return ("OK");
   }
  
   /**
    * Modify the content of the document data file.   
    * @param dataStream    Hash with the new values to write
    * @param newsDoc    DOM tree of the document
    * @param docSection    Section of the document
    * @return Document    New DOM tree
    */
   public Document writeDoc(Hashtable dataStream, Document newsDoc, String docSection)
   {
     String rootTag = null;
     String tagName = null;
     String tagValue = null;

     Element root = newsDoc.getDocumentElement();
  if (docSection.equals ("root")) {
    rootTag = root.getTagName();
  }
  else {
    rootTag = docSection; 
  }
  NodeList list = newsDoc.getElementsByTagName(rootTag).item(0).getChildNodes();

  // Now, we look for document tags that appears in the hash, and
  // we try to change its content
  for (Enumeration e = dataStream.keys() ; e.hasMoreElements() ;) {
    tagName = (String) e.nextElement();
    tagValue = (String) dataStream.get (tagName);
    for (int i=0; i < list.getLength(); i++) {
      if (list.item(i).getNodeName().equals(tagName)) {
        Node source = list.item(i);
        Node myDocNode = source.cloneNode (true);
        myDocNode.getLastChild().setNodeValue (tagValue);
        Node parent = source.getParentNode();
        Node temp = parent.replaceChild (myDocNode, source);
      }
    }
  }

     return (newsDoc);
   }

  /**
    * Inserts the image DOM structure into the given doc section
    * @param imageDoc    DOM tree with the content of the image
    * @param newsDoc    DOM tree of the document
    * @param docSection    Section of the document
    * @return Document    New DOM tree
    */
   public Document writeImgDoc (Document imageDoc, Document newsDoc, String docSection)
   {

  Element selectedNode = (Element) newsDoc.getElementsByTagName
          (docSection).item(0);
  NodeList list = selectedNode.getChildNodes ();
  // We remove all the previous image tags in the node
  for (int i=0; i < list.getLength(); i++) {
    if (list.item(i).getNodeName().equals("image")) {
      selectedNode.removeChild (list.item(i));
    }
  }
 
  Node newNode = selectedNode.cloneNode(true)
  // And we create a new image tag (and in the first position)
  Node firstChild = newNode.getFirstChild();
  newNode.insertBefore (newsDoc.importNode
        (imageDoc.getDocumentElement(), true), firstChild);
  Node parent = selectedNode.getParentNode ();
  parent.replaceChild (newNode, selectedNode);

     return (newsDoc);
   }
  
  /**
    * Removes the image child from the given DOM node
    * @param newsDoc    DOM tree of the document
    * @param docSection    Section of the document
    * @return Document    New DOM tree
    */
   public Document deleteImgDoc (Document newsDoc, String docSection)
   {

  Element selectedNode = (Element) newsDoc.getElementsByTagName
          (docSection).item(0);
  NodeList list = selectedNode.getChildNodes ();
  // We remove all the "image" tags in the node
  for (int i=0; i < list.getLength(); i++) {
    if (list.item(i).getNodeName().equals("image")) {
      selectedNode.removeChild (list.item(i));
    }
  }
 
     return (newsDoc);
   }


   /**
    * Returns the document content in a DOM tree structure.   
    * @param docName    doc ID
    * @return Document    DOM tree of the document
    */
   public Document docRead(String docName)
   {
  Document doc = null;  
try {
  DOMParser parser = new DOMParser();
  docName = this.dataDir + "/news/" + docName;
  docName = new File (docName).getAbsolutePath();
  docName = "file:" + docName;
 
  parser.parse(docName);
  doc = parser.getDocument();
}
catch (Exception e) {
  e.printStackTrace ();
}
  return (doc);

   }

   /**
    * Returns the subtree with the given root of the DOM document
    * @param newsDoc    DOM tree of the document
    * @param root    Sub tree root
    * @return Document    DOM sub tree
    */
   public Document readSubtree (Document newsDoc, String root)
   {
     Element item;
     Element docRoot = null;
  String rootTag = null;
  DocumentImpl docFragment = new DocumentImpl();
 
  if ( root.equals ("root") ) {
       docRoot = newsDoc.getDocumentElement();
  }
  else {
       docRoot = (Element) newsDoc.getElementsByTagName(root).item(0);
  }

  if (docRoot == null) {
    // The document doesn't have any tag with this name
    return (null);
  }

  // So, we copy all the subtree into the new document
  docFragment.appendChild (docFragment.importNode
        (docRoot, true));
     return (docFragment);
   }



  /**
    * Saves the content of a DOM tree into a file (in XML format, of course)
    * @param docName  doc ID (file to save)
    * @param doc  DOM tree of the document
    * @return void
    */
   public void saveFile (String docName, Document doc)
   {
try {
  FileAccess  fileHandler = new FileAccess ();

  OutputFormat    format  = new OutputFormat (doc, "ISO-8859-1" ,true);
  format.setIndent (2);
  format.setLineWidth (80);
  format.setPreserveSpace (true);
  StringWriter  stringOut = new StringWriter ();
  XMLSerializer    serial = new XMLSerializer (stringOut, format);
  serial.asDOMSerializer ();

  serial.serialize (doc.getDocumentElement());

  String filePath = this.dataDir + "/news/" + docName;
  // Check if the directory structure exists...
  fileHandler.createDocDir (docName,this.dataDir + "/news/");
  // 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 (stringOut.toString().getBytes());
  myFile.close();
}
catch (Exception e) {
  e.printStackTrace();
}
   }


  /**
    * Returns the associated HTML file name
    * @param docID  doc ID of the document
    * @return void
    */
   public String getHTMLname (String docID)
   {
     String docName = null;
try {
  RE r = new RE("xml$");
  docName = r.subst (docID,"html");
}
catch (Exception e) {
  e.printStackTrace();
}
  return (docName);
   }


  /**
    * Saves the string with the HTML content into a file
    * @param docName  doc ID (file to save)
    * @param content  String with the document
    * @return void
    */
   public void saveHtmlFile (String docName, String content)
   {
try {
  FileAccess  fileHandler = new FileAccess ();

  // First at all, we expect to recive the XML name of the document,
  // so we are going to change the file extension.
  docName = this.getHTMLname (docName);

  String filePath = this.docRoot + "/news/" + docName;
  // Check if the directory structure exists...
  fileHandler.createDocDir (docName,this.docRoot + "/news/");
  // 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();
}
   }

}
TOP

Related Classes of webEditor.core.article

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.