?? iomanager.java
字號(hào):
/** * Copyright 2004 Carlos Silva A. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package jgantt.model;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import java.text.ParseException;import java.util.Date;import java.util.zip.ZipEntry;import java.util.zip.ZipFile;import java.util.zip.ZipOutputStream;import org.xml.sax.SAXException;import com.csa.lib.xml.dom.DocumentWriter;import com.csa.lib.xml.dom.Node;/** * IOManager administra la lectura/escritura de proyectos en archivo. * * <p>$Date: 2004/04/25 05:53:20 $</p> * @version $Revision: 1.7 $ * @author Carlos Silva */public class IOManager { public static final String XML_PROJECT = "project.xml"; public static final String XML_GRAPH = "graph.xml"; public static final String XML_VIEWOPTIONS = "viewoptions.xml"; public static final String XML_TREEOPTIONS = "treeoptions.xml"; public static final String STD_FILE_EXT = ".lgantt"; public static final String SEC_FILE_EXT = ".xml"; public static Project createEmptyProject(){ DateOrganizer dateOrg= new DateOrganizer(); Project project = new Project(dateOrg); project.setName("New Project"); project.setDateOrganizer(dateOrg); project.setStartDate(dateOrg.getNormalizedStartDate(new Date())); project.clearUndoRedo(); return project; } /** * graba el contenido en un archivo. * Si el archivo tiene extension .lgantt genera un archivo comprimido. * Finalmente elimina el indicador de dirty. * * @param f * @throws IOException * @throws ParserConfigurationException */ public static void saveAs(Project p, File f) throws IOException{ if (f.getName().endsWith(STD_FILE_EXT)) saveAsZip(p,f); else saveAsXML(p,f); p.clearUndoRedo(); } /** * Graba un archivo XML con solo el contenido del proyecto. * @param f * @throws IOException * @throws ParserConfigurationException */ public static void saveAsXML(Project p, File f) throws IOException{ FileOutputStream out = new FileOutputStream(f); serialize(p,out); out.close(); } /** * Graba un archivo XML con el contenido del proyecto las definiciones graficas * y las opciones de visualizacion. * @param f * @throws IOException * @throws ParserConfigurationException */ public static void saveAsZip(Project p,File f) throws IOException { FileOutputStream fos = new FileOutputStream(f); ZipOutputStream zos = new ZipOutputStream(fos); zos.putNextEntry(new ZipEntry(XML_PROJECT)); serialize(p,zos); zos.putNextEntry(new ZipEntry(XML_GRAPH)); serialize(p.getGraphColors(), zos); zos.putNextEntry(new ZipEntry(XML_VIEWOPTIONS)); serialize(p.getViewOptions(), zos); zos.putNextEntry(new ZipEntry(XML_TREEOPTIONS)); serialize(p.getTreeColors(), zos); zos.close(); } /** * Carga un archivo de proyecto * Si el archivo tiene extension .lgantt lo carga como un archivo comprimido. * @param f * @return * @throws ParseException * @throws GanttException * @throws FileNotFoundException * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Project loadFrom(File f) throws ParseException, GanttException, FileNotFoundException, SAXException, IOException { //return loadFromDbForTest(); System.out.println("Project loadFrom==="+f.getAbsolutePath()+f.getName()); if (f.getName().endsWith(STD_FILE_EXT)) return loadFromZip(f); return loadFromXML(f); } /** * Carga un archivo desde un xml * @param f * @return * @throws ParseException * @throws GanttException * @throws FileNotFoundException * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Project loadFromXML(File f) throws ParseException, GanttException, FileNotFoundException, SAXException, IOException{ Node doc = XMLModel.readDocument(new FileInputStream(f)); return XMLModel.readProject(doc); } public static Project loadFromDbForTest() throws ParseException, GanttException,SAXException{ Node mainNode = new Node("DOCMENT"); Node childNode = new Node("project"); childNode = AddProjectChild(childNode); mainNode.appendChild(childNode); return XMLModel.readProject(mainNode); } public static Node AddProjectChild(Node project) throws ParseException, GanttException,SAXException{ //name project.appendChild(new Node("name","LGanttProject示例2006-05-07")); //start-date project.appendChild(new Node("start-date","2006-05-01")); // Node dateorganizer = new Node("date-organizer"); dateorganizer.appendChild(new Node("day-start","9:0")); dateorganizer.appendChild(new Node("work-hours","8:0")); dateorganizer.appendChild(new Node("work-hours","8:0")); //labor-days Node labordays = new Node("labor-days"); labordays.appendChild(new Node("monday")); labordays.appendChild(new Node("tuesday")); labordays.appendChild(new Node("wednesday")); labordays.appendChild(new Node("thursday")); labordays.appendChild(new Node("friday")); dateorganizer.appendChild(labordays); dateorganizer.appendChild(new Node("holidays")); dateorganizer.appendChild(new Node("extra-days")); project.appendChild(dateorganizer); Node resource = null; Node effect = null; for(int i=1;i<6;i++){ resource = new Node("resource"); resource.appendChild("id",Integer.toString(i)); resource.appendChild("name","id"+Integer.toString(i)); resource.appendChild("alias","name"+Integer.toString(i)); resource.appendChild("initials","initials"+Integer.toString(i)); effect = new Node("effect"); effect.appendChild(new Node("total",Integer.toString(i*10))); effect.appendChild(new Node("worked","0")); resource.appendChild(effect); project.appendChild(resource); } Node task = new Node("task"); task.appendChild(new Node("id","0")); task.appendChild(new Node("name","MainTask")); task.appendChild(new Node("length","128")); task.appendChild(new Node("completed","0")); task.appendChild(new Node("comments","MainTask")); task.appendChild(new Node("notes","MainTask")); task = AddTaskChildForTest(task); project.appendChild(task); //project.appendChild(project); return project; } public static Node AddTaskChildForTest(Node mainTask) throws ParseException, GanttException,SAXException{ Node task = null; task = new Node("task"); task.appendChild("id","1"); task.appendChild("name","任務(wù)1"); task.appendChild("length","35"); task.appendChild("completed","0"); task.appendChild("comments","任務(wù)1"); task.appendChild("notes","任務(wù)1"); mainTask.appendChild(task); System.out.println("======"); return mainTask; //name } /** * Carga un proyecto desde un archivo comprimido. * El archivo debe llamarse project.xml. * @param f * @return * @throws ParseException * @throws GanttException * @throws FileNotFoundException * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ public static Project loadFromZip(File f) throws ParseException, GanttException, FileNotFoundException, SAXException, IOException{ ZipFile zf = new ZipFile(f); ZipEntry ze = zf.getEntry(XML_PROJECT); InputStream is = zf.getInputStream(ze); Node prjXML = XMLModel.readDocument(is); Project prj = XMLModel.readProject(prjXML); is.close(); ze = zf.getEntry(XML_GRAPH); if (ze!=null) { is = zf.getInputStream(ze); prj.graphColors = loadGraphColors(is); is.close(); } ze = zf.getEntry(XML_VIEWOPTIONS); if (ze!=null) { is = zf.getInputStream(ze); Node graphXML = XMLModel.readDocument(is); ViewOptions vo = prj.getViewOptions(); XMLModel.readViewOptions(vo, graphXML); vo.recalc(); is.close(); } ze = zf.getEntry(XML_TREEOPTIONS); if (ze!=null) { is = zf.getInputStream(ze); Node graphXML = XMLModel.readDocument(is); TaskTreeOptions ttc = prj.getTreeColors(); XMLModel.readTaskTreeColors(ttc, graphXML); is.close(); } zf.close(); return prj; } public static GraphColors loadGraphColors(InputStream is) throws SAXException, IOException, ParseException, GanttException{ Node graphXML = XMLModel.readDocument(is); return XMLModel.readGraphColors(graphXML); } /** * Escribir la estructura del proyecto en xml * @param out * @throws IOException * @throws ParserConfigurationException */ static void serialize(Project p, OutputStream out)throws IOException{ DocumentWriter dw = new DocumentWriter(p.toXMLElement()); dw.write(out); } /** * Escribir el formato grafico en xml * @param out * @throws IOException * @throws ParserConfigurationException */ public static void serialize(GraphColors gc, OutputStream out)throws IOException{ DocumentWriter dw = new DocumentWriter(XMLModel.toXML(gc)); dw.write(out); } /** * Escribir las opciones de visualizacion en xml * @param out * @throws IOException * @throws ParserConfigurationException */ static void serialize(ViewOptionsData vo, OutputStream out)throws IOException{ DocumentWriter dw = new DocumentWriter(XMLModel.toXML(vo)); dw.write(out); } /** * Escribir las opciones de visualizacion en xml * @param out * @throws IOException * @throws ParserConfigurationException */ static void serialize(TaskTreeOptions ttc, OutputStream out)throws IOException{ DocumentWriter dw = new DocumentWriter(XMLModel.toXML(ttc)); dw.write(out); }}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -