⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xmlservicesparser.java

📁 First of all, the Applet-phone is a SIP User-Agent with audio and text messaging capabilities. But
💻 JAVA
字号:
/* * XMLServicesParser.java * * Created on July 31, 2003, 11:04 AM */package gov.nist.struts.webapp.upload;import java.io.FileReader;import java.io.FileWriter;import java.io.IOException;import java.io.PrintWriter;import java.util.Vector;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;import org.apache.log4j.Logger;/** * Class parsing the xml mapping file * @author  DERUELLE Jean */public class XMLServicesParser extends DefaultHandler {    /** log4j logging*/    static Logger logger = Logger.getLogger(XMLServicesParser.class);    /**list of all the users*/    private Vector usersServicesTagList=null;    /**tag representing a user*/    private UserServicesTag userServiceTag=null;    /**tag representing a service*/    private ServiceTag serviceTag=null;    /**list of services*/    private Vector services=null;    /**the parser*/    private XMLReader saxParser=null;    /**the file to parse*/    private String file=null;               /**      * start the parsing     * @param fileLocation - file to parse     */    public XMLServicesParser(String fileLocation) {        try {            java.io.File mapping=new java.io.File(fileLocation);                        if(mapping.length()>1){								SAXParserFactory saxParserFactory=SAXParserFactory.newInstance();				SAXParser saxParser = saxParserFactory.newSAXParser();				XMLReader xmlReader = saxParser.getXMLReader();				xmlReader.setContentHandler(this);				xmlReader.setFeature				("http://xml.org/sax/features/validation",false);				// parse the xml specification for the event tags.				xmlReader.parse(fileLocation);				            }            file=fileLocation;                   } catch (SAXParseException spe) {            spe.printStackTrace();        } catch (SAXException sxe) {            sxe.printStackTrace();        } catch (IOException ioe) {            // I/O error            ioe.printStackTrace();        } catch (Exception pce) {            // Parser with specified options can't be built            pce.printStackTrace();        }    }    /**     * Retrieve all the users and their services     * @return the users     */    public Vector getUsersServicesTagList() {        return usersServicesTagList;    }        //===========================================================    // SAX DocumentHandler methods    //===========================================================        public void startDocument() throws SAXException {        try {             logger.debug("Parsing XML mapping services file");        }         catch (Exception e) {            throw new SAXException("XMLAuthenticationParser error", e);        }    }    public void endDocument() throws SAXException {        try {           logger.debug("XML mapping services file parsed successfully!!!");        }         catch (Exception e) {            throw new SAXException("XMLAuthenticationParser error", e);        }    }    public void startElement(String namespaceURI,                             String lName, // local name                             String qName, // qualified name                             Attributes attrs)                             throws SAXException    {                String element=qName;        if (element.compareToIgnoreCase("mapping") ==0 ) {            usersServicesTagList=new Vector();                    }                        if (element.compareToIgnoreCase("user") ==0 ) {            userServiceTag=new UserServicesTag();            String userName= attrs.getValue("name");            if (userName!=null) {                userName=userName.trim();                userServiceTag.setUserName(userName);            }                       String userPassword= attrs.getValue("password");            if (userPassword!=null) {                userPassword=userPassword.trim();                userServiceTag.setPassword(userPassword);            }                        String uri= attrs.getValue("uri");            if (uri!=null) {                uri=uri.trim();                userServiceTag.setUri(uri);            }                    }        if (element.compareToIgnoreCase("services") ==0 ) {            services=new Vector();                    }            if (element.compareToIgnoreCase("service") ==0 ) {            serviceTag=new ServiceTag();            String mainClass= attrs.getValue("Main-Class");            if (mainClass!=null) {                mainClass=mainClass.trim();                serviceTag.setMainClass(mainClass);            }                       String fileName= attrs.getValue("fileName");            if (fileName!=null) {                fileName=fileName.trim();                serviceTag.setFileName(fileName);            }                        String toRewrite= attrs.getValue("toRewrite");            if (toRewrite!=null) {                toRewrite=toRewrite.trim();                if(toRewrite.equals("true"))                    serviceTag.setToRewrite(true);                else                    serviceTag.setToRewrite(false);            }			String serviceType= attrs.getValue("serviceType");			if (serviceType!=null) {				serviceType=serviceType.trim();				serviceTag.setServiceType(serviceType);							}        }    }        public void endElement(String namespaceURI,    String sName, // simple name    String qName  // qualified name    )    throws SAXException    {                String element=qName;        if (element.compareToIgnoreCase("service") ==0 ) {                        services.addElement(serviceTag);                        serviceTag=null;                  }        if (element.compareToIgnoreCase("services") ==0 ) {                        userServiceTag.setServices(services);                                  services=null;                              }        if (element.compareToIgnoreCase("user") ==0 ) {            usersServicesTagList.addElement(userServiceTag);                        userServiceTag=null;                  }    }    public void characters(char buf[], int offset, int len)    throws SAXException    {        String str = new String(buf, offset, len);    }    /**     * write to a xml file the users' information     * @param usersServicesTagList - the users     */    public void writeToXMLFile(Vector usersServicesTagList) {                try{            String newXMLContent="<?xml version='1.0' encoding='us-ascii'?> \n"+            "<mapping> \n";            for(int i=0;i<usersServicesTagList.size();i++){                newXMLContent+=((UserServicesTag)usersServicesTagList.elementAt(i)).toXMLString()+"\n";            }            newXMLContent+="</mapping>";                        writeFile(null,file,newXMLContent);                    }        catch(Exception e) {                        e.printStackTrace();        }    }        /** Utility method for reading a file and append in a other file the text     * @param inFile - file to read     * @param outFile - file to write     * @param xmlContent - String to set     */    public void writeFile(String inFile,String outFile, String xmlContent) {        // we read this file to obtain the options         try{            FileWriter fileWriter = new FileWriter(outFile,false);            PrintWriter pw = new PrintWriter(fileWriter,true);                        if (inFile!=null) {                FileReader in=new FileReader(inFile);                                int c;                int cpt=0;                StringBuffer s=new StringBuffer();                while ( ( c = in.read() ) != -1)                    cpt++;                in.close();                char[] cbuf=new char[cpt];                in=new FileReader(inFile);                in.read(cbuf,0,cpt);                in.close();                s.append(cbuf);                String content=s.toString();                pw.println(content);            }                        pw.println(xmlContent);            pw.close();            fileWriter.close();        }        catch(Exception e) {            e.printStackTrace();        }    }   }

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -