List of usage examples for javax.xml.parsers DocumentBuilderFactory setValidating
public void setValidating(boolean validating)
From source file:org.infoscoop.service.GadgetService.java
public JSONObject getGadgetJson(String type, Locale locale) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setValidating(false); DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setEntityResolver(NoOpEntityResolver.getInstance()); Gadget gadget = gadgetDAO.select(type); Document gadgetDoc = builder.parse(new ByteArrayInputStream(gadget.getData())); Element gadgetEl = gadgetDoc.getDocumentElement(); JSONObject confJson = WidgetConfUtil.gadget2JSONObject(gadgetEl, null); return confJson; }
From source file:org.infoscoop.service.GadgetService.java
private static String gadget2JSON(List<Gadget> gadgetList, boolean isUpdate, Locale locale, int timeout, boolean enableI18N) throws Exception { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setValidating(false); DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setEntityResolver(NoOpEntityResolver.getInstance()); JSONObject json = new JSONObject(); for (int i = 0; i < gadgetList.size(); i++) { Gadget gadget = gadgetList.get(i); try {/*w w w. java 2s . c om*/ //TODO:enocoding Document gadgetDoc = (Document) XmlUtil .string2DomWithBomCode(new String(gadget.getData(), "UTF-8")); Element gadgetEl = gadgetDoc.getDocumentElement(); String type = gadget.getType(); I18NConverter i18n = new I18NConverter(locale, new MessageBundle.Factory.Upload(timeout, type).createBundles(gadgetDoc)); JSONObject confJson = WidgetConfUtil.gadget2JSONObject(gadgetEl, i18n); if (!type.startsWith("upload__")) type = "upload__" + type; json.put(type, confJson); } catch (Exception ex) { log.error("UploadGadget Parse failed: [" + gadget.getType() + "]", ex); } } String jsonStr = json.toString(1); if (enableI18N) { jsonStr = I18NUtil.resolve(I18NUtil.TYPE_WIDGET, jsonStr, locale); } return jsonStr; }
From source file:org.infoscoop.service.GadgetService.java
/** * @param type/*from w w w .j a v a 2s . com*/ * @param widgetConfJSON a widgetConf whose form is JSON. * @throws Exception */ public void updateGadget(String type, String gadgetJSON) throws Exception { if (type.startsWith("upload__")) type = type.substring(8); if (log.isInfoEnabled()) log.info("uploadGadget type=" + type); try { Gadget gadget = gadgetDAO.select(type); JSONObject json = new JSONObject(gadgetJSON); DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setValidating(false); DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setEntityResolver(NoOpEntityResolver.getInstance()); Document gadgetDoc = builder.parse(new ByteArrayInputStream(gadget.getData())); Element gadgetEl = gadgetDoc.getDocumentElement(); String resourceUrl = ""; if (json.has("ModulePrefs")) { JSONObject modulePrefs = json.getJSONObject("ModulePrefs"); for (Iterator<String> prefNames = modulePrefs.keys(); prefNames.hasNext();) { String prefName = prefNames.next(); if (!("autoRefresh".equals(prefName) || "title".equals(prefName) || "directory_title".equals(prefName) || "title_url".equals(prefName) || "height".equals(prefName) || "scrolling".equals(prefName) || "singleton".equals(prefName) || "resource_url".equals(prefName))) continue; String value = modulePrefs.getString(prefName); if (log.isInfoEnabled()) log.info("Modify Gadget's ModulePrefs@" + prefName + " to " + value + "."); Element modulePrefsEl = (Element) gadgetEl.getElementsByTagName("ModulePrefs").item(0); modulePrefsEl.setAttribute(prefName, value); if (prefName.equals("resource_url")) resourceUrl = value; } } Element iconElm = (Element) XPathAPI.selectSingleNode(gadgetDoc, "/Module/ModulePrefs/Icon"); if (iconElm != null) { String iconUrl = resourceUrl + iconElm.getTextContent(); GadgetIconDAO.newInstance().insertUpdate(type, iconUrl); } else { GadgetIconDAO.newInstance().insertUpdate(type, ""); } updateUserPrefNodes(gadgetDoc, json); if (json.has("WidgetPref")) WidgetConfService.updateWidgetPrefNode(gadgetDoc, gadgetEl, json.getJSONObject("WidgetPref")); gadgetDAO.update(type, "/", type + ".xml", XmlUtil.dom2String(gadgetDoc).getBytes("UTF-8")); } catch (Exception e) { log.error("update of widet configuration \"" + type + "\" failed.", e); throw e; } }
From source file:org.infoscoop.service.WidgetConfService.java
public String getWidgetConfsJson(String uid, Locale locale) throws Exception { try {// w w w . j a v a2 s .c o m List<String> useTypes = WidgetDAO.newInstance().getWidgetTypes(uid); List<String> widgetTypes = new ArrayList<String>(); List<String> gadgetTypes = new ArrayList<String>(); for (String type : useTypes) { if (type == null) type = ""; if (type.indexOf("g_") == 0) { if (type.indexOf("g_upload") == 0) gadgetTypes.add(type.substring(10).split("/")[0] + ".xml"); } else { widgetTypes.add(type); } } List<WidgetConf> widgetConfs = widgetConfDAO.selectAll(); JSONObject json = new JSONObject(); for (WidgetConf widgetConf : widgetConfs) { String type = widgetConf.getType(); json.put(type, WidgetConfUtil.widgetConf2JSONObject(widgetConf.getElement(), null, true)); } if (!gadgetTypes.isEmpty()) { DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); builderFactory.setValidating(false); DocumentBuilder builder = builderFactory.newDocumentBuilder(); builder.setEntityResolver(NoOpEntityResolver.getInstance()); List<Gadget> gadgets = GadgetDAO.newInstance().selectConfsByType(gadgetTypes); for (Gadget gadget : gadgets) { if (!gadget.getName().equalsIgnoreCase(gadget.getType() + ".xml")) continue; WidgetConfUtil.GadgetContext context = new WidgetConfUtil.GadgetContext() .setUrl("upload__" + gadget.getType()); Document gadgetDoc = builder.parse(new ByteArrayInputStream(gadget.getData())); JSONObject gadgetJson = WidgetConfUtil.gadget2JSONObject(gadgetDoc.getDocumentElement(), context.getI18NConveter(locale, gadgetDoc), true); String gadgetType = "g_upload__" + gadget.getType() + "/gadget"; gadgetJson = WidgetConfUtil.gadgetJSONtoPortalGadgetJSON(gadgetJson); gadgetJson.put("type", gadgetType); json.put(gadgetType, gadgetJson); } } return I18NUtil.resolve(I18NUtil.TYPE_WIDGET, json.toString(1), locale, true); } catch (Exception e) { log.error("Unexpected error occurred.", e); throw e; } }
From source file:org.infoscoop.widgetconf.MessageBundle.java
private static Map<String, String> parseResourceBundle(InputStream in) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); builder.setEntityResolver(NoOpEntityResolver.getInstance()); Document doc = builder.parse(in); NodeList msgs = doc.getElementsByTagName("msg"); Map<String, String> map = new HashMap<String, String>(); for (int j = 0; j < msgs.getLength(); j++) { Element msg = (Element) msgs.item(j); String name = msg.getAttribute("name"); String msgStr = XmlUtil.getChildText(msg).trim(); map.put(name, msgStr);//from w w w . j a va2 s . c o m } return map; }
From source file:org.jamwiki.utils.XMLUtil.java
/** * Given an <code>InputSource</code> object that points to XML data, parse * the XML and return a parsed <code>Document</code> object. * * @param source The InputSource object that points to XML data. * @param validating Set to <code>true</code> if the parser should * validate against a DTD.// w w w . j a va2 s .c o m * @return A parsed Document object. * @throws ParseException Thrown if any error occurs during parsing. */ public static Document parseXML(InputSource source, boolean validating) throws ParseException { // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(validating); // Create the builder and parse the file try { return factory.newDocumentBuilder().parse(source); } catch (IOException e) { ParseException pe = new ParseException("IO exception while parsing XML", -1); pe.initCause(e); throw pe; } catch (ParserConfigurationException e) { ParseException pe = new ParseException("XML could not be parsed", -1); pe.initCause(e); throw pe; } catch (SAXException e) { ParseException pe = new ParseException("XML contains invalid XML", -1); pe.initCause(e); throw pe; } }
From source file:org.jboss.ejb3.entity.PersistenceXmlLoader.java
private static Document loadURL(URL configURL, EntityResolver resolver) throws Exception { InputStream is = configURL != null ? configURL.openStream() : null; if (is == null) { throw new IOException("Failed to obtain InputStream from url: " + configURL); }/*from w w w . j av a 2 s . c o m*/ List errors = new ArrayList(); DocumentBuilderFactory docBuilderFactory = null; docBuilderFactory = DocumentBuilderFactory.newInstance(); docBuilderFactory.setValidating(true); docBuilderFactory.setNamespaceAware(true); try { //otherwise Xerces fails in validation docBuilderFactory.setAttribute("http://apache.org/xml/features/validation/schema", true); } catch (IllegalArgumentException e) { docBuilderFactory.setValidating(false); docBuilderFactory.setNamespaceAware(false); } InputSource source = new InputSource(is); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); docBuilder.setEntityResolver(resolver); docBuilder.setErrorHandler(new PersistenceXmlLoader.ErrorLogger("XML InputStream", errors, resolver)); Document doc = docBuilder.parse(source); if (errors.size() != 0) { throw new PersistenceException("invalid persistence.xml", (Throwable) errors.get(0)); } return doc; }
From source file:org.jboss.loom.utils.as7.AS7ModuleUtils.java
private static Document createDoc(String namespace, String rootElmName) throws ParserConfigurationException { DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance(); // Need specifically Xerces as it treats namespaces better way. /*DocumentBuilderFactory domFactory; try {// w ww. j a v a 2 s . c o m domFactory = (DocumentBuilderFactory) Class.forName("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl").newInstance(); } catch( ClassNotFoundException | InstantiationException | IllegalAccessException ex ) { throw new IllegalStateException("JDK's DocumentBuilderFactoryImpl not found:\n " + ex.getMessage(), ex ); }*/ //DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance("com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl", ClassLoader.getSystemClassLoader()); domFactory.setIgnoringComments(true); domFactory.setNamespaceAware(false); domFactory.setValidating(false); DocumentBuilder builder = domFactory.newDocumentBuilder(); Document doc = builder.getDOMImplementation().createDocument(namespace, rootElmName, null);// rootElmName return doc; }
From source file:org.jbpm.designer.filter.InjectionConfig.java
public synchronized void load(String contextPath) { DocumentBuilder parser;/* ww w .jav a 2 s . c om*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(false); try { parser = factory.newDocumentBuilder(); } catch (ParserConfigurationException e) { // TODO log return; } InputStream is = null; try { is = this.getClass().getResourceAsStream(DEFAULT_INJECTION_CONF_FILE); if (is == null) { System.out.println("unable to find designer injection conf file " + DEFAULT_INJECTION_CONF_FILE); } else { Document doc = parser.parse(is); NodeList rulesConf = doc.getElementsByTagName("rule"); rules = new InjectionRules(); for (int i = 0; i < rulesConf.getLength(); i++) { Node ruleNode = rulesConf.item(i); InjectionRule rule = new InjectionRule(ruleNode, contextPath); if (rule.isEnabled()) { rules.add(rule); } } } } catch (Exception e) { // TODO Log e.printStackTrace(); } finally { IOUtils.closeQuietly(is); } }
From source file:org.jmingo.parser.xml.dom.DocumentBuilderFactoryCreator.java
/** * Creates DocumentBuilderFactory./*from w ww. ja va 2 s . c om*/ * * @param parserConfiguration {@link ParserConfiguration} * @return DocumentBuilderFactory a factory API that enables applications to obtain a * parser that produces DOM object trees from XML documents * @throws ParserConfigurationException {@link ParserConfigurationException} */ public static DocumentBuilderFactory createDocumentBuilderFactory(ParserConfiguration parserConfiguration) throws ParserConfigurationException { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setValidating(parserConfiguration.isValidate()); documentBuilderFactory.setNamespaceAware(parserConfiguration.isNamespaceAware()); documentBuilderFactory.setFeature(ParserConstants.DYNAMIC_VALIDATION, true); List<Source> sourceList = createSchemaSources(parserConfiguration.getXsdSchemaPaths()); if (CollectionUtils.isNotEmpty(sourceList)) { documentBuilderFactory.setSchema(createSchema(sourceList)); } return documentBuilderFactory; }