📄 catalogmanager.java
字号:
/* * Created on 22-ott-2005 * *//** * @author magowiz * */package catalog_exchange;import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.FileWriter;import java.io.IOException;import java.io.InputStream;import java.net.MalformedURLException;import java.net.URL;import java.util.Date;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import javax.xml.transform.OutputKeys;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerConfigurationException;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import org.apache.xerces.dom.DocumentImpl;import org.apache.xml.serialize.OutputFormat;import org.apache.xml.serialize.XMLSerializer;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Node;import org.w3c.dom.NodeList;import org.xml.sax.SAXException;public class CatalogManager { private int catalogNumber; private int N; private int firstFreeID; private File catalogData=new File("cdata.xml"); private Document doc; private boolean lock=false; private CatalogInfo [] info; public boolean isFull() { boolean f=false; if(catalogNumber==N) f=true; return f; } private void increaseSize() { CatalogInfo [] temp= new CatalogInfo[N+1]; for(int i=0;i<N;i++) { temp[i]=new CatalogInfo(); temp[i].insert(info[i]); } N=N+1; info=temp; } private void increaseSize(int n) { if (n==0) n++; CatalogInfo [] temp= new CatalogInfo[info.length+n]; for(int i=0;i<info.length;i++) { temp[i]=new CatalogInfo(); temp[i].insert(info[i]); } N=info.length+n; info=temp; } private void decreaseSize() { CatalogInfo [] temp= new CatalogInfo[N-1]; for(int i=0;i<N-1;i++) { temp[i]=new CatalogInfo(); temp[i].insert(info[i]); } N=N-1; info=temp; } private void decreaseSize(int n) { if (n==0) n++; CatalogInfo [] temp= new CatalogInfo[N-n]; for(int i=0;i<N-n;i++) { temp[i]=new CatalogInfo(); temp[i].insert(info[i]); } N=N-n; info=temp; } public boolean delete (String cname) { while (lock) {try{Thread.sleep(5000);}catch(InterruptedException ie){}} lock=true; boolean d=false; int id=search(cname); File f=new File("catalog"+id+".xml"); f.delete(); Element rootdoc = doc.getDocumentElement(); NodeList nl=doc.getElementsByTagName("catalog"); for(int i=0;i<nl.getLength();i++) { Element root=(Element) nl.item(i); NodeList nl2=root.getChildNodes(); for(int j=0;j<nl2.getLength();j++) { if(nl2.item(j).getNodeName().equals("id")) { Element temp=(Element)nl2.item(j); NodeList nl3=temp.getChildNodes(); Node node=nl3.item(0); String ids=String.valueOf(id); if(ids.equals(node.getNodeValue())) rootdoc.removeChild(root); } } } info[id]=null; compress(id); this.decreaseSize(); catalogNumber--; lock=false; save(); float freePercent=(float)(N-catalogNumber)/100; if(freePercent>30) this.decreaseSize(((30*catalogNumber)/100)); return d; } private boolean compress(int id) { boolean a=false; boolean found=false; int comodo=id+1; File next=new File("catalog"+comodo+".xml"); if (next.exists()) { info[id]=info[id+1]; File old=new File("catalog"+id+".xml"); next.renameTo(old); int i=0; NodeList nl=null; nl=doc.getElementsByTagName("id"); while ((!found)&&(i<nl.getLength())) { Node n2=nl.item(i).getFirstChild(); if (n2.getNodeValue().equals(String.valueOf(id+1))) { found=true; n2.setNodeValue(String.valueOf(id)); lock=false; save(); lock=true; } i++; } compress(id+1); } else info[id]=new CatalogInfo(); lock=false; return a; } public CatalogManager(int dim) { N=dim; boolean vuoto=true; info=new CatalogInfo[N]; DocumentBuilder db = null; for (int i=0; i<N;i++) { info[i]=new CatalogInfo(); } DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance(); try { db = dbf.newDocumentBuilder(); } catch (ParserConfigurationException pce) {pce.printStackTrace();} if(!catalogData.exists()) { firstFreeID=0; catalogNumber=0; doc= new DocumentImpl(); Element root = doc.createElement("cmdata"); doc.appendChild(root); FileOutputStream fos=null; try{ fos = new FileOutputStream(catalogData);} catch(FileNotFoundException fnf){} OutputFormat of = new OutputFormat("XML","ISO-8859-15",true); of.setIndent(1); of.setIndenting(true); //of.setDoctype(null,"users.dtd"); XMLSerializer serializer = new XMLSerializer(fos,of); try{serializer.asDOMSerializer(); serializer.serialize( doc.getDocumentElement() ); fos.close();} catch(IOException ioe){} } else{ try {doc = db.parse(catalogData);} catch (SAXException se){se.printStackTrace();} catch (IOException ioe) { ioe.printStackTrace(); System.exit(1); } NodeList l = doc.getElementsByTagName("catalog"); catalogNumber=l.getLength(); for(int i=0;i<l.getLength();i++) { vuoto=false; int id=0; Date d=null; Date lm=null; URL u=null; String n=null; Element e=(Element) l.item(i); NodeList childs=e.getChildNodes(); for(int j=0; j<childs.getLength();j++) { if(childs.item(j).getNodeType()==Node.ELEMENT_NODE) { if(childs.item(j).getNodeName().equals("id")) { id=Integer.parseInt(childs.item(j).getFirstChild().getNodeValue().trim()); } if(childs.item(j).getNodeName().equals("name")) { n=childs.item(j).getFirstChild().getNodeValue(); } if(childs.item(j).getNodeName().equals("downloadDate")) { String s=childs.item(j).getFirstChild().getNodeValue().trim(); long dl = Long.valueOf(s).longValue(); d= new Date(dl); } if(childs.item(j).getNodeName().equals("url")) { ; try{u=new URL(childs.item(j).getFirstChild().getNodeValue().trim());} catch(MalformedURLException mue){mue.printStackTrace();} } if(childs.item(j).getNodeName().equals("lastModified")) { //System.out.println(childs.item(j).getFirstChild().getNodeValue()); String s=childs.item(j).getFirstChild().getNodeValue().trim(); long dl = Long.valueOf(s).longValue(); lm= new Date(dl); } } } //info.add(id,new CatalogInfo(n,d,lm,u)); if(id>=(N-1)) { CatalogInfo[] temp=new CatalogInfo[N+1]; for(int k=0;k<N;k++) { temp[k]=new CatalogInfo(); temp[k].insert(info[k]); } N=N+1; info=temp; } info[id]= new CatalogInfo(n,d,lm,u); } if (!vuoto) firstFreeID=getFirstFree(); else firstFreeID=0; } } public int updatePeerReceivedCatalog(org.w3c.dom.Document cat,Date dd,Date lm,URL curl,String cname) { int s=0; int id=-1; id=search(cname); if(id>=0) { try{ Transformer transformer= TransformerFactory.newInstance().newTransformer(); //String systemID=doc.getDoctype().getSystemId(); //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"prodotti.dtd"); transformer.setOutputProperty(OutputKeys.METHOD,"xml"); transformer.setOutputProperty(OutputKeys.INDENT,"yes"); DOMSource source=new DOMSource(cat); FileOutputStream fos=new FileOutputStream("catalog"+id+".xml"); StreamResult result = new StreamResult (fos); transformer.transform(source,result); } catch(TransformerConfigurationException tce){System.out.println("tce");return -1;} catch(TransformerException te){System.out.println("te");return -2;} catch(IOException ioe){System.out.println("ioe");return -3;} } updateCatalog(lm,dd, curl, id); boolean b=save(); if(!b) s=1; return s; } public int insertPeerReceivedCatalog(org.w3c.dom.Document cat,Date dd,Date lm,URL curl,String cname) { int s=0; int id=-1; id=getFirstFree(); if (search(cname)>=0) { return -1; } if(id>=0) { try{ Transformer transformer= TransformerFactory.newInstance().newTransformer(); //String systemID=doc.getDoctype().getSystemId(); //transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM,"prodotti.dtd"); transformer.setOutputProperty(OutputKeys.METHOD,"xml"); transformer.setOutputProperty(OutputKeys.INDENT,"yes"); DOMSource source=new DOMSource(cat); FileOutputStream fos=new FileOutputStream("catalog"+id+".xml"); StreamResult result = new StreamResult (fos); transformer.transform(source,result); } catch(TransformerConfigurationException tce){System.out.println("tce");return -1;} catch(TransformerException te){System.out.println("te");return -2;} catch(IOException ioe){System.out.println("ioe");return -3;} addCatalog(id,new CatalogInfo(cname,dd,lm,curl)); } float freePercent=(float)(N-catalogNumber)/100; if(freePercent<20) this.increaseSize((20*catalogNumber)/100); return s; } private void updateCatalog(Date dd,URL src,int id) { Date lm=null; try{lm=new Date(src.openConnection().getLastModified());} catch(IOException ioe){ioe.printStackTrace();} NodeList el=doc.getElementsByTagName("catalog"); for (int i=0;i<el.getLength();i++) { Element e=(Element)el.item(i); NodeList n=e.getChildNodes(); for (int j=0;j<n.getLength();j++) { if(n.item(j).getNodeName().equals("id")) { Element k=(Element)n.item(j); NodeList tmp=k.getChildNodes(); Node nod=tmp.item(0); String ids = Integer.toString(id); if(nod.getNodeValue().equals(ids))
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -