⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 abstractimportxmltest.java

📁 jsr170接口的java实现。是个apache的开源项目。
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
     * Workspace.getImportContentHandler or Session.getImportContentHandler.     * This handler is then passed to a XML parser which parses the given     * document.     *     * @param absPath       the absPath to the parent node where to import the     *                      document     * @param document      the document to import     * @param uuidBehaviour how the uuid collisions should be handled     * @param withWorkspace if workspace or session interface should be used     * @throws RepositoryException     * @throws SAXException     * @throws IOException     */    public void importWithHandler(String absPath, Document document,                                  int uuidBehaviour, boolean withWorkspace)            throws Exception {        serialize(document);        BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file));        ContentHandler handler;        if (withWorkspace) {            handler = workspace.getImportContentHandler(absPath, uuidBehaviour);        } else {            handler = session.getImportContentHandler(absPath, uuidBehaviour);        }        XMLReader reader = XMLReaderFactory.createXMLReader();        reader.setFeature("http://xml.org/sax/features/namespaces", true);        reader.setFeature("http://xml.org/sax/features/namespace-prefixes", false);        reader.setContentHandler(handler);        reader.parse(new InputSource(bin));        if (!withWorkspace) {            session.save();        }    }//--------------------------------< helpers >-----------------------------------    /**     * Tests if jcr:uuid property of mix:referenceable nodetype is respected.     * This is believed as true when during import with uuidBehaviour     * IMPORT_UUID_COLLISION_REMOVE_EXISTING a node with the same uuid as a node     * to be imported will be deleted.     *     * @return     * @throws RepositoryException     * @throws IOException     */    public boolean isMixRefRespected() throws RepositoryException, IOException {        boolean respected = false;        if (supportsNodeType(mixReferenceable)) {            String uuid;            try {                uuid = createReferenceableNode(referenced);            } catch (NotExecutableException e) {                return false;            }            Document document = dom.newDocument();            Element root = document.createElement(rootElem);            root.setAttribute(XML_NS + ":jcr", NS_JCR_URI);            root.setAttributeNS(NS_JCR_URI, jcrUUID, uuid);            root.setAttributeNS(NS_JCR_URI, jcrMixinTypes, mixReferenceable);            document.appendChild(root);            importXML(refTargetNode.getPath(), document,                    ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING, SESSION);            session.save();            // existing node with same uuid should now be deleted            respected = !testRootNode.hasNode(referenced);            // check if imported document node is referenceable            Node rootNode = refTargetNode.getNode(rootElem);            respected &= rootNode.isNodeType(mixReferenceable);        }        return respected;    }    /**     * Creates a node with given name below the testRootNode which will be     * referenced by the node nodeName2 and returns the UUID assigned to the     * created node.     *     * @param name     * @return     * @throws RepositoryException     * @throws NotExecutableException if the created node is not referenceable     * and cannot be made referenceable by adding mix:referenceable.     */    public String createReferenceableNode(String name) throws RepositoryException, NotExecutableException {        // remove a yet existing node at the target        try {            Node node = testRootNode.getNode(name);            node.remove();            session.save();        } catch (PathNotFoundException pnfe) {            // ok        }        // a referenceable node        Node n1 = testRootNode.addNode(name, testNodeType);        if (!n1.isNodeType(mixReferenceable) && !n1.canAddMixin(mixReferenceable)) {            n1.remove();            session.save();            throw new NotExecutableException("node type " + testNodeType +                    " does not support mix:referenceable");        }        n1.addMixin(mixReferenceable);        // make sure jcr:uuid is available        testRootNode.save();        return n1.getUUID();    }    /**     * Creates a document with a element rootElem containing a jcr:uuid     * attribute with the given uuid as value. This document is imported below     * the node with path absPath. If nod node at absPth it is created.     * If there is yet a node rootElem below the then this node is     * romoved in advance.     *     * @param uuid     * @param uuidBehaviour     * @param withWorkspace     * @param withHandler     * @throws RepositoryException     * @throws IOException     */    public void importRefNodeDocument(            String absPath, String uuid, int uuidBehaviour,            boolean withWorkspace, boolean withHandler)            throws Exception {        Document document = dom.newDocument();        Element root = document.createElement(rootElem);        root.setAttribute(XML_NS + ":jcr", NS_JCR_URI);        root.setAttributeNS(NS_JCR_URI, jcrUUID, uuid);        root.setAttributeNS(NS_JCR_URI, jcrMixinTypes, mixReferenceable);        root.setAttribute(propertyName1, "some text");        document.appendChild(root);        Node targetNode = null;        try {            targetNode = (Node) session.getItem(absPath);            // remove a yet existing node at the target            try {                Node node = targetNode.getNode(rootElem);                node.remove();                session.save();            } catch (PathNotFoundException pnfe) {                // ok            }        } catch (PathNotFoundException pnfe) {            // create the node            targetNode = createAncestors(absPath);        }        if (withHandler) {            importWithHandler(targetNode.getPath(), document, uuidBehaviour, withWorkspace);        } else {            importXML(targetNode.getPath(), document, uuidBehaviour, withWorkspace);        }        session.save();    }    protected Node createAncestors(String absPath) throws RepositoryException {        // create nodes to name of absPath        Node root = session.getRootNode();        StringTokenizer names = new StringTokenizer(absPath, "/");        Node currentNode = root;        while (names.hasMoreTokens()) {            String name = names.nextToken();            if (currentNode.hasNode(name)) {                currentNode = currentNode.getNode(name);            } else {                currentNode = currentNode.addNode(name);            }        }        root.save();        return currentNode;    }    public void serialize(Document document) throws IOException {        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));        try {            // disable pretty printing/default line wrapping!            Transformer t = TransformerFactory.newInstance().newTransformer();            t.setOutputProperty(OutputKeys.METHOD, "xml");            t.setOutputProperty(OutputKeys.ENCODING, "UTF-8");            t.setOutputProperty(OutputKeys.INDENT, "no");            Source s = new DOMSource(document);            Result r = new StreamResult(bos);            t.transform(s, r);        } catch (TransformerException te) {            throw (IOException) new IOException(te.getMessage()).initCause(te);        } finally {            bos.close();        }    }    public boolean supportsNodeType(String ntName) throws RepositoryException {        boolean support = false;        try {            ntManager.getNodeType(ntName);            support = true;        } catch (NoSuchNodeTypeException nste) {            //        }        return support;    }    /**     * Returns a namespace prefix that currently not used in the namespace     * registry.     *     * @return an unused namespace prefix.     */    protected String getUnusedPrefix() throws RepositoryException {        Set prefixes = new HashSet(Arrays.asList(nsp.getPrefixes()));        String prefix = TEST_PREFIX;        int i = 0;        while (prefixes.contains(prefix)) {            prefix = TEST_PREFIX + i++;        }        return prefix;    }    /**     * Returns a namespace URI that currently not used in the namespace     * registry.     *     * @return an unused namespace URI.     */    protected String getUnusedURI() throws RepositoryException {        Set uris = new HashSet(Arrays.asList(nsp.getURIs()));        String uri = TEST_URI;        int i = 0;        while (uris.contains(uri)) {            uri = TEST_URI + i++;        }        return uri;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -