📄 commentadmin.java
字号:
/* * CommentAdmin.java * * Created on 2004年12月28日, 上午03:24 * 这个类主要用于管理用户对相片的评论 * */package com.mg.admin;import java.io.*;import java.util.*;import org.jdom.*;import org.jdom.input.*;import org.jdom.output.*;/** * 这个类主要用于管理用户对相片的评论 */public class CommentAdmin extends Object{ //********************成员变量************************ /** * 操作的根路径 */ private String rootPath; /** * 当前操作的XML文件名 */ private static final String TABLE_NAME = "comments.xml"; //********************成员函数************************ /** * 构造函数,如果使用这个构造函数,需要调用setRootPath设置根路径 * <p> * @see #setRootPath */ public CommentAdmin() { } /** * 设置操作的根路径 * <p> * @see #getRootPath */ public void setRootPath(String rp) { if (rootPath != rp) { rootPath = rp; } } /** * 返回操作的根路径 * <p> * @return 返回根路径 * <p> * @see #setRootPath */ public String getRootPath() { return rootPath; } /** * 获得当前的XML Document对象 */ private Document getDoc() { Document doc = null; PrintStream out = System.out; //读取文件 try { File file = new File(rootPath, TABLE_NAME); if (file.exists()) { SAXBuilder builder = new SAXBuilder(false); doc = builder.build(file); } } catch(Exception e) { out.println("Exception: " + e.toString()); } return doc; } /** * 连接当前的rootPath下的comments.xml名的XML文件, * 并返回对指定照片的所有评论。 * <p> * @param uid 照片的所有者。 * @param pictureFileName 照片的文件名。 * <p> * @return 封装了Comment对象的Vecotr对象。 */ public Vector getComments(String uid, String pictureFileName) { Vector ret = new Vector(); Document doc=getDoc(); if (doc != null) { // 获取根节点 Element root = doc.getRootElement(); List list = root.getChildren(); Iterator list_i = list.iterator(); Element userNode=null; while (list_i.hasNext()) { //获取子节点 Element e = (Element)list_i.next(); //获取二级子节点 List details = e.getChildren(); if (details.size()<Picture.ELEMENT_NUM) { //错误 break; }// <comment>// <content>123</content> // <filename>RL_1104333493935_Winter.jpg</filename> // <datetime>2004-12-29 11:18 下午</datetime> // <userid>RL</userid> // </comment> //添加Picture对象 String content = ((Element)details.get(0)).getText(); String filename = ((Element)details.get(1)).getText(); String datetime = ((Element)details.get(2)).getText(); String userid = ((Element)details.get(3)).getText(); if (!filename.equals(pictureFileName) || !filename.startsWith(uid)) { continue; } Comment commnent = new Comment(); commnent.setContent(content); commnent.setFileName(filename); commnent.setDateTime(datetime); commnent.setUser(userid); ret.add(commnent); } } return ret; } /** * 连接当前的rootPath下的pictures.xml名的XML文件, * 并添加一条相片评论,如果成功则返回true,否则返回false。 * <p> * @param comment Comment对象 */ public boolean add(Comment comment) { boolean ret = false; Document doc=getDoc(); if (doc != null) { // 获取根节点 Element root = doc.getRootElement(); Element newE = new Element("comment"); Element eContent= new Element("content"); eContent.setText(comment.getContent()); newE.addContent(eContent); Element eFileName = new Element("filename"); eFileName.setText(comment.getFileName()); newE.addContent(eFileName); Element eDateTime = new Element("datetime"); eDateTime.setText(comment.getDateTime()); newE.addContent(eDateTime); Element eUserID = new Element("userid"); eUserID.setText(comment.getUser()); newE.addContent(eUserID); root.addContent(newE); //保存修改 save(doc); ret = true; } return ret; } /** * 保存修改到XML文件 * <p> * @param doc 要保存的Document对象 */ private void save(Document doc) { //保存修改 try { Format format = Format.getPrettyFormat(); format.setEncoding("GBK"); XMLOutputter out = new XMLOutputter(format); File file = new File(rootPath, TABLE_NAME); FileOutputStream fos = new FileOutputStream(file); out.output(doc, fos); } catch(Exception e) { System.out.println("Exception: " + e.toString()); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -