private static InputStream getAsInputStreamFromClassLoader(String filename) { ClassLoader cl = Thread.currentThread().getContextClassLoader(); InputStream is = cl == null ? null : cl.getResourceAsStream(filename); if (is == null) { // check system class loader is = XmlConfigurator.class.getClassLoader().getResourceAsStream(filename); } return is; }
// return the resource as a stream private InputStream mapResource(String publicId) { if (publicId == null || id2resource == null) return null; String resourceName = (String) id2resource.get(publicId); ClassLoader loader = null; if (resourceName == null) return null; if (id2loader != null) loader = (ClassLoader) id2loader.get(publicId); if (loader == null) return ClassLoader.getSystemResourceAsStream(resourceName); return loader.getResourceAsStream(resourceName); }
/* STATIC ACCESSORY METHODS */ public static Class getClassOfBean(ClassLoader classLoader, String clazz) { Class cls = null; try { cls = classLoader.loadClass(clazz); } catch (ClassNotFoundException cnfe) { } return cls; }
/** * @return <code>Document</code> based on <code>documentURI</code>. * @throws Exception if an error occurs during the process of building a <code>Document</code> */ private Document getDocument() throws Exception { Document returnDoc; DocumentBuilder db = getNonValidatingBuilder(); URL documentURL = documentURI.toURL(); InputSource is = new InputSource(getInputStream(documentURL)); is.setSystemId(documentURI.toURL().toExternalForm()); Document doc = null; try { doc = db.parse(is); } catch (SAXParseException spe) { // [mojarra-1693] // Test if this is a zero length or whitespace only faces-config.xml file. // If so, just make an empty Document InputStream stream = is.getByteStream(); stream.close(); is = new InputSource(getInputStream(documentURL)); stream = is.getByteStream(); if (streamIsZeroLengthOrEmpty(stream) && documentURL.toExternalForm().endsWith("faces-config.xml")) { ClassLoader loader = this.getClass().getClassLoader(); is = new InputSource(getInputStream(loader.getResource(EMPTY_FACES_CONFIG))); doc = db.parse(is); } } String documentNS = doc.getDocumentElement().getNamespaceURI(); if (validating && documentNS != null) { DOMSource domSource = new DOMSource(doc, documentURL.toExternalForm()); /* * If the Document in question is 1.2 (i.e. it has a namespace matching * JAVAEE_SCHEMA_DEFAULT_NS, then perform validation using the cached schema * and return. Otherwise we assume a 1.0 or 1.1 faces-config in which case * we need to transform it to reference a special 1.1 schema before validating. */ Node documentElement = ((Document) domSource.getNode()).getDocumentElement(); if (JAVAEE_SCHEMA_DEFAULT_NS.equals(documentNS)) { Attr version = (Attr) documentElement.getAttributes().getNamedItem("version"); DbfFactory.FacesSchema schema; if (version != null) { String versionStr = version.getValue(); if ("2.0".equals(versionStr)) { if ("facelet-taglib".equals(documentElement.getLocalName())) { schema = DbfFactory.FacesSchema.FACELET_TAGLIB_20; } else { schema = DbfFactory.FacesSchema.FACES_20; } } else if ("2.1".equals(versionStr)) { if ("facelet-taglib".equals(documentElement.getLocalName())) { schema = DbfFactory.FacesSchema.FACELET_TAGLIB_20; } else { schema = DbfFactory.FacesSchema.FACES_21; } } else if ("2.2".equals(versionStr)) { if ("facelet-taglib".equals(documentElement.getLocalName())) { schema = DbfFactory.FacesSchema.FACELET_TAGLIB_22; } else { schema = DbfFactory.FacesSchema.FACES_21; } } else if ("1.2".equals(versionStr)) { schema = DbfFactory.FacesSchema.FACES_12; } else { throw new ConfigurationException("Unknown Schema version: " + versionStr); } DocumentBuilder builder = getBuilderForSchema(schema); if (builder.isValidating()) { builder.getSchema().newValidator().validate(domSource); returnDoc = ((Document) domSource.getNode()); } else { returnDoc = ((Document) domSource.getNode()); } } else { // this shouldn't happen, but... throw new ConfigurationException("No document version available."); } } else { DOMResult domResult = new DOMResult(); Transformer transformer = getTransformer(documentNS); transformer.transform(domSource, domResult); // copy the source document URI to the transformed result // so that processes that need to build URLs relative to the // document will work as expected. ((Document) domResult.getNode()) .setDocumentURI(((Document) domSource.getNode()).getDocumentURI()); DbfFactory.FacesSchema schemaToApply; if (FACES_CONFIG_1_X_DEFAULT_NS.equals(documentNS)) { schemaToApply = DbfFactory.FacesSchema.FACES_11; } else if (FACELETS_1_0_DEFAULT_NS.equals(documentNS)) { schemaToApply = DbfFactory.FacesSchema.FACELET_TAGLIB_20; } else { throw new IllegalStateException(); } DocumentBuilder builder = getBuilderForSchema(schemaToApply); if (builder.isValidating()) { builder.getSchema().newValidator().validate(new DOMSource(domResult.getNode())); returnDoc = (Document) domResult.getNode(); } else { returnDoc = (Document) domResult.getNode(); } } } else { returnDoc = doc; } // mark this document as the parsed representation of the // WEB-INF/faces-config.xml. This is used later in the configuration // processing. if (documentURL.toExternalForm().contains("/WEB-INF/faces-config.xml")) { Attr webInf = returnDoc.createAttribute(WEB_INF_MARKER); webInf.setValue("true"); returnDoc.getDocumentElement().getAttributes().setNamedItem(webInf); } return returnDoc; }