📄 forumdaoxml.java
字号:
/*
* XP Forum
*
* Copyright (c) 2002-2003 RedSoft Group. All rights reserved.
*
*/
package org.redsoft.forum.dao.xml;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.redsoft.forum.dao.ForumDAO;
import org.redsoft.forum.web.Forum;
/**
* AccountDAO's implmentation for xml
*
* @@author <a href="mailto:chjxm@msn.com">cinc</a>
*
* @@version $Id: ForumDAOxml.java,v 1.1.1.1 2003/07/08 08:25:16 cinc Exp $
*/
public class ForumDAOxml implements ForumDAO {
Forum[] forums;
/**
* Constructor, read forum infomation from forum.xml, and save them to
* array forums[]
*
* @param filename full filename of forum.xml
*/
public ForumDAOxml( String filename ){
int i;
File xmlFile = new File( filename );
ArrayList forumList = new ArrayList();
Document doc = null;
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
// validate xml file by parser
// that is: use a dtd file to validate the xml file
dbf.setValidating(true);
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse( xmlFile );
Element root = doc.getDocumentElement();
root.normalize();
NodeList nodes = root.getChildNodes();
for (i=0; i<nodes.getLength() ; i++){
Node node = nodes.item(i);
if (node.getNodeName().equals( "forum" )){
forumList.add( parseForumNode(node) );
}
}
// convert ArrayList to Array
forums = new Forum[forumList.size()];
for (i=0; i<forumList.size(); i++){
forums[i] = (Forum)forumList.get(i);
}
}catch(Exception e){
System.out.println ("Error reading forums.xml: " + e);
}
}
/**
* Read id, name, desc from this node
*/
protected Forum parseForumNode( Node forum ){
int id = 0;
String name = null;
String desc = null;
String property, value;
NodeList nodes = forum.getChildNodes();
for (int i=0; i<nodes.getLength() ; i++){
Node node = nodes.item(i);
if (node.getNodeName().equals( "#text" )){
continue;
}
property = node.getNodeName();
value = node.getFirstChild().getNodeValue();
if ( property.equals("id") ){
id = Integer.parseInt( value );
}else if ( property.equals("name") ){
name = value;
}else if ( property.equals("desc") ){
desc = value;
}
// System.out.println(node.getNodeName() + ":" + node.getFirstChild().getNodeValue());
}
return new Forum( id, name, desc );
}
/**
* Return the forum given a forum id
*
* @return Forum - the forum.
* @exception CategoryNotFoundException
*/
public Forum getForum( final int forumID ) {
if( (forumID - 1) > forums.length ){
return null;
}else{
return forums[ forumID - 1 ];
}
}
/**
* Retrun a list of forums
*
* @return Forum[] - A array of forums
*/
public Forum[] getForumCategory () {
return forums;
}
}//EOC
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -