📄 abstractimportxmltest.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.test.api;import org.apache.jackrabbit.test.AbstractJCRTest;import org.apache.jackrabbit.test.NotExecutableException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.w3c.dom.Attr;import org.xml.sax.ContentHandler;import org.xml.sax.SAXException;import org.xml.sax.InputSource;import org.xml.sax.XMLReader;import org.xml.sax.helpers.XMLReaderFactory;import javax.jcr.nodetype.NodeTypeManager;import javax.jcr.nodetype.NoSuchNodeTypeException;import javax.jcr.Session;import javax.jcr.Workspace;import javax.jcr.NamespaceRegistry;import javax.jcr.Node;import javax.jcr.ImportUUIDBehavior;import javax.jcr.RepositoryException;import javax.jcr.PathNotFoundException;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.transform.OutputKeys;import javax.xml.transform.Result;import javax.xml.transform.Source;import javax.xml.transform.Transformer;import javax.xml.transform.TransformerException;import javax.xml.transform.TransformerFactory;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.StreamResult;import java.util.Set;import java.util.HashSet;import java.util.Arrays;import java.util.StringTokenizer;import java.io.File;import java.io.FileInputStream;import java.io.BufferedInputStream;import java.io.IOException;import java.io.BufferedOutputStream;import java.io.FileOutputStream;/** * <code>AbstractImportXmlTest</code> Provides names, data and methods to create * xml documents and referenceable nodes for the tests of document view import * methods of Workspace and Session. */abstract class AbstractImportXmlTest extends AbstractJCRTest { protected final boolean WORKSPACE = true; protected final boolean SESSION = false; protected boolean CONTENTHANDLER = true; protected boolean STREAM = false; // the absolute path to the target nodes for the imports protected String target; protected String refTarget; protected Session session; protected Workspace workspace; protected NodeTypeManager ntManager; protected NamespaceRegistry nsp; protected String ntUnstructured; protected File file; // some node names protected String referenced; protected String referencing; // the target nodes protected Node targetNode; protected Node refTargetNode; protected String unusedPrefix; protected String unusedURI; // names for namespace import protected final String TEST_PREFIX = "docview"; protected final String TEST_URI = "www.apache.org/jackrabbit/test/namespaceImportTest"; protected final String XML_NS = "xmlns"; // xml document related names protected static final String rootElem = "docRoot"; protected static final String refNodeElem = "refNodeElem"; protected static final String xmltextElem = "xmltextElem"; protected static final String childElem = "childElem"; protected static final String grandChildElem = "grandChildElem"; protected static final String encodedElemName = "Element_x003C__x003E_Name"; protected static final String decodedElemName = "Element<>Name"; protected static final String attributeName = "attribute"; protected static final String attributeValue = "attrVal"; protected static final String encodedAttributeName = "Prop_x0020_Name"; protected static final String decodedAttributeName = "Prop Name"; protected static final String encodedAttributeValue = "Hello_x0009_&_x0009_GoodBye"; protected static final String decodedAttributeValue = "Hello\\t&\\tGoodBye"; //String value for the test with leading and trailing spaces and entity reference charachters protected String xmltext = "\\t Text for docView Export test_x0009_with escaped _x003C_ characters. "; // is semantic of mix:referenceable respected? protected boolean respectMixRef = false; // default uuidBehaviour for the tests protected int uuidBehaviour = ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW; protected DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); protected DocumentBuilder dom; /** * Sets up the fixture for the test cases. */ public void setUp() throws Exception { super.setUp(); try { dom = factory.newDocumentBuilder(); file = File.createTempFile("docViewImportTest", ".xml"); log.print("Tempfile: " + file.getAbsolutePath()); session = superuser; workspace = session.getWorkspace(); // create the target nodes for the imports target = testRoot + "/target"; targetNode = createAncestors(target); refTarget = testRoot + "/refTarget"; refTargetNode = createAncestors(refTarget); nsp = workspace.getNamespaceRegistry(); ntManager = workspace.getNodeTypeManager(); // construct a namespace not existing in the repository unusedPrefix = getUnusedPrefix(); unusedURI = getUnusedURI(); referenced = nodeName1; referencing = nodeName2; // test if jcr:uuid of mix:referenceable node type is respected respectMixRef = isMixRefRespected(); } catch (Exception ex) { if (file != null) { file.delete(); file = null; } throw (ex); } } public void tearDown() throws Exception { if (file != null) { file.delete(); file = null; } super.tearDown(); } /** * Creates a document with some nodes and props for Namespace adding test * and for correct tree structure tests after having imported. * * @return */ public Document createSimpleDocument() { Document doc = dom.newDocument(); Element root = doc.createElementNS(unusedURI, unusedPrefix + ":" + rootElem); root.setAttribute(XML_NS + ":" + unusedPrefix, unusedURI); Element child = doc.createElement(childElem); Element xmlElem = doc.createElement(xmltextElem); Element encoded = doc.createElement(encodedElemName); Element grandchild = doc.createElement(grandChildElem); Attr attr = doc.createAttribute(attributeName); attr.setValue(attributeValue); Attr encodedAttr = doc.createAttribute(encodedAttributeName); encodedAttr.setValue(encodedAttributeValue); child.appendChild(encoded); child.setAttributeNode(encodedAttr); grandchild.setAttributeNode(attr); xmlElem.appendChild(doc.createTextNode(xmltext)); xmlElem.appendChild(grandchild); xmlElem.setAttribute(attributeName, attributeValue); root.appendChild(child); root.appendChild(xmlElem); doc.appendChild(root); return doc; } /** * Imports a given document using either Workspace.importXML or * Session.importXML method. * * @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 IOException */ protected void importXML(String absPath, Document document, int uuidBehaviour, boolean withWorkspace) throws RepositoryException, IOException { serialize(document); BufferedInputStream bin = new BufferedInputStream(new FileInputStream(file)); try { if (withWorkspace) { workspace.importXML(absPath, bin, uuidBehaviour); } else { session.importXML(absPath, bin, uuidBehaviour); session.save(); } } finally { bin.close(); } } /** * Imports a given document using the ContentHandler received either with
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -