📄 catalogmanager.java
字号:
{ Element root=(Element)el.item(i); NodeList chr=root.getChildNodes(); for(int l=0;l<chr.getLength();l++) { if(chr.item(l).getNodeName().equals("downloadDate")) { Element ddd=(Element)chr.item(l); NodeList ddc=ddd.getChildNodes(); ddc.item(0).setNodeValue(String.valueOf(dd)); } if(chr.item(l).getNodeName().equals("lastModified")) { Element lmd=(Element)chr.item(l); NodeList lmdc=lmd.getChildNodes(); lmdc.item(0).setNodeValue(String.valueOf(lm)); } } } } } } save(); } private void updateCatalog(Date lm,Date dd,URL src,int id) { 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)) { Element root=(Element)el.item(i); NodeList chr=root.getChildNodes(); for(int l=0;l<chr.getLength();l++) { if(chr.item(l).getNodeName().equals("downloadDate")) { Element ddd=(Element)chr.item(l); NodeList ddc=ddd.getChildNodes(); ddc.item(0).setNodeValue(String.valueOf(dd)); } if(chr.item(l).getNodeName().equals("lastModified")) { Element lmd=(Element)chr.item(l); NodeList lmdc=lmd.getChildNodes(); lmdc.item(0).setNodeValue(String.valueOf(lm)); } } } } } } save(); } public int refreshCatalog(String cn,URL u) { int s=0; int id=search(cn); URL src= u; try{ InputStream is=src.openStream(); File outputFile = new File("catalog"+id+".xml"); FileWriter out = new FileWriter(outputFile); int c; while ((c = is.read()) != -1) out.write(c); is.close(); out.close();} catch (IOException ioe){return 1;} if (id<0){ return 2;} else { Date d=new Date(System.currentTimeMillis()); this.updateCatalog(d, src,id); } boolean b=save(); if(!b) s=1; return s; } public int downloadCatalog(String cn,URL u) { int s=0; if (search(cn)>=0) { return 0; } URL src= u; try{ InputStream is=src.openStream(); File outputFile = new File("catalog"+firstFreeID+".xml"); FileWriter out = new FileWriter(outputFile); int c; while ((c = is.read()) != -1) out.write(c); is.close(); out.close();} catch (IOException ioe){return 1;} int id=getFirstFree(); if (id<0){ return 2;} else { Date d=new Date(System.currentTimeMillis()); Date lm=null; doc.createElement("catalog"); try{lm=new Date(src.openConnection().getLastModified());} catch(IOException ioe){s=3;ioe.printStackTrace();} CatalogInfo ci=new CatalogInfo(cn,d,lm,src); addCatalog(id,ci); } float freePercent=(float)(N-catalogNumber)/100; if(freePercent<20) this.increaseSize((20*catalogNumber)/100); return s; } private int addCatalog(int id,CatalogInfo c) { int s=0; //System.out.println("id "+id ); //System.out.println("cname "+c.getCatalogName()); if(!isFull()) { //Date t1= new Date(c.getDownloadDate()); //Date t2= new Date(c.getLastModified()); //CatalogInfo n=new CatalogInfo(c.getCatalogName(),t1,t2,c.getCatalogUrl()); //System.out.println("cname "+n.getCatalogName()); info[id].insert(c); Node cat=doc.createElement("catalog"); NodeList l=doc.getElementsByTagName("cmdata"); Node r=l.item(0); r.appendChild(cat); firstFreeID=getFirstFree(); Node idE=doc.createElement("id"); Node idT=doc.createTextNode(String.valueOf(id)); idE.appendChild(idT); cat.appendChild(idE); Node nameE=doc.createElement("name"); Node nameT=doc.createTextNode(c.getCatalogName()); nameE.appendChild(nameT); cat.appendChild(nameE); Node downloadDateE=doc.createElement("downloadDate"); Node downloadDateT=doc.createTextNode(String.valueOf(c.getDownloadDate())); downloadDateE.appendChild(downloadDateT); cat.appendChild(downloadDateE); Node lastModifiedE=doc.createElement("lastModified"); Node lastModifiedT=doc.createTextNode(String.valueOf(c.getLastModified())); lastModifiedE.appendChild(lastModifiedT); cat.appendChild(lastModifiedE); Node urlE=doc.createElement("url"); Node urlT=doc.createTextNode(c.getCatalogUrl().toString()); urlE.appendChild(urlT); cat.appendChild(urlE); boolean b=save(); if(!b) s=1; else { firstFreeID=getFirstFree(); catalogNumber++; } } else s=1; return s; } private Element search(int id) { Element e=null; CatalogInfo temp; temp=info[id]; String cname=temp.getCatalogName(); NodeList nl=doc.getElementsByTagName("catalog"); int i=0; while ((i<nl.getLength())&&(e==null)) { Node f=nl.item(i).getFirstChild(); if (f.getNodeType()==Node.ELEMENT_NODE) { if (f.getNodeName().equals("name")) { if(f.getFirstChild().getNodeValue().equals(cname)) { e=(Element)f; } } } i++; } return e; } private boolean removeCatalog(String cname) { boolean s=true; int id=-1; id=search(cname); if(id>0) { Element e=search(id); Element r=(Element)e.getParentNode(); r.removeChild(e); info[id]=new CatalogInfo(); save(); File f=new File("catalog"+id+".xml"); f.delete(); firstFreeID=id; catalogNumber--; } else s=false; if (s){ float freePercent=(float)(N-catalogNumber)/100; if(freePercent>50) this.decreaseSize((20*catalogNumber)/100); } return s; } public int search (String cname) { int s=-1; int k=0; if(catalogNumber>0) {while((k<N)&&(s==-1)) { if((info[k]!=null)) { CatalogInfo temp=info[k]; if(temp.getCatalogName().equals(cname)) s=k; } k++; } } return s; } private boolean save() { while (lock) {try{Thread.sleep(5000);}catch(InterruptedException ie){}} lock=true; boolean s=true; try{ System.out.println("saving"); 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(doc); FileOutputStream fos=new FileOutputStream(catalogData); StreamResult result = new StreamResult (fos); transformer.transform(source,result); } catch(TransformerConfigurationException tce){System.out.println("tce");return false;} catch(TransformerException te){System.out.println("te");return false;} catch(IOException ioe){System.out.println("ioe");return false;} lock=false; return s; } private int getFirstFree() { int i=-1; int k=0; while((k<N)&&(i==-1)) { if (info[k].getCatalogName().equals("*")) i=k; k++; } return i; } public Date getCatalogDownloadDate(String cname) { Date dd=null; int id=search(cname); if(id>=0) { CatalogInfo temp=info[id]; long t=temp.getDownloadDate(); dd=new Date(t); } return dd; } public Date getCatalogLastModified(String cname) { Date lm=null; int id=search(cname); if(id>=0) { CatalogInfo temp= info[id]; long t=temp.getLastModified(); lm=new Date(t); } return lm; } public Document getCatalog (String cname) { Document docAr=null; DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance(); dbf.setValidating(false); DocumentBuilder db=null; //NodeList nl=null; try{db = dbf.newDocumentBuilder();} catch (ParserConfigurationException pce){pce.printStackTrace();} int id=search(cname); if(id>=0) { File a=new File ("catalog"+id+".xml"); try{docAr=db.parse(a);} catch(SAXException se){se.printStackTrace();} catch(IOException ioe){ioe.printStackTrace();} } return docAr; } public int getN() { return catalogNumber; } public String getCatalogName(int n) { return info[n].getCatalogName(); } public Document getNCatalog (int n) { Document docAr=null; DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance(); dbf.setValidating(false); DocumentBuilder db=null; //NodeList nl=null; try{db = dbf.newDocumentBuilder();} catch (ParserConfigurationException pce){pce.printStackTrace();} int id=n; if(id<catalogNumber) { File a=new File ("catalog"+id+".xml"); try{docAr=db.parse(a);} catch(SAXException se){se.printStackTrace();} catch(IOException ioe){ioe.printStackTrace();} } return docAr; } public CatalogInfo [] getInfo() { return info; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -