List of usage examples for javax.xml.parsers DocumentBuilderFactory setIgnoringComments
public void setIgnoringComments(boolean ignoreComments)
From source file:com.l2jfree.gameserver.document.DocumentBase.java
final void parse() { try {//from w ww . j a v a 2 s . co m DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setIgnoringComments(true); parseDocument(factory.newDocumentBuilder().parse(_file)); } catch (Exception e) { _log.fatal("Error in file: " + _file, e); } }
From source file:com.l2jfree.gameserver.instancemanager.ZoneManager.java
private void load() { Document doc = null;/*from w w w . j a v a 2 s .c o m*/ for (File f : Util.getDatapackFiles("zone", ".xml")) { int count = 0; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setIgnoringComments(true); doc = factory.newDocumentBuilder().parse(f); } catch (Exception e) { _log.fatal("ZoneManager: Error loading file " + f.getAbsolutePath(), e); continue; } try { count = parseDocument(doc); } catch (Exception e) { _log.fatal("ZoneManager: Error in file " + f.getAbsolutePath(), e); continue; } _log.info("ZoneManager: " + f.getName() + " loaded with " + count + " zones"); } }
From source file:net.sf.commons.ssh.directory.Directory.java
private Document getDocument(String resource) throws ParserConfigurationException, SAXException, IOException { final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setCoalescing(true); documentBuilderFactory.setIgnoringComments(true); documentBuilderFactory.setNamespaceAware(false); documentBuilderFactory.setValidating(false); final DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder(); return documentBuilder.parse(Directory.class.getResource(resource).toString()); }
From source file:org.dspace.submit.lookup.ArXivService.java
protected List<Record> search(String query, String arxivid, int max_result) throws IOException, HttpException { List<Record> results = new ArrayList<Record>(); HttpGet method = null;/* w ww . j a v a2 s .c o m*/ try { HttpClient client = new DefaultHttpClient(); HttpParams params = client.getParams(); params.setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); try { URIBuilder uriBuilder = new URIBuilder("http://export.arxiv.org/api/query"); uriBuilder.addParameter("id_list", arxivid); uriBuilder.addParameter("search_query", query); uriBuilder.addParameter("max_results", String.valueOf(max_result)); method = new HttpGet(uriBuilder.build()); } catch (URISyntaxException ex) { throw new HttpException(ex.getMessage()); } // Execute the method. HttpResponse response = client.execute(method); StatusLine responseStatus = response.getStatusLine(); int statusCode = responseStatus.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { if (statusCode == HttpStatus.SC_BAD_REQUEST) throw new RuntimeException("arXiv query is not valid"); else throw new RuntimeException("Http call failed: " + responseStatus); } try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document inDoc = db.parse(response.getEntity().getContent()); Element xmlRoot = inDoc.getDocumentElement(); List<Element> dataRoots = XMLUtils.getElementList(xmlRoot, "entry"); for (Element dataRoot : dataRoots) { Record crossitem = ArxivUtils.convertArxixDomToRecord(dataRoot); if (crossitem != null) { results.add(crossitem); } } } catch (Exception e) { throw new RuntimeException("ArXiv identifier is not valid or not exist"); } } finally { if (method != null) { method.releaseConnection(); } } return results; }
From source file:edu.internet2.middleware.shibboleth.common.config.SpringDocumentLoader.java
/** {@inheritDoc} */ public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler, int validationMode, boolean namespaceAware) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", "http://www.w3.org/2001/XMLSchema"); factory.setCoalescing(true);/*from w w w. j a va 2s.co m*/ factory.setIgnoringComments(true); factory.setNamespaceAware(true); factory.setValidating(true); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setErrorHandler(new LoggingErrorHandler(log)); builder.setEntityResolver(new ClasspathResolver()); return builder.parse(inputSource); }
From source file:com.connexta.arbitro.finder.impl.FileBasedPolicyFinderModule.java
/** * Private helper that tries to load the given file-based policy, and * returns null if any error occurs.//from w w w .j a va 2 s.c o m * * @param policyFile file path to policy * @param finder policy finder * @return <code>AbstractPolicy</code> */ private AbstractPolicy loadPolicy(String policyFile, PolicyFinder finder) { AbstractPolicy policy = null; InputStream stream = null; try { // create the factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true); factory.setNamespaceAware(true); factory.setValidating(false); // create a builder based on the factory & try to load the policy DocumentBuilder db = factory.newDocumentBuilder(); stream = new FileInputStream(policyFile); Document doc = db.parse(stream); // handle the policy, if it's a known type Element root = doc.getDocumentElement(); String name = DOMHelper.getLocalName(root); if (name.equals("Policy")) { policy = Policy.getInstance(root); } else if (name.equals("PolicySet")) { policy = PolicySet.getInstance(root, finder); } } catch (Exception e) { // just only logs log.error("Fail to load policy : " + policyFile, e); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { log.error("Error while closing input stream"); } } } if (policy != null) { policies.put(policy.getId(), policy); } return policy; }
From source file:org.fcrepo.auth.xacml.FedoraPolicyFinderModule.java
/** * Creates a new policy or policy set object from the given policy node * * @param policyBinary// ww w. j a v a 2 s . c o m * @return */ private AbstractPolicy loadPolicy(final FedoraBinary policyBinary) { String policyName = "unparsed"; try { // create the factory final DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setIgnoringComments(true); factory.setNamespaceAware(true); factory.setValidating(false); final DocumentBuilder db = factory.newDocumentBuilder(); // Parse the policy content final Document doc = db.parse(policyBinary.getContent()); // handle the policy, if it's a known type final Element root = doc.getDocumentElement(); final String name = root.getTagName(); policyName = PolicyUtil.getID(doc); if (name.equals("Policy")) { return Policy.getInstance(root); } else if (name.equals("PolicySet")) { return PolicySet.getInstance(root, finder); } else { // this isn't a root type that we know how to handle throw new Exception("Unknown root document type: " + name); } } catch (final Exception e) { LOGGER.error("Unable to parse policy from {}", policyName, e); } // a default fall-through in the case of an error return null; }
From source file:org.dspace.submit.lookup.CrossRefService.java
public List<Record> search(Context context, Set<String> dois, String apiKey) throws HttpException, IOException, JDOMException, ParserConfigurationException, SAXException { List<Record> results = new ArrayList<Record>(); if (dois != null && dois.size() > 0) { for (String record : dois) { try { HttpGet method = null;/*from w w w . java2 s .com*/ try { HttpClient client = new DefaultHttpClient(); client.getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, timeout); try { URIBuilder uriBuilder = new URIBuilder("http://www.crossref.org/openurl/"); uriBuilder.addParameter("pid", apiKey); uriBuilder.addParameter("noredirect", "true"); uriBuilder.addParameter("id", record); method = new HttpGet(uriBuilder.build()); } catch (URISyntaxException ex) { throw new HttpException("Request not sent", ex); } // Execute the method. HttpResponse response = client.execute(method); StatusLine statusLine = response.getStatusLine(); int statusCode = statusLine.getStatusCode(); if (statusCode != HttpStatus.SC_OK) { throw new RuntimeException("Http call failed: " + statusLine); } Record crossitem; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setIgnoringComments(true); factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db = factory.newDocumentBuilder(); Document inDoc = db.parse(response.getEntity().getContent()); Element xmlRoot = inDoc.getDocumentElement(); Element queryResult = XMLUtils.getSingleElement(xmlRoot, "query_result"); Element body = XMLUtils.getSingleElement(queryResult, "body"); Element dataRoot = XMLUtils.getSingleElement(body, "query"); crossitem = CrossRefUtils.convertCrossRefDomToRecord(dataRoot); results.add(crossitem); } catch (Exception e) { log.warn(LogManager.getHeader(context, "retrieveRecordDOI", record + " DOI is not valid or not exist: " + e.getMessage())); } } finally { if (method != null) { method.releaseConnection(); } } } catch (RuntimeException rt) { log.error(rt.getMessage(), rt); } } } return results; }
From source file:com.l2jfree.gameserver.datatables.EnchantHPBonusData.java
private EnchantHPBonusData() { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);//w ww . j a v a2s. c o m factory.setIgnoringComments(true); File file = new File(Config.DATAPACK_ROOT, "data/enchantHPBonus.xml"); Document doc = null; try { doc = factory.newDocumentBuilder().parse(file); } catch (Exception e) { _log.warn("", e); return; } for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling()) { if ("list".equalsIgnoreCase(n.getNodeName())) { for (Node d = n.getFirstChild(); d != null; d = d.getNextSibling()) { if ("enchantHP".equalsIgnoreCase(d.getNodeName())) { NamedNodeMap attrs = d.getAttributes(); Node att; boolean fullArmor; att = attrs.getNamedItem("grade"); if (att == null) { _log.warn("[EnchantHPBonusData] Missing grade, skipping"); continue; } int grade = Integer.parseInt(att.getNodeValue()); att = attrs.getNamedItem("fullArmor"); if (att == null) { _log.warn("[EnchantHPBonusData] Missing fullArmor, skipping"); continue; } fullArmor = Boolean.valueOf(att.getNodeValue()); att = attrs.getNamedItem("values"); if (att == null) { _log.warn("[EnchantHPBonusData] Missing bonus id: " + grade + ", skipping"); continue; } StringTokenizer st = new StringTokenizer(att.getNodeValue(), ","); int tokenCount = st.countTokens(); Integer[] bonus = new Integer[tokenCount]; for (int i = 0; i < tokenCount; i++) { Integer value = Integer.decode(st.nextToken().trim()); if (value == null) { _log.warn("[EnchantHPBonusData] Bad Hp value!! grade: " + grade + " FullArmor? " + fullArmor + " token: " + i); value = 0; } bonus[i] = value; } if (fullArmor) _fullArmorHPBonus.set(grade, bonus); else _singleArmorHPBonus.set(grade, bonus); } } } } if (_fullArmorHPBonus.isEmpty() && _singleArmorHPBonus.isEmpty()) return; int count = 0; for (L2Item item0 : ItemTable.getInstance().getAllTemplates()) { if (!(item0 instanceof L2Equip)) continue; final L2Equip item = (L2Equip) item0; if (item.getCrystalType() == L2Item.CRYSTAL_NONE) continue; boolean shouldAdd = false; // normally for armors if (item instanceof L2Armor) { switch (item.getBodyPart()) { case L2Item.SLOT_CHEST: case L2Item.SLOT_FEET: case L2Item.SLOT_GLOVES: case L2Item.SLOT_HEAD: case L2Item.SLOT_LEGS: case L2Item.SLOT_BACK: case L2Item.SLOT_FULL_ARMOR: case L2Item.SLOT_UNDERWEAR: case L2Item.SLOT_L_HAND: shouldAdd = true; break; } } // shields in the weapons table else if (item instanceof L2Weapon) { switch (item.getBodyPart()) { case L2Item.SLOT_L_HAND: shouldAdd = true; break; } } if (shouldAdd) { count++; item.attach(new FuncTemplate(null, "EnchantHp", Stats.MAX_HP, 0x60, 0)); } } _log.info("Enchant HP Bonus registered for " + count + " items!"); }
From source file:com.l2jfree.gameserver.datatables.MerchantPriceConfigTable.java
public void loadXML() throws SAXException, IOException, ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true);/*w ww .j av a 2 s . com*/ factory.setIgnoringComments(true); File file = new File(Config.DATAPACK_ROOT, "data/" + MPCS_FILE); if (file.exists()) { int defaultPriceConfigId; Document doc = factory.newDocumentBuilder().parse(file); Node n = doc.getDocumentElement(); Node dpcNode = n.getAttributes().getNamedItem("defaultPriceConfig"); if (dpcNode == null) { throw new IllegalStateException("merchantPriceConfig must define an 'defaultPriceConfig'"); } defaultPriceConfigId = Integer.parseInt(dpcNode.getNodeValue()); MerchantPriceConfig mpc; for (n = n.getFirstChild(); n != null; n = n.getNextSibling()) { mpc = parseMerchantPriceConfig(n); if (mpc != null) { _mpcs.put(mpc.getId(), mpc); } } MerchantPriceConfig defaultMpc = this.getMerchantPriceConfig(defaultPriceConfigId); if (defaultMpc == null) { throw new IllegalStateException("'defaultPriceConfig' points to an non-loaded priceConfig"); } _defaultMpc = defaultMpc; } }