📄 badgesxmlparser.java
字号:
/* * BadgesXMLParser.java * * Created on July 31, 2003, 8:53 PM */package gov.nist.examples.bps.presenceserver;import gov.nist.examples.bps.gateway.GatewayDebug;import java.io.StringReader;import java.util.Vector;import javax.xml.parsers.ParserConfigurationException;import javax.xml.parsers.SAXParser;import javax.xml.parsers.SAXParserFactory;import org.xml.sax.Attributes;import org.xml.sax.InputSource;import org.xml.sax.SAXException;import org.xml.sax.SAXParseException;import org.xml.sax.XMLReader;import org.xml.sax.helpers.DefaultHandler;/** * * @author olivier * @version */public class BadgesXMLParser extends DefaultHandler { private Badge badge; private Vector badgesList; private XMLReader xmlReader; /** Start the parsing * @param file to parse */ public BadgesXMLParser() { try{ SAXParserFactory saxParserFactory=SAXParserFactory.newInstance(); SAXParser saxParser = saxParserFactory.newSAXParser(); xmlReader = saxParser.getXMLReader(); xmlReader.setContentHandler(this); xmlReader.setFeature ("http://xml.org/sax/features/validation",false); // parse the xml specification for the event tags. } catch (SAXParseException spe) { spe.printStackTrace(); } catch (SAXException sxe) { sxe.printStackTrace(); } catch(ParserConfigurationException pce){ pce.printStackTrace(); } } public Vector parseFile(String fileName) { try { xmlReader.parse(fileName); return badgesList; } catch (Exception e) { e.printStackTrace(); return null; } } public Vector parseString(String text) { try { StringReader stringReader=new StringReader(text); InputSource inputSource=new InputSource(stringReader); xmlReader.parse(inputSource); return badgesList; } catch (Exception e) { e.printStackTrace(); return null; } } //=========================================================== // SAX DocumentHandler methods //=========================================================== public void startDocument() throws SAXException { try { GatewayDebug.println("BadgesXMLParser, Parsing XML badge file"); badgesList=new Vector(); } catch (Exception e) { throw new SAXException("BadgesXMLParser, error:", e); } } public void endDocument() throws SAXException { try { GatewayDebug.println("BadgesXMLParser, XML Badge file parsed successfully!!!"); } catch (Exception e) { throw new SAXException("BadgesXMLParser 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("badge") ==0 ) { String identifier=attrs.getValue("identifier"); if (identifier==null || identifier.trim().equals("") ) { throw new SAXException("ERROR, BadgesXMLParser, startElement()"+ " the identifier attribute is missing, Badge not added!!!"); } String uri=attrs.getValue("uri"); String displayName=attrs.getValue("owner"); String rank=attrs.getValue("rank"); badge=new Badge(identifier,uri,displayName,rank); badgesList.addElement(badge); } if (element.compareToIgnoreCase("rank_constraint") ==0 ) { String rank=attrs.getValue("rank"); if (rank==null || rank.trim().equals("") ) { throw new SAXException("ERROR, BadgesXMLParser, startElement()"+ " the rank attribute in the rank_constraint is missing, Constraint not added!!!"); } else { RankConstraint rankConstraint=new RankConstraint(rank); badge.rankConstraints.addElement(rankConstraint); } } if (element.compareToIgnoreCase("location_constraint") ==0 ) { String name=attrs.getValue("name"); if (name==null || name.trim().equals("") ) { throw new SAXException("ERROR, BadgesXMLParser, startElement()"+ " the name attribute in the location_constraint is missing, Constraint not added!!!"); } else { LocationConstraint locationConstraint=new LocationConstraint(name); badge.locationConstraints.addElement(locationConstraint); } } if (element.compareToIgnoreCase("people_constraint") ==0 ) { String name=attrs.getValue("name"); if (name==null || name.trim().equals("") ) { throw new SAXException("ERROR, BadgesXMLParser, startElement()"+ " the name attribute in the people_constraint is missing, Constraint not added!!!"); } else { PeopleConstraint peopleConstraint=new PeopleConstraint(name); badge.peopleConstraints.addElement(peopleConstraint); } } } public void endElement(String namespaceURI, String sName, // simple name String qName // qualified name ) throws SAXException { String element=qName; } public void characters(char buf[], int offset, int len) throws SAXException { String str = new String(buf, offset, len); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -