Posted on 2010-06-07 15:25 
java小爬虫 阅读(4780) 
评论(0)  编辑  收藏  
			 
			
		 
		 package net.better_best.www.dynamicADPics.utils;
package net.better_best.www.dynamicADPics.utils;

 import java.io.File;
import java.io.File;
 import java.io.FileWriter;
import java.io.FileWriter;
 import java.io.IOException;
import java.io.IOException;
 import java.util.ArrayList;
import java.util.ArrayList;
 import java.util.Iterator;
import java.util.Iterator;
 import java.util.List;
import java.util.List;

 import org.dom4j.Attribute;
import org.dom4j.Attribute;
 import org.dom4j.Document;
import org.dom4j.Document;
 import org.dom4j.DocumentException;
import org.dom4j.DocumentException;
 import org.dom4j.DocumentHelper;
import org.dom4j.DocumentHelper;
 import org.dom4j.Element;
import org.dom4j.Element;
 import org.dom4j.io.SAXReader;
import org.dom4j.io.SAXReader;
 import org.dom4j.io.XMLWriter;
import org.dom4j.io.XMLWriter;


 public class DOM4jUtils
public class DOM4jUtils  {
{

 /** *//**
    /** *//**
 * 在一个xml文件中,遍历xml文件,根据Element的elementName,取出所有复合条件的Element元素,把它们转换为javabean类型,并放入List中返回。
     * 在一个xml文件中,遍历xml文件,根据Element的elementName,取出所有复合条件的Element元素,把它们转换为javabean类型,并放入List中返回。
 */
     */

 public static List parseElementsToObjects(Element root, String elementName)
    public static List parseElementsToObjects(Element root, String elementName)  {
{
 List<Banner> result =  new ArrayList<Banner>();
        List<Banner> result =  new ArrayList<Banner>();
 List<Element> list = root.elements(elementName);
          List<Element> list = root.elements(elementName);      

 for (Element element : list)
          for (Element element : list)  {
{
 Banner banner = new Banner();
             Banner banner = new Banner();

 for(Iterator it=element.attributeIterator();it.hasNext();)
             for(Iterator it=element.attributeIterator();it.hasNext();) {
{
 Attribute attribute = (Attribute) it.next();
                 Attribute attribute = (Attribute) it.next();

 if(attribute.getName().equals(DOM4jConstant.ELEMENT_ATTRIBUTE_BGURL))
                 if(attribute.getName().equals(DOM4jConstant.ELEMENT_ATTRIBUTE_BGURL)) {
{
 banner.setBnurl(attribute.getValue());
                     banner.setBnurl(attribute.getValue());

 }else if(attribute.getName().equals(DOM4jConstant.ELEMENT_ATTRIBUTE_IMG))
                 }else if(attribute.getName().equals(DOM4jConstant.ELEMENT_ATTRIBUTE_IMG)) {
{
 banner.setImg(attribute.getValue());
                     banner.setImg(attribute.getValue());

 }else if(attribute.getName().equals(DOM4jConstant.ELEMENT_ATTRIBUTE_BNTARGET))
                 }else if(attribute.getName().equals(DOM4jConstant.ELEMENT_ATTRIBUTE_BNTARGET)) {
{
 banner.setBntarget(attribute.getValue());
                     banner.setBntarget(attribute.getValue());
 }
                 }
 }
             }
 result.add(banner);
             result.add(banner);            
 }
        }
 return result ;
          return result ;
 }
    }

 /** *//**
    /** *//**
 * 根据xml文件的文件名,把xml文件转换成Document对象并返回。
     * 根据xml文件的文件名,把xml文件转换成Document对象并返回。
 */
     */

 public static Document getDocument(String fileUrl)throws DocumentException
    public static Document getDocument(String fileUrl)throws DocumentException  {
{
 File file = new File(fileUrl);
        File file = new File(fileUrl);
 SAXReader reader = new SAXReader();
        SAXReader reader = new SAXReader();
 Document document = reader.read(file);
        Document document = reader.read(file);
 return document;
        return document;
 }
    }

 /** *//**
    /** *//**
 * 根据root 元素的rootName,Element元素的javabean对象以及Element元素的elementName创建Document对象并返回。
     * 根据root 元素的rootName,Element元素的javabean对象以及Element元素的elementName创建Document对象并返回。
 */
     */


 public static Document createDocument(String rootName,Banner banner,String elementName)
    public static Document createDocument(String rootName,Banner banner,String elementName)  {
{
 Document doc = DocumentHelper.createDocument();
        Document doc = DocumentHelper.createDocument();
 Element root = doc.addElement(rootName);
        Element root = doc.addElement(rootName);
 setDocumentHeader(root);
        setDocumentHeader(root);
 addElement(root,banner,elementName);
        addElement(root,banner,elementName);
 return doc;
        return doc;
 }
    }

 /** *//**
    /** *//**
 * 根据Element元素的javabean对象以及Element元素的elementName在Element root下增加一个Element 。
     * 根据Element元素的javabean对象以及Element元素的elementName在Element root下增加一个Element 。
 */
     */

 public static void addElement(Element root, Banner banner,String elementName)
    public static void addElement(Element root, Banner banner,String elementName)  {
{
 Element element = root.addElement(elementName);
        Element element = root.addElement(elementName);
 element.addAttribute(DOM4jConstant.ELEMENT_ATTRIBUTE_IMG, banner.getImg()).addAttribute(DOM4jConstant.ELEMENT_ATTRIBUTE_BGURL, banner.getBnurl()).addAttribute(DOM4jConstant.ELEMENT_ATTRIBUTE_BNTARGET, banner.getBntarget()).addText("");
        element.addAttribute(DOM4jConstant.ELEMENT_ATTRIBUTE_IMG, banner.getImg()).addAttribute(DOM4jConstant.ELEMENT_ATTRIBUTE_BGURL, banner.getBnurl()).addAttribute(DOM4jConstant.ELEMENT_ATTRIBUTE_BNTARGET, banner.getBntarget()).addText("");
 }
    }

 /** *//**
    /** *//**
 * 在Document对象中,以elementName,attributeName,attributeValue为参数删除对应的Element元素。
     * 在Document对象中,以elementName,attributeName,attributeValue为参数删除对应的Element元素。
 */
     */

 public static Document removeElementByAttribute(Document document, String elementName,String attributeName,String attributeValue)
    public static Document removeElementByAttribute(Document document, String elementName,String attributeName,String attributeValue)  {
{
 List<Element> list = document.getRootElement().elements(elementName);
          List<Element> list = document.getRootElement().elements(elementName);        

 exit:   for (Element element : list)
        exit:   for (Element element : list)  {
{

 for(Iterator it=element.attributeIterator();it.hasNext();)
             for(Iterator it=element.attributeIterator();it.hasNext();) {
{
 Attribute attribute = (Attribute) it.next();
                 Attribute attribute = (Attribute) it.next();   

 if(attribute.getName().equals(attributeName)&&attribute.getValue().equals(attributeValue))
                 if(attribute.getName().equals(attributeName)&&attribute.getValue().equals(attributeValue)) {
{
 element.getParent().remove(element);
                     element.getParent().remove(element);
 break exit;
                     break exit;
 }
                 }
 }
             }           
 }
        }
 return document ;
          return document ;        
 }
    }

 /** *//**
    /** *//**
 * 把Document对象与filePath对应的物理文件进行同步。
     * 把Document对象与filePath对应的物理文件进行同步。
 */
     */

 public static void writeDocumentToFile(Document document, String filePath)throws IOException
    public static void writeDocumentToFile(Document document, String filePath)throws IOException  {
{
 XMLWriter writer = new XMLWriter(new FileWriter(filePath));
        XMLWriter writer = new XMLWriter(new FileWriter(filePath));
 writer.write(document);
        writer.write(document);
 writer.flush();
        writer.flush();
 writer.close();
        writer.close();
 }
    }

 /** *//**
    /** *//**
 * 设置Document对象的文件头。
     * 设置Document对象的文件头。
 */
     */

 public static void setDocumentHeader(Element root)
    public static void setDocumentHeader(Element root)  {
{
 Element fixedbn = root.addElement("fixedbn");
        Element fixedbn = root.addElement("fixedbn");
 fixedbn.addElement("fixedlink").addAttribute("posx", "0")
        fixedbn.addElement("fixedlink").addAttribute("posx", "0")
 .addAttribute("posy", "0")
                                       .addAttribute("posy", "0")
 .addAttribute("scalex", "1510")
                                       .addAttribute("scalex", "1510")
 .addAttribute("scaley", "800")
                                       .addAttribute("scaley", "800")
 .addAttribute("bnurl", "http://www.lanrentuku.com/")
                                       .addAttribute("bnurl", "http://www.lanrentuku.com/")
 .addAttribute("bntarget", "_self")
                                       .addAttribute("bntarget", "_self")
 .addText("");
                                       .addText("");
 fixedbn.addElement("fixedlink").addAttribute("posx", "0")
        fixedbn.addElement("fixedlink").addAttribute("posx", "0")
 .addAttribute("posy", "80")
                                       .addAttribute("posy", "80")
 .addAttribute("scalex", "1510")
                                       .addAttribute("scalex", "1510")
 .addAttribute("scaley", "1150")
                                       .addAttribute("scaley", "1150")
 .addAttribute("bnurl", "http://www.lanrentuku.com/")
                                       .addAttribute("bnurl", "http://www.lanrentuku.com/")
 .addAttribute("bntarget", "_self")
                                       .addAttribute("bntarget", "_self")
 .addText("");
                                       .addText("");
 fixedbn.addAttribute("alpha_num", "0");
        fixedbn.addAttribute("alpha_num", "0");
 }
    }

 }
}
 package net.better_best.www.dynamicADPics.utils;
package net.better_best.www.dynamicADPics.utils;


 public class DOM4jConstant
public class DOM4jConstant  {
{
 public static final String ELEMENT_ATTRIBUTE_IMG  = "img";
    public static final String ELEMENT_ATTRIBUTE_IMG  = "img";
 public static final String ELEMENT_ATTRIBUTE_BGURL  = "bnurl";
    public static final String ELEMENT_ATTRIBUTE_BGURL  = "bnurl";
 public static final String ELEMENT_ATTRIBUTE_BNTARGET  = "bntarget";
    public static final String ELEMENT_ATTRIBUTE_BNTARGET  = "bntarget";
 
    
 public static final String FILE_PATH_NAME  = "/WEB-INF/xml.xml";
    public static final String FILE_PATH_NAME  = "/WEB-INF/xml.xml";

 }
}




 package net.better_best.www.dynamicADPics.utils;
package net.better_best.www.dynamicADPics.utils;


 public class Banner
public class Banner  {
{
 private String img;
    private String img;
 private String bnurl;
    private String bnurl;
 private String bntarget;
    private String bntarget;
 
    

 public String getImg()
    public String getImg()  {
{
 return img;
        return img;
 }
    }

 public void setImg(String img)
    public void setImg(String img)  {
{
 this.img = img;
        this.img = img;
 }
    }

 public String getBnurl()
    public String getBnurl()  {
{
 return bnurl;
        return bnurl;
 }
    }

 public void setBnurl(String bnurl)
    public void setBnurl(String bnurl)  {
{
 this.bnurl = bnurl;
        this.bnurl = bnurl;
 }
    }

 public String getBntarget()
    public String getBntarget()  {
{
 return bntarget;
        return bntarget;
 }
    }

 public void setBntarget(String bntarget)
    public void setBntarget(String bntarget)  {
{
 this.bntarget = bntarget;
        this.bntarget = bntarget;
 }
    }
 
    

 }
}

 package net.better_best.www.dynamicADPic.action;
package net.better_best.www.dynamicADPic.action;

 import java.util.List;
import java.util.List;

 import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponse;

 import net.better_best.www.dynamicADPic.form.DynamicADPicForm;
import net.better_best.www.dynamicADPic.form.DynamicADPicForm;
 import net.better_best.www.dynamicADPics.utils.Banner;
import net.better_best.www.dynamicADPics.utils.Banner;
 import net.better_best.www.dynamicADPics.utils.DOM4jConstant;
import net.better_best.www.dynamicADPics.utils.DOM4jConstant;
 import net.better_best.www.dynamicADPics.utils.DOM4jUtils;
import net.better_best.www.dynamicADPics.utils.DOM4jUtils;

 import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForm;
 import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionForward;
 import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMapping;
 import org.apache.struts.actions.DispatchAction;
import org.apache.struts.actions.DispatchAction;
 import org.dom4j.Document;
import org.dom4j.Document;
 import org.dom4j.DocumentException;
import org.dom4j.DocumentException;


 public class DynamicADPicAction extends DispatchAction
public class DynamicADPicAction extends DispatchAction  {
{
 @Override
    @Override
 public ActionForward execute(ActionMapping mapping, ActionForm form,
    public ActionForward execute(ActionMapping mapping, ActionForm form,
 HttpServletRequest request, HttpServletResponse response)
            HttpServletRequest request, HttpServletResponse response)

 throws Exception
            throws Exception  {
{
 return super.execute(mapping, form, request, response);
        return super.execute(mapping, form, request, response);
 }
    }

 public  ActionForward add(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception
     public  ActionForward add(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception  {
{
 String filePath=request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
         String filePath=request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
 Document document = DOM4jUtils.getDocument(filePath);
          Document document = DOM4jUtils.getDocument(filePath);
 DynamicADPicForm dynamicADPic = (DynamicADPicForm)form ;
          DynamicADPicForm dynamicADPic = (DynamicADPicForm)form ;
 String elementName="banner";
          String elementName="banner";
 Banner banner = new Banner();
          Banner banner = new Banner();
 banner.setImg(dynamicADPic.getImg());
            banner.setImg(dynamicADPic.getImg());
 banner.setBnurl(dynamicADPic.getBnurl());
            banner.setBnurl(dynamicADPic.getBnurl());
 banner.setBntarget("_blank");
            banner.setBntarget("_blank");
 DOM4jUtils.addElement(document.getRootElement(), banner, elementName);
          DOM4jUtils.addElement(document.getRootElement(), banner, elementName);  
 DOM4jUtils.writeDocumentToFile(document, filePath);
          DOM4jUtils.writeDocumentToFile(document, filePath);
 return  this.list(mapping, form, request, response);
        return  this.list(mapping, form, request, response);
 }
        }

 public  ActionForward list(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception
     public  ActionForward list(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception  {
{
 
                            
 String elementName = "banner";
         String elementName = "banner";
 String filePath=request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
         String filePath=request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
 Document document = null;
         Document document = null;

 try
            try  {
{
 document = DOM4jUtils.getDocument(filePath) ;
                 document = DOM4jUtils.getDocument(filePath) ;

 } catch (DocumentException e)
            } catch (DocumentException e)  {
{
 init(request);
                init(request);
 document = DOM4jUtils.getDocument(filePath) ;
                 document = DOM4jUtils.getDocument(filePath) ;
 }
            }
 
        
 List list = DOM4jUtils.parseElementsToObjects(document.getRootElement(), elementName);
          List list = DOM4jUtils.parseElementsToObjects(document.getRootElement(), elementName); 
 request.setAttribute("piclist", list);
            request.setAttribute("piclist", list);
 request.getSession().setAttribute("n", request.getParameter("n"));
            request.getSession().setAttribute("n", request.getParameter("n"));
 return mapping.findForward("list");
            return mapping.findForward("list");
 }
        }

 public  ActionForward delete(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception
     public  ActionForward delete(ActionMapping mapping, ActionForm form,HttpServletRequest request, HttpServletResponse response) throws Exception  {
{
 String filePath=request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
         String filePath=request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;     
 Document document = DOM4jUtils.getDocument(filePath);
         Document document = DOM4jUtils.getDocument(filePath);
 document = DOM4jUtils.removeElementByAttribute(document, "banner", "img", request.getParameter("img"));
         document = DOM4jUtils.removeElementByAttribute(document, "banner", "img", request.getParameter("img"));
 DOM4jUtils.writeDocumentToFile(document, filePath);
         DOM4jUtils.writeDocumentToFile(document, filePath);
 return  this.list(mapping, form, request, response);
         return  this.list(mapping, form, request, response);
 }
        }

 public  void  init(HttpServletRequest request) throws Exception
     public  void  init(HttpServletRequest request) throws Exception  {
{
 Banner banner = new Banner();
            Banner banner = new Banner();
 banner.setImg("images/01.jpg");
            banner.setImg("images/01.jpg");
 banner.setBnurl("http://www.lanrentuku.com/");
            banner.setBnurl("http://www.lanrentuku.com/");
 banner.setBntarget("_blank");
            banner.setBntarget("_blank");
 String elementName="banner";
            String elementName="banner";
 String rootName="banner_swf";
            String rootName="banner_swf";
 Document document = DOM4jUtils.createDocument(rootName, banner, elementName);
            Document document = DOM4jUtils.createDocument(rootName, banner, elementName);
 String filePath=request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
            String filePath=request.getRealPath("/")+DOM4jConstant.FILE_PATH_NAME;
 DOM4jUtils.writeDocumentToFile(document, filePath);
            DOM4jUtils.writeDocumentToFile(document, filePath);
 
           
 }
        }
 }
}
