protected org.dom4j.io.SAXReader getSAXReader(boolean validate) { org.dom4j.io.SAXReader reader = null; if (!PropsValues.XML_VALIDATION_ENABLED) { validate = false; } try { reader = new org.dom4j.io.SAXReader(new SAXParser(), validate); reader.setEntityResolver(new EntityResolver()); reader.setFeature(_FEATURES_DYNAMIC, validate); reader.setFeature(_FEATURES_VALIDATION, validate); reader.setFeature(_FEATURES_VALIDATION_SCHEMA, validate); reader.setFeature(_FEATURES_VALIDATION_SCHEMA_FULL_CHECKING, validate); if (!validate) { reader.setFeature(_FEATURES_LOAD_DTD_GRAMMAR, validate); reader.setFeature(_FEATURES_LOAD_EXTERNAL_DTD, validate); } } catch (Exception e) { if (_log.isWarnEnabled()) { _log.warn("XSD validation is disabled because " + e.getMessage()); } reader = new org.dom4j.io.SAXReader(false); reader.setEntityResolver(new EntityResolver()); } return reader; }
@Override public Document getXmlContent() { // get the data to populate the widgets IPentahoResultSet resultSet = null; if (solution != null) { resultSet = getActionData(); } else { // TODO support other methods of getting data } // create the widget to use // load the XML document that defines the dial Document dialDefinition = null; try { org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader(); reader.setEntityResolver(new SolutionURIResolver()); dialDefinition = reader.read( ActionSequenceResource.getInputStream(definitionPath, LocaleHelper.getLocale())); } catch (Throwable t) { // XML document can't be read. We'll just return a null document. } // create a dial definition from the XML definition WidgetDefinition widgetDefinition = new DialWidgetDefinition(dialDefinition, 0, widgetWidth, widgetHeight, getSession()); return createDials(resultSet, widgetDefinition); }
@Test public void html() throws Exception { String subject = "html"; buildSMTPAppender(subject, SYNCHRONOUS); smtpAppender.setAsynchronousSending(false); smtpAppender.setLayout(buildHTMLLayout()); smtpAppender.start(); logger.addAppender(smtpAppender); logger.debug("html"); logger.error("en error", new Exception("an exception")); MimeMultipart mp = verifyAndExtractMimeMultipart(subject); // verifyAndExtractMimeMultipart strict adherence to xhtml1-strict.dtd SAXReader reader = new SAXReader(); reader.setValidation(true); reader.setEntityResolver(new XHTMLEntityResolver()); byte[] messageBytes = getAsByteArray(mp.getBodyPart(0).getInputStream()); ByteArrayInputStream bais = new ByteArrayInputStream(messageBytes); try { reader.read(bais); } catch (DocumentException de) { System.out.println("incoming message:"); System.out.println(new String(messageBytes)); throw de; } }
private String _buildPramatiXMLWebModule(String path) throws DocumentException, IOException { String contextRoot = path.substring(2, path.length() - 4); String filePath = path + "/docroot/WEB-INF/web.xml"; if (path.endsWith("-complete")) { contextRoot = "/"; filePath = path.substring(0, path.length() - 9) + "/docroot/WEB-INF/web.xml"; } FastStringBuffer sb = new FastStringBuffer(); sb.append("\t<web-module>\n"); sb.append("\t\t<name>").append(contextRoot).append("</name>\n"); sb.append("\t\t<module-name>") .append(path.substring(3, path.length())) .append(".war</module-name>\n"); SAXReader reader = new SAXReader(); reader.setEntityResolver(new EntityResolver()); Document doc = reader.read(new File(filePath)); Iterator itr = doc.getRootElement().elements("ejb-local-ref").iterator(); while (itr.hasNext()) { Element ejbLocalRef = (Element) itr.next(); sb.append("\t\t<ejb-local-ref>\n"); sb.append("\t\t\t<ejb-ref-name>") .append(ejbLocalRef.elementText("ejb-ref-name")) .append("</ejb-ref-name>\n"); sb.append("\t\t\t<ejb-link>") .append(ejbLocalRef.elementText("ejb-link")) .append("</ejb-link>\n"); sb.append("\t\t</ejb-local-ref>\n"); } itr = doc.getRootElement().elements("resource-ref").iterator(); while (itr.hasNext()) { Element resourceRef = (Element) itr.next(); sb.append("\t\t<resource-mapping>\n"); sb.append("\t\t\t<resource-name>") .append(resourceRef.elementText("res-ref-name")) .append("</resource-name>\n"); sb.append("\t\t\t<resource-type>") .append(resourceRef.elementText("res-type")) .append("</resource-type>\n"); sb.append("\t\t\t<resource-link>") .append(resourceRef.elementText("res-ref-name")) .append("</resource-link>\n"); sb.append("\t\t</resource-mapping>\n"); } sb.append("\t</web-module>\n"); return sb.toString(); }
public boolean init() { if (filterPanel == null) { // load the XML document that defines the dial Document filterDocument = null; try { org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader(); reader.setEntityResolver(new SolutionURIResolver()); filterDocument = reader.read( ActionSequenceResource.getInputStream(definitionPath, LocaleHelper.getLocale())); } catch (Throwable t) { FilterPanelComponent.logger.error( Messages.getInstance().getString("FilterPanelComponent.ERROR_0002_CREATE_XML"), t); //$NON-NLS-1$ return false; } try { filterPanel = getFilterPanel(filterDocument); } catch (FilterPanelException e) { // TODO sbarkdull localize FilterPanelComponent.logger.error( Messages.getInstance().getString("FilterPanelComponent.ERROR_0003_CREATE"), e); //$NON-NLS-1$ return false; } } return true; }
public ValidatorSaxHandler(File filePath) throws DocumentException { super(); SAXReader reader = new SAXReader(); reader.setValidation(false); reader.setIncludeInternalDTDDeclarations(false); reader.setIncludeExternalDTDDeclarations(false); reader.setEntityResolver(new NoDtdEntityResolver()); document = reader.read(filePath); }
/** * 处理XML头部采用DTD验证, 以下设置忽略验证,否则无法取到节点信息 * * @param reader document解析器 */ private static void setReaderValidation(SAXReader reader) { // 处理XML头部采用DTD验证, 以下设置忽略验证,否则无法取到节点信息 reader.setValidation(false); reader.setEntityResolver( new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new StringReader("")); } }); }
@Test public void html() throws Exception { buildSMTPAppender(smtpServer.getSmtp().getPort(), SYNCHRONOUS); smtpAppender.setAsynchronousSending(false); smtpAppender.setLayout(buildHTMLLayout(lc)); smtpAppender.start(); logger.addAppender(smtpAppender); logger.debug("hello"); logger.error("an error", new Exception("an exception")); MimeMultipart mp = verify(smtpServer, TEST_SUBJECT); // verify strict adherence to xhtml1-strict.dtd SAXReader reader = new SAXReader(); reader.setValidation(true); reader.setEntityResolver(new XHTMLEntityResolver()); reader.read(mp.getBodyPart(0).getInputStream()); }
public boolean setDataAction(final String widgetGridDataDefinition) { try { Document dataActionDocument = null; try { org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader(); reader.setEntityResolver(new SolutionURIResolver()); dataActionDocument = reader.read( ActionSequenceResource.getInputStream( widgetGridDataDefinition, LocaleHelper.getLocale())); } catch (Throwable t) { return false; } Node dataNode = dataActionDocument.selectSingleNode("widgetgrid/data"); // $NON-NLS-1$ solution = XmlDom4JHelper.getNodeText("data-solution", dataNode); // $NON-NLS-1$ actionPath = XmlDom4JHelper.getNodeText("data-path", dataNode); // $NON-NLS-1$ actionName = XmlDom4JHelper.getNodeText("data-action", dataNode); // $NON-NLS-1$ actionOutput = XmlDom4JHelper.getNodeText("data-output", dataNode); // $NON-NLS-1$ valueItem = XmlDom4JHelper.getNodeText("data-value", dataNode); // $NON-NLS-1$ nameItem = XmlDom4JHelper.getNodeText("data-name", dataNode); // $NON-NLS-1$ widgetWidth = (int) XmlDom4JHelper.getNodeText( "widgetgrid/width", dataActionDocument, 125); // $NON-NLS-1$ widgetHeight = (int) XmlDom4JHelper.getNodeText( "widgetgrid/height", dataActionDocument, 125); // $NON-NLS-1$ columns = (int) XmlDom4JHelper.getNodeText( "widgetgrid/columns", dataActionDocument, 2); // $NON-NLS-1$ style = XmlDom4JHelper.getNodeText("widgetgrid/style", dataActionDocument); // $NON-NLS-1$ } catch (Exception e) { error( Messages.getInstance() .getErrorString( "WidgetGrid.ERROR_0003_DEFINITION_NOT_VALID", widgetGridDataDefinition), e); //$NON-NLS-1$ return false; } return true; }
/** * Loads the configuration from the specified url location. A DOM configuration will be triggered * if the url ends with ".xml". * * @param url the url of the configuration source. * @throws Exception if the operation is unsuccessful. * @see hk.hku.cecid.piazza.commons.module.PersistentComponent#loading(java.net.URL) */ protected void loading(URL url) throws Exception { if (url.getPath().toLowerCase().endsWith(".xml")) { Properties params = getParameters(); if (params != null) { String checkConfig = params.getProperty("checkConfig"); if (checkConfig != null) { if (checkConfig.equals("true")) { SAXReader xmlReader = new SAXReader(); xmlReader.setEntityResolver( new EntityResolver() { public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException { return new InputSource(new ByteArrayInputStream(new byte[0])); } }); org.dom4j.Document doc = xmlReader.read(url); Node node = doc.selectSingleNode("/*[local-name()='configuration']/category/priority"); String priority = node.valueOf("@value"); if (!chechPriorityString(priority)) { throw new UtilitiesException( "Log4j does not support the value for priority - " + priority); } // Node node2 = doc.selectSingleNode("/*[local-name()='configuration']/root/priority"); String priority2 = node.valueOf("@value"); if (!chechPriorityString(priority2)) { throw new UtilitiesException( "Log4j does not support the value for priority - " + priority2); } } } } DOMConfigurator.configure(url); } else { PropertyConfigurator.configure(url); } }
/** * Create a dial image. * * <ul> * <li>Load the specified XML document describing the dial definition * <li>Create a dial definition object from the XML definition * <li>Use the JFreeChartEngine to create a dial image * <li>Create an XML document describing the dial * <li>Return the XML document * </ul> * * @return The XML document describing this dial */ @Override public Document getXmlContent() { WidgetDefinition widget = null; if (type == DashboardWidgetComponent.TYPE_DIAL) { // load the XML document that defines the dial IActionSequenceResource resource = new ActionSequenceResource( title, IActionSequenceResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ definitionPath); Document dialDefinition = null; try { org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader(); reader.setEntityResolver(new SolutionURIResolver()); dialDefinition = reader.read( resource.getInputStream(RepositoryFilePermission.READ, LocaleHelper.getLocale())); } catch (Throwable t) { // XML document can't be read. We'll just return a null document. } if (dialDefinition == null) { error( Messages.getInstance() .getErrorString( "Widget.ERROR_0002_INVALID_RESOURCE", definitionPath)); // $NON-NLS-1$ return null; } // create a dial definition from the XML definition widget = new DialWidgetDefinition(dialDefinition, 0, width, height, getSession()); if (widget != null) { // set the value to be displayed on the dial widget.setValue(new Double(value)); } } /* * else if( type == TYPE_THERMOMETER ) { // load the XML document that * defines the thermometer * * ActionResource resource = new ActionResource( title, * IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ * PentahoSystem.getApplicationContext().getSolutionPath( definitionPath ) ); * //$NON-NLS-1$ Document thermometerDefinition = null; try { * thermometerDefinition = PentahoSystem.getResourceAsDocument( resource ); } * catch (IOException e) {} // create a dial definition from the XML * definition widget = createThermometer( thermometerDefinition ); * * if( widget != null ) { // set the value to be displayed on the dial * widget.setValue( new Double(value) ); // Set the XSL file to be used * to generate the HTML setXsl( "text/html", "DialWidget.xsl" ); * //$NON-NLS-1$ //$NON-NLS-2$ } else { error( * Messages.getInstance().getString("Widget.ERROR_0001_COULD_NOT_CREATE") ); * //$NON-NLS-1$ return null; } } */ if (widget == null) { error(Messages.getInstance().getString("Widget.ERROR_0001_COULD_NOT_CREATE")); // $NON-NLS-1$ return null; } // create an image for the dial using the JFreeChart engine StringWriter stringWriter = new StringWriter(); PrintWriter printWriter = new PrintWriter(stringWriter); // create temporary file names String solutionDir = "system/tmp/"; // $NON-NLS-1$ String fileNamePrefix = "tmp_pie_"; // $NON-NLS-1$ String extension = ".png"; // $NON-NLS-1$ String fileName = null; String filePathWithoutExtension = null; try { File file = PentahoSystem.getApplicationContext() .createTempFile(getSession(), fileNamePrefix, extension, true); fileName = file.getName(); filePathWithoutExtension = solutionDir + fileName.substring(0, fileName.indexOf('.')); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } String dialTitle = ""; // $NON-NLS-1$ JFreeChartEngine.saveChart( widget, dialTitle, units, filePathWithoutExtension, width, height, JFreeChartEngine.OUTPUT_PNG, printWriter, this); // Create a document that describes the result Document result = DocumentHelper.createDocument(); IPentahoRequestContext requestContext = PentahoRequestContextHolder.getRequestContext(); setXslProperty( "baseUrl", requestContext.getContextPath()); // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ setXslProperty( "fullyQualifiedServerUrl", PentahoSystem.getApplicationContext() .getFullyQualifiedServerURL()); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ Element root = result.addElement("widget"); // $NON-NLS-1$ root.addElement("title").setText(title); // $NON-NLS-1$ root.addElement("units").setText(units); // $NON-NLS-1$ root.addElement("width").setText(Integer.toString(width)); // $NON-NLS-1$ root.addElement("height").setText(Integer.toString(height)); // $NON-NLS-1$ Element valueNode = root.addElement("value"); // $NON-NLS-1$ valueNode.setText(Double.toString(value)); valueNode.addAttribute( "in-image", Boolean.toString(widget.getValueFont() != null)); // $NON-NLS-1$ root.addElement("image").setText(fileName); // $NON-NLS-1$ return result; }
public static int saveAnalysis( final IPentahoSession session, final HashMap props, final String path, String fileName, final boolean overwrite) { if ("true" .equals( PentahoSystem.getSystemSetting( "kiosk-mode", "false"))) { // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ throw new RuntimeException( Messages.getInstance() .getErrorString("ANALYSISSAVER.ERROR_0006_SAVE_IS_DISABLED")); // $NON-NLS-1$ } int result = 0; try { AnalysisSaver.logger = LogFactory.getLog(AnalysisSaver.class); String baseUrl = PentahoSystem.getApplicationContext().getSolutionPath(""); // $NON-NLS-1$ ISolutionRepository solutionRepository = PentahoSystem.get(ISolutionRepository.class, session); // We will (at this point in time) always have an original action sequence to start from... String originalActionReference = (String) props.get("actionreference"); // $NON-NLS-1$ if (originalActionReference == null) { throw new MissingParameterException( Messages.getInstance() .getErrorString( "ANALYSISSAVER.ERROR_0001_MISSING_ACTION_REFERENCE")); //$NON-NLS-1$ } Document document = null; try { org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader(); reader.setEntityResolver(new SolutionURIResolver()); document = reader.read( ActionSequenceResource.getInputStream( originalActionReference, LocaleHelper.getLocale())); } catch (Throwable t) { // XML document can't be read. We'll just return a null document. } // Update the document with the stuff we passed in on the props document = AnalysisSaver.updateDocument(document, props); fileName = fileName.endsWith(AnalysisSaver.SUFFIX) ? fileName : fileName + AnalysisSaver.SUFFIX; result = solutionRepository.publish( baseUrl, path, fileName, document.asXML().getBytes(document.getXMLEncoding()), overwrite); // Now save the resource files ActionInfo actionInfo = ActionInfo.parseActionString(originalActionReference); String originalPath = actionInfo.getSolutionName() + "/" + actionInfo.getPath(); // $NON-NLS-1$ String originalFileName = actionInfo.getActionName(); originalFileName = originalFileName.substring(0, originalFileName.lastIndexOf(AnalysisSaver.SUFFIX)); ISolutionFile[] parentFiles = solutionRepository .getSolutionFile(originalPath, ISolutionRepository.ACTION_EXECUTE) .listFiles(); String baseFileName = fileName.substring(0, fileName.lastIndexOf(AnalysisSaver.SUFFIX)); for (ISolutionFile aSolutionFile : parentFiles) { if (!aSolutionFile.isDirectory() && aSolutionFile.getFileName().startsWith(originalFileName) && aSolutionFile .getFileName() .toLowerCase() .endsWith(AnalysisSaver.PROPERTIES_SUFFIX)) { String newFileName = aSolutionFile.getFileName().replaceFirst(originalFileName, baseFileName); result = result & solutionRepository.publish( baseUrl, path, newFileName, aSolutionFile.getData(), overwrite); } } solutionRepository.resetRepository(); } catch (Exception e) { AnalysisSaver.logger.error( Messages.getInstance().getErrorString("ANALYSISSAVER.ERROR_0000_UNKNOWN"), e); //$NON-NLS-1$ result = ISolutionRepository.FILE_ADD_FAILED; } return result; }
@Before public void initReader() throws Exception { reader = new SAXReader(true); reader.setEntityResolver(resolver); reader.setProperty(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE); }