📄 xmlauthenticationparser.java
字号:
/******************************************************************************** Product of NIST/ITL Advanced Networking Technologies Division (ANTD) ** Creator: O. Deruelle (deruelle@nist.gov) ** Questions/Comments: nist-sip-dev@antd.nist.gov ********************************************************************************/package gov.nist.security.authentication;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;/** Parser for a XML authentication file *@author deruelle@antd.nist.gov */public class XMLAuthenticationParser extends DefaultHandler { /**user list*/ private Vector usersTagList; /**group list*/ private Vector groupsTagList; /**a user*/ private UserTag userTag; /**the parser*/ private XMLReader saxParser; /**the file to parse*/ private String file; /**realm of the users*/ private String realm; /**group of a user*/ private String group; /** start the parsing * * @param fileLocation - file to parse */ public XMLAuthenticationParser(String fileLocation) { try{ 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); } 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(); } file=fileLocation; } /** * Retrieve the realm of the users * @return the realm */ public String getRealm() { return realm; } /** * Retrieve the users * @return the users */ public Vector getUsersTagList() { return usersTagList; } public Vector getGroupsList(){ return groupsTagList; } //=========================================================== // SAX DocumentHandler methods //=========================================================== public void startDocument() throws SAXException { try { System.out.println("XMLAuthenticationParser, startDocument(): "+ " Parsing XML passwords file"); } catch (Exception e) { throw new SAXException("XMLAuthenticationParser error", e); } } public void endDocument() throws SAXException { try { System.out.println("XMLAuthenticationParser, endDocument(): "+ " XML passwords 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("authentication") ==0 ) { usersTagList=new Vector(); groupsTagList=new Vector(); String userRealm= attrs.getValue("realm"); if (userRealm!=null) { userRealm=userRealm.trim(); realm=userRealm; } } if(element.compareToIgnoreCase("group") ==0 ) { String userGroup= attrs.getValue("name"); if (userGroup!=null) { userGroup=userGroup.trim(); group=userGroup; groupsTagList.addElement(group); } } if (element.compareToIgnoreCase("user") ==0 ) { userTag=new UserTag(); String userName= attrs.getValue("name"); if (userName!=null) { userName=userName.trim(); userTag.setUserName(userName); } String userPassword= attrs.getValue("password"); if (userPassword!=null) { userPassword=userPassword.trim(); userTag.setUserPassword(userPassword); } String uri= attrs.getValue("uri"); if (uri!=null) { uri=uri.trim(); userTag.setUri(uri); } userTag.setUserGroup(group); } } public void endElement(String namespaceURI, String sName, // simple name String qName // qualified name ) throws SAXException { String element=qName; if (element.compareToIgnoreCase("user") ==0 ) { usersTagList.addElement(userTag); } } 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 usersTagList - the users */ public void writeToXMLFile(Vector usersTagList) { Vector groupList=new Vector(); try{ String newXMLContent="<?xml version='1.0' encoding='us-ascii'?> \n"+ "<AUTHENTICATION realm=\""+ realm +"\"> \n"; for (int i=0;i<usersTagList.size();i++) { UserTag userTag=(UserTag)usersTagList.elementAt(i); String userGroup=userTag.getUserGroup(); if(!groupList.contains(userGroup)) groupList.addElement(userGroup); } for(int i=0;i<groupList.size();i++) { String group=(String)groupList.elementAt(i); newXMLContent+="<Group name=\""+ group +"\">\n"; for(int j=0;j<usersTagList.size();j++){ UserTag userTag=(UserTag)usersTagList.elementAt(i); String userGroup=userTag.getUserGroup(); if(group.equals(userGroup)){ newXMLContent+="<User name=\""+ userTag.getUserName()+ "\" password=\""+userTag.getUserPassword()+"\"/>\n"; } } newXMLContent+="</Group>"; } newXMLContent+="</AUTHENTICATION>\n"; //writeFile(null,file,newXMLContent); System.out.println("XMLAuthenticationParser, writeToXMLFile(), New"+ " authentications wrote to the file!!"); } catch(Exception e) { System.out.println("ProxyDebug, XMLAuthenticationParser, writeToXMLFile(), exception"+ " raised:"); 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 text - String to set */ public void writeFile(String inFile,String outFile, String text) { // we readthis 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(text); pw.close(); fileWriter.close(); } catch(Exception e) { System.out.println("ProxyDebug, XMLAuthenticationParser, writeFile(), Unable"+ " to write in the XML authentication file!!"); e.printStackTrace(); } } }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -