📄 blogservice.java
字号:
outputter.output(doc,out);
}
/**
* 输出指定日志的全部引用为XML文档
* @param out
* @param blogid
* @throws Exception
*/
public void outputReference(PrintWriter out, String blogid) throws Exception {
BlogReference[] references = this.getReference(blogid);
Blog blog = this.getBlog(blogid);
Document doc = new Document();
Element root = new Element("references");
Element track = new Element("track").addContent(blog.getTrack());
root.addContent(track);
if((references!=null)&&(references.length!=0)) {
for(int i=0;i<references.length;i++) {
Element element = new Element("reference");
Element refername = new Element("refername").addContent(references[i].getRefername());
Element referurl = new Element("referurl").addContent(references[i].getReferurl());
element.addContent(refername);
element.addContent(referurl);
root.addContent(element);
}
}
doc.addContent(root);
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setEncoding("ISO8859-1"));
outputter.output(doc,out);
}
/**
* 获取全部的自定义列表,以对象数组的形式返回
* @return
* @throws Exception
*/
public BlogList[] getAllList() throws Exception{
Collection bloglist = new ArrayList();
if(this.listdom.getRootElement().getChildren().size()!=0) {
Iterator iterator = this.listdom.getRootElement().getChildren().iterator();
do {
Element element = (Element)iterator.next();
BlogList list = new BlogList();
list.initialItem(element);
bloglist.add(list);
}while(iterator.hasNext());
return (BlogList[])bloglist.toArray(new BlogList[0]);
}
else
return new BlogList[0];
}
/**
* 获取全部的自定义列表,以Map的形式返回
* @return
* @throws Exception
*/
public Map getAllListMap() throws Exception {
Map bloglist = new HashMap();
if(this.listdom.getRootElement().getChildren().size()!=0) {
Iterator iterator = this.listdom.getRootElement().getChildren().iterator();
do {
Element element = (Element)iterator.next();
BlogList list = new BlogList();
list.initialItem(element);
bloglist.put(list.getListid(),list);
}while(iterator.hasNext());
return bloglist;
}
else
return bloglist;
}
/**
* 获取指定编号的自定义列表
* @param listid
* @return
* @throws Exception
*/
public BlogList getBlogList(String listid) throws Exception {
BlogList list = null;
if(this.listdom.getRootElement().getChildren().size()!=0) {
Iterator iterator = this.listdom.getRootElement().getChildren().iterator();
do {
Element element = (Element)iterator.next();
if(element.getAttributeValue("listid").equalsIgnoreCase(listid)){
list = new BlogList();
list.initialItem(element);
}
}while(iterator.hasNext());
}
return list;
}
/**
* 获取指定自定义列表的全部列表项目,以对象数组的形式返回
* @param listid
* @return
* @throws Exception
*/
public BlogListItem[] getAllListItem(String listid) throws Exception {
Collection itemes = new ArrayList();
if(this.listdom.getRootElement().getChildren().size()!=0) {
Iterator iterator = this.listdom.getRootElement().getChildren().iterator();
do {
Element element = (Element)iterator.next();
if(element.getAttributeValue("listid").equalsIgnoreCase(listid)) {
if(element.getChildren("item").size()!=0) {
Iterator cIterator = element.getChildren("item").iterator();
do {
Element cElement = (Element)cIterator.next();
BlogListItem item = new BlogListItem(cElement);
itemes.add(item);
}while(cIterator.hasNext());
}
}
}while(iterator.hasNext());
}
return (BlogListItem[])itemes.toArray(new BlogListItem[0]);
}
/**
* 获取指定自定义列表的列表项目,以Map的形式返回
* @param listid
* @return
* @throws Exception
*/
public Map getAllListItemMap(String listid) throws Exception {
Map itemes = new HashMap();
if(this.listdom.getRootElement().getChildren().size()!=0) {
Iterator iterator = this.listdom.getRootElement().getChildren().iterator();
do {
Element element = (Element)iterator.next();
if(element.getAttributeValue("listid").equalsIgnoreCase(listid)) {
if(element.getChild("item").getChildren().size()!=0) {
Iterator cIterator = element.getChild("item").getChildren().iterator();
do {
Element cElement = (Element)cIterator.next();
BlogListItem item = new BlogListItem(cElement);
itemes.put(item.getItemid(),item);
}while(cIterator.hasNext());
}
}
}while(iterator.hasNext());
}
return itemes;
}
/**
* 获取指定的列表项
* @param listid
* @param itemid
* @return
* @throws Exception
*/
public BlogListItem getListItem(String listid, String itemid) throws Exception {
BlogListItem[] itemes = this.getAllListItem(listid);
BlogListItem item = null;
if((itemes!=null)&&(itemes.length!=0)) {
for(int i=0;i<itemes.length;i++) {
if(itemid.equalsIgnoreCase(itemes[i].getItemid())) {
item = itemes[i];
break;
}
}
}
return item;
}
/**
* 往指定自定义列表中添加列表项目
* @param listid
* @param item
* @throws Exception
*/
public void addListItem(String listid, BlogListItem item) throws Exception {
BlogList[] listes = this.getAllList();
if((listes!=null)&&(listes.length!=0)) {
for(int i=0;i<listes.length;i++) {
if(listid.equalsIgnoreCase(listes[i].getListid())) {
item.setItemid(String.valueOf(this.getAllListItem(listes[i].getListid()).length+1));
listes[i].getListitem().put(item.getItemid(),item);
break;
}
}
this.outputListDocument(listes);
}
}
/**
* 删除指定的列表项目
* @param listid
* @param itemid
* @throws Exception
*/
public void deleteListItem(String listid, String itemid) throws Exception {
BlogList[] listes = this.getAllList();
if((listes!=null)&&(listes.length!=0)) {
for(int i=0;i<listes.length;i++) {
if(listid.equalsIgnoreCase(listes[i].getListid())) {
listes[i].getListitem().remove(itemid);
}
}
}
this.outputListDocument(listes);
}
/**
* 更新指定的列表项目
* @param listid
* @param item
* @throws Exception
*/
public void updateListItem(String listid, BlogListItem item) throws Exception {
BlogList[] listes = this.getAllList();
if((listes!=null)&&(listes.length!=0)) {
for(int i=0;i<listes.length;i++) {
if(listid.equalsIgnoreCase(listes[i].getListid())) {
listes[i].getListitem().put(item.getItemid(),item);
}
}
}
this.outputListDocument(listes);
}
/**
* 输出自定义列表到XML持久文档
* @param listes
* @throws Exception
*/
public void outputListDocument(BlogList[] listes) throws Exception {
File file = new File(ClassService.class.getResource("list.xml").getPath());
if(file.exists()) file.delete();
if(!file.exists()) file.createNewFile();
FileOutputStream stream = new FileOutputStream(file);
Document doc = new Document();
Element root = new Element("listes");
for(int i=0;i<listes.length;i++) {
Element list = new Element("list");
list.setAttribute("listid",listes[i].getListid());
list.setAttribute("listname",listes[i].getListname());
Element itemtotal = new Element("itemtotal").addContent(String.valueOf(listes[i].getItemtotal()));
list.addContent(itemtotal);
if((listes[i].getListitem()!=null)&&(listes[i].getListitem().size()!=0)) {
Iterator iterator = listes[i].getListitem().values().iterator();
do {
BlogListItem item = (BlogListItem)iterator.next();
Element itemElement = new Element("item");
itemElement.setAttribute("itemid",item.getItemid());
Element itemname = new Element("itemname").addContent(item.getItemname());
Element itemurl = new Element("itemurl").addContent(item.getItemurl());
CDATA cdata = new CDATA(item.getDescription());
Element description = new Element("description").addContent(cdata);
itemElement.addContent(itemname);
itemElement.addContent(itemurl);
itemElement.addContent(description);
list.addContent(itemElement);
}while(iterator.hasNext());
}
root.addContent(list);
}
doc.addContent(root);
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setEncoding("gb2312"));
outputter.output(doc,stream);
}
/**
* 输出指定自定义列表为XML文档
* @param out
* @param listes
* @throws Exception
*/
public void outputList(PrintWriter out, BlogList[] listes) throws Exception {
Document doc = new Document();
Element root = new Element("listes");
for(int i=0;i<listes.length;i++) {
Element list = new Element("list");
list.setAttribute("listid",listes[i].getListid());
list.setAttribute("listname",listes[i].getListname());
if((listes[i].getListitem()!=null)&&(listes[i].getListitem().size()!=0)) {
Iterator iterator = listes[i].getListitem().values().iterator();
do {
BlogListItem item = (BlogListItem)iterator.next();
Element itemElement = new Element("item");
itemElement.setAttribute("itemid",item.getItemid());
Element itemname = new Element("itemname").addContent(item.getItemname());
Element itemurl = new Element("itemurl").addContent(item.getItemurl());
Element description = new Element("description").addContent(item.getDescription());
itemElement.addContent(itemname);
itemElement.addContent(itemurl);
itemElement.addContent(description);
list.addContent(itemElement);
}while(iterator.hasNext());
}
root.addContent(list);
}
doc.addContent(root);
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat().setEncoding("ISO8859-1"));
outputter.output(doc,out);
}
/**
* 处理系统异常
* @param ex
*/
private void handleError(Exception ex) {
ex.printStackTrace();
System.out.println(ex.toString());
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -