List of usage examples for javax.xml.parsers DocumentBuilderFactory setValidating
public void setValidating(boolean validating)
From source file:org.chiba.tools.schemabuilder.AbstractSchemaFormBuilder.java
/** * builds a form from a XML schema./*www . j a va 2 s .co m*/ * * @param inputURI the URI of the Schema to be used * @return __UNDOCUMENTED__ * @throws FormBuilderException __UNDOCUMENTED__ */ public Document buildForm(String inputURI) throws FormBuilderException { try { this.loadSchema(inputURI); buildTypeTree(schema); //refCounter = 0; counter = new HashMap(); Document xForm = createFormTemplate(_rootTagName, _rootTagName + " Form", getProperty(CSS_STYLE_PROP, DEFAULT_CSS_STYLE_PROP)); //this.buildInheritenceTree(schema); Element envelopeElement = xForm.getDocumentElement(); //Element formSection = (Element) envelopeElement.getElementsByTagNameNS(CHIBA_NS, "form").item(0); //Element formSection =(Element) envelopeElement.getElementsByTagName("body").item(0); //find form element: last element created NodeList children = xForm.getDocumentElement().getChildNodes(); int nb = children.getLength(); Element formSection = (Element) children.item(nb - 1); Element modelSection = (Element) envelopeElement.getElementsByTagNameNS(XFORMS_NS, "model").item(0); //add XMLSchema if we use schema types if (_useSchemaTypes && modelSection != null) { modelSection.setAttributeNS(XFORMS_NS, this.getXFormsNSPrefix() + "schema", inputURI); } //change stylesheet String stylesheet = this.getStylesheet(); if ((stylesheet != null) && !stylesheet.equals("")) { envelopeElement.setAttributeNS(CHIBA_NS, this.getChibaNSPrefix() + "stylesheet", stylesheet); } // TODO: Commented out because comments aren't output properly by the Transformer. //String comment = "This XForm was automatically generated by " + this.getClass().getName() + " on " + (new Date()) + System.getProperty("line.separator") + " from the '" + rootElementName + "' element from the '" + schema.getSchemaTargetNS() + "' XML Schema."; //xForm.insertBefore(xForm.createComment(comment),envelopeElement); //xxx XSDNode node = findXSDNodeByName(rootElementTagName,schemaNode.getElementSet()); //check if target namespace //no way to do this with XS API ? load DOM document ? //TODO: find a better way to find the targetNamespace try { Document domDoc = DOMUtil.parseXmlFile(inputURI, true, false); if (domDoc != null) { Element root = domDoc.getDocumentElement(); targetNamespace = root.getAttribute("targetNamespace"); if (targetNamespace != null && targetNamespace.equals("")) targetNamespace = null; } } catch (Exception ex) { LOGGER.error("Schema not loaded as DOM document: " + ex.getMessage()); } //if target namespace & we use the schema types: add it to form ns declarations if (_useSchemaTypes && targetNamespace != null && !targetNamespace.equals("")) { envelopeElement.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns:schema", targetNamespace); } //TODO: WARNING: in Xerces 2.6.1, parameters are switched !!! (name, namespace) //XSElementDeclaration rootElementDecl =schema.getElementDeclaration(targetNamespace, _rootTagName); XSElementDeclaration rootElementDecl = schema.getElementDeclaration(_rootTagName, targetNamespace); if (rootElementDecl == null) { //DEBUG rootElementDecl = schema.getElementDeclaration(_rootTagName, targetNamespace); if (rootElementDecl != null && LOGGER.isDebugEnabled()) LOGGER.debug("getElementDeclaration: inversed parameters OK !!!"); throw new FormBuilderException("Invalid root element tag name [" + _rootTagName + ", targetNamespace=" + targetNamespace + "]"); } Element instanceElement = (Element) modelSection .appendChild(xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "instance")); this.setXFormsId(instanceElement); Element rootElement; if (_instanceMode == AbstractSchemaFormBuilder.INSTANCE_MODE_NONE) { rootElement = (Element) instanceElement.appendChild( xForm.createElementNS(targetNamespace, getElementName(rootElementDecl, xForm))); String prefix = xmlSchemaInstancePrefix.substring(0, xmlSchemaInstancePrefix.length() - 1); rootElement.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns:" + prefix, XMLSCHEMA_INSTANCE_NAMESPACE_URI); } else if (_instanceMode == AbstractSchemaFormBuilder.INSTANCE_MODE_INCLUDED) //get the instance element { boolean ok = true; try { /*DOMResult result = new DOMResult(); TransformerFactory trFactory = TransformerFactory.newInstance(); Transformer tr = trFactory.newTransformer(); tr.transform(_instanceSource, result); Document instanceDoc = (Document) result.getNode();*/ DocumentBuilderFactory docFact = DocumentBuilderFactory.newInstance(); docFact.setNamespaceAware(true); docFact.setValidating(false); DocumentBuilder parser = docFact.newDocumentBuilder(); Document instanceDoc = parser.parse(new InputSource(_instanceSource.getSystemId())); //possibility abandonned for the moment: //modify the instance to add the correct "xsi:type" attributes wherever needed //Document instanceDoc=this.setXMLSchemaAndPSVILoad(inputURI, _instanceSource, targetNamespace); if (instanceDoc != null) { Element instanceInOtherDoc = instanceDoc.getDocumentElement(); if (instanceInOtherDoc.getNodeName().equals(_rootTagName)) { rootElement = (Element) xForm.importNode(instanceInOtherDoc, true); instanceElement.appendChild(rootElement); //add XMLSchema instance NS String prefix = xmlSchemaInstancePrefix.substring(0, xmlSchemaInstancePrefix.length() - 1); if (!rootElement.hasAttributeNS(XMLNS_NAMESPACE_URI, prefix)) rootElement.setAttributeNS(XMLNS_NAMESPACE_URI, "xmlns:" + prefix, XMLSCHEMA_INSTANCE_NAMESPACE_URI); //possibility abandonned for the moment: //modify the instance to add the correct "xsi:type" attributes wherever needed //this.addXSITypeAttributes(rootElement); } else { ok = false; } } else { ok = false; } } catch (Exception ex) { ex.printStackTrace(); //if there is an exception we put the empty root element ok = false; } //if there was a problem if (!ok) { rootElement = (Element) instanceElement.appendChild(xForm.createElement(_rootTagName)); } } else if (_instanceMode == AbstractSchemaFormBuilder.INSTANCE_MODE_HREF) //add the xlink:href attribute { instanceElement.setAttributeNS(SchemaFormBuilder.XLINK_NS, this.getXLinkNSPrefix() + "href", _instanceHref); } Element formContentWrapper = _wrapper.createGroupContentWrapper(formSection); addElement(xForm, modelSection, formContentWrapper, rootElementDecl, rootElementDecl.getTypeDefinition(), "/" + getElementName(rootElementDecl, xForm)); Element submitInfoElement = (Element) modelSection .appendChild(xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "submission")); //submitInfoElement.setAttributeNS(XFORMS_NS,getXFormsNSPrefix()+"id","save"); String submissionId = this.setXFormsId(submitInfoElement); //action if (_action == null) { submitInfoElement.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "action", ""); } else { submitInfoElement.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "action", _action); } //method if ((_submitMethod != null) && !_submitMethod.equals("")) { submitInfoElement.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "method", _submitMethod); } else { //default is "post" submitInfoElement.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "method", AbstractSchemaFormBuilder.SUBMIT_METHOD_POST); } //Element submitButton = (Element) formSection.appendChild(xForm.createElementNS(XFORMS_NS,getXFormsNSPrefix()+"submit")); Element submitButton = xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "submit"); Element submitControlWrapper = _wrapper.createControlsWrapper(submitButton); formContentWrapper.appendChild(submitControlWrapper); submitButton.setAttributeNS(XFORMS_NS, getXFormsNSPrefix() + "submission", submissionId); this.setXFormsId(submitButton); Element submitButtonCaption = (Element) submitButton .appendChild(xForm.createElementNS(XFORMS_NS, getXFormsNSPrefix() + "label")); submitButtonCaption.appendChild(xForm.createTextNode("Submit")); this.setXFormsId(submitButtonCaption); return xForm; } catch (ParserConfigurationException x) { throw new FormBuilderException(x); } catch (java.lang.ClassNotFoundException x) { throw new FormBuilderException(x); } catch (java.lang.InstantiationException x) { throw new FormBuilderException(x); } catch (java.lang.IllegalAccessException x) { throw new FormBuilderException(x); } }
From source file:org.chiba.xml.base.XMLBaseResolverTest.java
private Document getXmlResource(String fileName) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);// w w w.j av a 2 s. c om factory.setValidating(false); // Create builder. DocumentBuilder builder = factory.newDocumentBuilder(); // Parse files. return builder.parse(getClass().getResourceAsStream(fileName)); }
From source file:org.chiba.xml.xforms.BindingTest.java
protected void setUp() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true);//from w w w . j a v a 2s . co m factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); Document document = builder.parse(getClass().getResourceAsStream("BindingTest.xhtml")); this.chibaBean = new ChibaBean(); this.chibaBean.setXMLContainer(document); this.chibaBean.init(); }
From source file:org.chiba.xml.xforms.ChibaBean.java
private DocumentBuilder getDocumentBuilder() throws XFormsException { // ensure xerces dom try {/*w ww . java2 s.co m*/ DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); factory.setAttribute("http://apache.org/xml/properties/dom/document-class-name", "org.apache.xerces.dom.DocumentImpl"); return factory.newDocumentBuilder(); } catch (Exception e) { throw new XFormsException(e); } }
From source file:org.chiba.xml.xforms.ChibaBeanTest.java
/** * Sets up the test./*from ww w . ja v a 2 s .c om*/ * * @throws Exception in any error occurred during setup. */ protected void setUp() throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); DocumentBuilder builder = factory.newDocumentBuilder(); this.form = builder.parse(getClass().getResourceAsStream("ChibaBeanTest.xhtml")); String path = getClass().getResource("ChibaBeanTest.xhtml").getPath(); this.baseURI = "file://" + path.substring(0, path.lastIndexOf("ChibaBeanTest.xhtml")); this.processor = new ChibaBean(); }
From source file:org.chiba.xml.xforms.config.Config.java
/** * Creates a new configuration.//www . ja v a2 s .co m * * @param stream the input stream to be read. */ private Config(InputStream stream) throws XFormsConfigException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); Document document = factory.newDocumentBuilder().parse(stream); JXPathContext context = JXPathContext.newContext(document.getDocumentElement()); this.properties = load(context, "/properties/property", "name", "value"); this.stylesheets = load(context, "/stylesheets/stylesheet", "name", "value"); this.uriResolvers = load(context, "/connectors/uri-resolver", "scheme", "class"); this.submissionHandlers = load(context, "/connectors/submission-handler", "scheme", "class"); this.modelItemCalculators = load(context, "/connectors/modelitem-calculator", "scheme", "class"); this.modelItemValidators = load(context, "/connectors/modelitem-validator", "scheme", "class"); this.errorMessages = load(context, "/error-messages/message", "id", "value"); this.instanceSerializerMap = loadSerializer(context, "/register-serializer/instance-serializer", "scheme", "method", "mediatype", "class"); this.extensionFunctions = loadExtensionFunctions(context, "/extension-functions/function"); // this.actions = load (context,"/actions/action","name","class"); // this.generators = load(context, "/generators/generator", "name", "class"); connectorFactory = load(context, "/connectors", "factoryClass"); } catch (Exception e) { throw new XFormsConfigException(e); } }
From source file:org.chiba.xml.xforms.config.DefaultConfig.java
/** * Creates and loads a new configuration. * /*from w w w .j a v a2 s .c om*/ * @param stream * InputStream to read XML data in the default format. */ public DefaultConfig(InputStream stream) throws XFormsConfigException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(false); factory.setValidating(false); Document document = factory.newDocumentBuilder().parse(stream); JXPathContext context = JXPathContext.newContext(document.getDocumentElement()); this.properties = load(context, "/properties/property", "name", "value"); this.stylesheets = load(context, "/stylesheets/stylesheet", "name", "value"); this.uriResolvers = load(context, "/connectors/uri-resolver", "scheme", "class"); this.submissionHandlers = load(context, "/connectors/submission-handler", "scheme", "class"); this.errorMessages = load(context, "/error-messages/message", "id", "value"); //this.actions = load (context,"/actions/action", "name", "class"); //this.generators = load(context, "/generators/generator", "name", // "class"); this.extensionFunctions = loadExtensionFunctions(context, "/extension-functions/function"); this.customElements = loadCustomElements(context, "/custom-elements/element[not(@type) or @type='ui']"); this.customActionsElements = loadCustomElements(context, "/custom-elements/element[@type='action']"); this.connectorFactory = load(context, "/connectors", "factoryClass"); this.instanceSerializerMap = loadSerializer(context, "/register-serializer/instance-serializer", "scheme", "method", "mediatype", "class"); } catch (Exception e) { throw new XFormsConfigException(e); } }
From source file:org.chiba.xml.xforms.connector.file.FileURIResolver.java
/** * Performs link traversal of the <code>file</code> URI and returns the * result as a DOM document./*from www .j a va 2s .c o m*/ * * @return a DOM node parsed from the <code>file</code> URI. * @throws XFormsException if any error occurred during link traversal. */ public Object resolve() throws XFormsException { try { // create uri URI uri = new URI(getURI()); // use scheme specific part in order to handle UNC names String fileName = uri.getSchemeSpecificPart(); if (LOGGER.isDebugEnabled()) { LOGGER.debug("loading file '" + fileName + "'"); } // create file File file = new File(fileName); // check for directory if (file.isDirectory()) { return FileURIResolver.buildDirectoryListing(file); } // parse file DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); Document document = factory.newDocumentBuilder().parse(file); // check for fragment identifier if (uri.getFragment() != null) { return document.getElementById(uri.getFragment()); } return document; } catch (Exception e) { throw new XFormsException(e); } }
From source file:org.chiba.xml.xforms.connector.http.HTTPURIResolver.java
/** * Performs link traversal of the <code>http</code> URI and returns the result * as a DOM document.// ww w .ja v a 2 s .c om * * @return a DOM node parsed from the <code>http</code> URI. * @throws XFormsException if any error occurred during link traversal. */ public Object resolve() throws XFormsException { try { URI uri = new URI(getURI()); if (LOGGER.isDebugEnabled()) { LOGGER.debug("getting '" + uri + "'"); } get(getURIWithoutFragment()); if (LOGGER.isDebugEnabled()) { LOGGER.debug("converting response stream to XML"); } DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); Document document = factory.newDocumentBuilder().parse(getResponseBody()); if (uri.getFragment() != null) { return document.getElementById(uri.getFragment()); } return document; } catch (Exception e) { throw new XFormsException(e); } }
From source file:org.chiba.xml.xforms.core.Instance.java
/** * Returns a new created instance document. * <p/>//from w ww.j a va 2 s.c om * If this instance has an original instance, it will be imported into this * new document. Otherwise the new document is left empty. * * @return a new created instance document. */ private Document createInstanceDocument() throws XFormsException { try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); factory.setValidating(false); Document document = factory.newDocumentBuilder().newDocument(); if (this.initialInstance != null) { document.appendChild(document.importNode(this.initialInstance.cloneNode(true), true)); String srcAttribute = getXFormsAttribute(SRC_ATTRIBUTE); if (srcAttribute == null) { // apply namespaces NamespaceResolver.applyNamespaces(this.element, document.getDocumentElement()); } } return document; } catch (Exception e) { throw new XFormsException(e); } }