javax.jcr.Session.importXML()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(13.5k)|赞(0)|评价(0)|浏览(110)

本文整理了Java中javax.jcr.Session.importXML()方法的一些代码示例,展示了Session.importXML()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Session.importXML()方法的具体详情如下:
包路径:javax.jcr.Session
类名称:Session
方法名:importXML

Session.importXML介绍

[英]Deserializes an XML document and adds the resulting item subgraph as a child of the node at parentAbsPath.

If the incoming XML stream does not appear to be a JCR system view XML document then it is interpreted as a document view XML document.

The passed InputStream is closed before this method returns either normally or because of an exception.

The tree of new items is built in the transient storage of the Session. In order to persist the new content, save must be called. The advantage of this through-the-session method is that (depending on what constraint checks the implementation leaves until save) structures that violate node type constraints can be imported, fixed and then saved. The disadvantage is that a large import will result in a large cache of pending nodes in the session. See Workspace#importXML for a version of this method that does not go through the Session.

The flag uuidBehavior governs how the identifiers of incoming nodes are handled. There are four options:

  • ImportUUIDBehavior#IMPORT_UUID_CREATE_NEW: Incoming nodes are added in the same way that new node is added with Node.addNode. That is, they are either assigned newly created identifiers upon addition or upon save (depending on the implementation, see 4.9.1.1 When Identifiers are Assigned in the specification). In either case, identifier collisions will not occur.
  • ImportUUIDBehavior#IMPORT_UUID_COLLISION_REMOVE_EXISTING: If an incoming node has the same identifier as a node already existing in the workspace then the already existing node (and its subgraph) is removed from wherever it may be in the workspace before the incoming node is added. Note that this can result in nodes "disappearing" from locations in the workspace that are remote from the location to which the incoming subgraph is being written. Both the removal and the new addition will be dispatched on save.
  • ImportUUIDBehavior#IMPORT_UUID_COLLISION_REPLACE_EXISTING: If an incoming node has the same identifier as a node already existing in the workspace, then the already-existing node is replaced by the incoming node in the same position as the existing node. Note that this may result in the incoming subgraph being disaggregated and "spread around" to different locations in the workspace. In the most extreme case this behavior may result in no node at all being added as child of parentAbsPath. This will occur if the topmost element of the incoming XML has the same identifier as an existing node elsewhere in the workspace. The change will be dispatched on save.
  • ImportUUIDBehavior#IMPORT_UUID_COLLISION_THROW: If an incoming node has the same identifier as a node already existing in the workspace then an ItemExistsException is thrown.
    Unlike Workspace#importXML, this method does not necessarily enforce all node type constraints during deserialization. Those that would be immediately enforced in a normal write method (Node.addNode, Node.setProperty etc.) of this implementation cause an immediate ConstraintViolationException during deserialization. All other constraints are checked on save, just as they are in normal write operations. However, which node type constraints are enforced depends upon whether node type information in the imported data is respected, and this is an implementation-specific issue.

A ConstraintViolationException will also be thrown immediately if uuidBehavior is set to IMPORT_UUID_COLLISION_REMOVE_EXISTING and an incoming node has the same identifier as the node at parentAbsPath or one of its ancestors.

A PathNotFoundException is thrown either immediately, on dispatch or on persist, if no node exists at parentAbsPath. Implementations may differ on when this validation is performed

A ConstraintViolationException is thrown either immediately, on dispatch or on persist, if the new subgraph cannot be added to the node at parentAbsPath due to node-type or other implementation-specific constraints. Implementations may differ on when this validation is performed.

A VersionException is thrown either immediately, on dispatch or on persist, if the node at parentAbsPath is read-only due to a check-in. Implementations may differ on when this validation is performed.

A LockException is thrown either immediately, on dispatch or on persist, if a lock prevents the addition of the subgraph. Implementations may differ on when this validation is performed.
[中]反序列化XML文档,并将结果项子图添加为parentAbsPath处节点的子图。
如果传入的XML流看起来不是JCR系统视图XML文档,那么它将被解释为文档视图XML文档。
传递的InputStream在该方法正常返回或由于异常返回之前关闭。
新项目树构建在Session的临时存储中。为了保存新内容,必须调用save。通过session方法实现的优点是(取决于在save之前检查实现的约束)可以导入、修复并保存违反节点类型约束的结构。缺点是,大型导入将导致会话中挂起节点的大型缓存。请参阅Workspace#importXML以获取此方法的一个版本,该版本不经过Session
标志uuidBehavior控制如何处理传入节点的标识符。有四种选择:
*ImportUUIDBehavior#IMPORT_UUID_CREATE_NEW:传入节点的添加方式与新节点的添加方式相同[$7$]。也就是说,它们要么在添加时被分配新创建的标识符,要么在save时被分配(取决于实现,在规范中分配标识符时参见4.9.1.1)。在这两种情况下,都不会发生标识符冲突。
*ImportUUIDBehavior#IMPORT UID U COLLISION _REMOVE U EXISTING:如果传入节点与工作区中已存在的节点具有相同的标识符,则在添加传入节点之前,将已存在的节点(及其子图)从工作区中的任何位置移除。请注意,这可能会导致节点从工作区中远离写入传入子图的位置的位置“消失”。删除和新添加的内容将于[$9$]发送。
*ImportUUIDBehavior#IMPORT UID U COLLISION _REPLACE U EXISTING:如果传入节点与工作区中已存在的节点具有相同的标识符,则已存在的节点将被与现有节点位于相同位置的传入节点替换。请注意,这可能会导致传入的子图被分解并“分散”到工作区中的不同位置。在最极端的情况下,这种行为可能会导致根本没有节点被添加为parentAbsPath的子节点。如果传入XML的最顶层元素与工作区中其他地方的现有节点具有相同的标识符,就会发生这种情况。零钱将于[$11$]发送。
*ImportUUIDBehavior#IMPORT UID U CLISION U THROW:如果传入节点与工作区中已存在的节点具有相同的标识符,则会抛出ItemExistsException
与Workspace#importXML不同,此方法在反序列化期间不一定强制执行所有节点类型约束。在此实现的正常写入方法(Node.addNodeNode.setProperty等)中立即强制执行的那些会在反序列化过程中立即导致ConstraintViolationException。所有其他约束都会在save上进行检查,就像在正常写入操作中一样。然而,实施哪些节点类型约束取决于导入数据中的节点类型信息是否得到尊重,这是一个特定于实现的问题。
如果uuidBehavior设置为IMPORT_UUID_COLLISION_REMOVE_EXISTING,并且传入节点与parentAbsPath处的节点或其祖先节点具有相同的标识符,则ConstraintViolationException也将立即抛出。
如果[$22$]上不存在节点,则在分派或持久化时立即抛出PathNotFoundException。执行此验证的时间可能会有所不同
如果由于节点类型或其他特定于实现的约束,无法将新子图添加到位于parentAbsPath的节点,则ConstraintViolationException会在分派或持久化时立即抛出。执行此验证的时间可能会有所不同。
如果parentAbsPath处的节点由于签入而为只读,则VersionException会在分派或持久化时立即抛出。执行此验证的时间可能会有所不同。
如果锁阻止子图的添加,则LockException会在分派或持久化时立即抛出。执行此验证的时间可能会有所不同。

代码示例

代码示例来源:origin: org.onehippo.cms7/hippo-repository-connector

/**
 * Forwards the method call to the underlying session.
 */
public void importXML(String parentAbsPath, InputStream in, int uuidBehaviour) throws IOException,
    PathNotFoundException, ItemExistsException, ConstraintViolationException, VersionException,
    InvalidSerializedDataException, LockException, RepositoryException {
  session.importXML(parentAbsPath, in, uuidBehaviour);
}

代码示例来源:origin: apache/jackrabbit-oak

@Override
protected void runTest() throws Exception {
  InputStream in = getClass().getClassLoader().getResourceAsStream("deepTree.xml");
  adminSession.importXML(testRoot.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
  adminSession.save();
}

代码示例来源:origin: Adobe-Consulting-Services/acs-aem-commons

@Override
default void importXML(final String parentAbsPath, final InputStream in, final int uuidBehavior)
    throws IOException, PathNotFoundException, ItemExistsException, ConstraintViolationException,
    VersionException, InvalidSerializedDataException, LockException, RepositoryException {
  unwrapSession().importXML(parentAbsPath, in, uuidBehavior);
}

代码示例来源:origin: ModeShape/modeshape

@Override
public void importXML( String string,
            InputStream in,
            int i )
  throws IOException, PathNotFoundException, ItemExistsException, ConstraintViolationException, VersionException,
  InvalidSerializedDataException, LockException, RepositoryException {
  session().importXML(string, in, i);
}

代码示例来源:origin: apache/jackrabbit

/** {@inheritDoc} */
public void importXML(String path, byte[] xml, int mode)
    throws IOException, RepositoryException, RemoteException {
  try {
    session.importXML(path, new ByteArrayInputStream(xml), mode);
  } catch (RepositoryException ex) {
    throw getRepositoryException(ex);
  }
}

代码示例来源:origin: org.apache.jackrabbit/jackrabbit-jca

/**
 * Import XML content.
 */
public void importXML(String arg0, InputStream arg1, int arg2)
    throws IOException, PathNotFoundException, ItemExistsException,
    ConstraintViolationException, VersionException, InvalidSerializedDataException,
    LockException, RepositoryException {
  getSession().importXML(arg0, arg1, arg2);
}

代码示例来源:origin: apache/jackrabbit

public void doImportNoSave(String absPath, FileInputStream in, boolean useHandler)
      throws Exception {
    if (useHandler) {
      ContentHandler ih = session.getImportContentHandler(absPath, 0);
      createXMLReader(ih).parse(new InputSource(in));
    } else {
      session.importXML(absPath, in, ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
    }
  }
//------------< System view export import tests >-----------------------------------

代码示例来源:origin: apache/jackrabbit-oak

protected void runImport() throws RepositoryException, IOException {
  String path = target.getPath();
  InputStream in = new ByteArrayInputStream(XML.getBytes("UTF-8"));
  adminSession.importXML(target.getPath(), in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
}

代码示例来源:origin: apache/jackrabbit-oak

protected void doImport(String parentPath, String xml) throws IOException, RepositoryException {
  InputStream in = new ByteArrayInputStream(xml.getBytes("UTF-8"));
  if (isSessionImport()) {
    superuser.importXML(parentPath, in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
  } else {
    superuser.save();
    superuser.getWorkspace().importXML(parentPath, in, ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
  }
}

代码示例来源:origin: apache/jackrabbit-oak

public void testRemoveUUIDSameTree() throws Exception {
  superuser.save();
  try {
    superuser.importXML(path, getImportStream(), ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING);
    fail("ConstraintViolationException expected");
  } catch (ConstraintViolationException e) {
    // success
  }
}

代码示例来源:origin: apache/jackrabbit-oak

public void testTransientRemoveUUIDSameTree() throws Exception {
  try {
    superuser.importXML(path, getImportStream(), ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING);
    fail("ConstraintViolationException expected");
  } catch (ConstraintViolationException e) {
    // success
  }
}

代码示例来源:origin: apache/jackrabbit-oak

public void testTransientThrow() throws Exception {
    try {
      superuser.importXML(siblingPath, getImportStream(), ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
      fail("ItemExistsException expected");
    } catch (ItemExistsException e) {
      // success
    }
  }
}

代码示例来源:origin: brix-cms/brix-cms

public void importXML(String parentAbsPath, InputStream in, int uuidBehavior)
    throws IOException, RepositoryException {
  getActionHandler().beforeSessionImportXML(parentAbsPath);
  getDelegate().importXML(parentAbsPath, in, uuidBehavior);
  getActionHandler().afterSessionImportXML(parentAbsPath);
}

代码示例来源:origin: ModeShape/modeshape

@Test
public void shouldExportSharedNodesAsDocumentViewXml() throws RepositoryException, IOException {
  createExportableContent();
  // Export the content ...
  ByteArrayOutputStream baos = new ByteArrayOutputStream();
  session.exportDocumentView("/", baos, false, false);
  // System.out.println(baos);
  // Now import the content ...
  session2.importXML("/", new ByteArrayInputStream(baos.toByteArray()), ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
  session2.save();
  checkImportedContent(session2);
}

代码示例来源:origin: apache/jackrabbit-oak

public void testReplaceUUIDSameTree() throws Exception {
  superuser.save();
  superuser.importXML(path, getImportStream(), ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
  superuser.save();
  // original node must have been replaced (but no child node added)
  assertTrue(testRootNode.hasNode(nodeName1));
  Node n2 = testRootNode.getNode(nodeName1);
  assertTrue(n2.isNodeType(mixReferenceable));
  assertEquals(uuid, n2.getIdentifier());
  assertFalse(n2.hasNode(nodeName1));
}

代码示例来源:origin: apache/jackrabbit-oak

public void testRemoveUUID() throws Exception {
  superuser.save();
  superuser.importXML(siblingPath, getImportStream(), ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING);
  superuser.save();
  // original node must have been removed
  assertFalse(testRootNode.hasNode(nodeName1));
  Node sibling = superuser.getNode(siblingPath);
  assertTrue(sibling.hasNode(nodeName1));
  Node imported = sibling.getNode(nodeName1);
  assertTrue(imported.isNodeType(mixReferenceable));
  assertEquals(uuid, imported.getIdentifier());
}

代码示例来源:origin: info.magnolia/magnolia-core

@Test
public void bootstrapOfExistingNodesWillFailByDefault() throws Exception {
  // GIVEN
  final Session websiteSession = MgnlContext.getJCRSession(RepositoryConstants.WEBSITE);
  websiteSession.importXML(websiteSession.getRootNode().getPath(), getClass().getResourceAsStream("website.xml"), ImportUUIDBehavior.IMPORT_UUID_COLLISION_THROW);
  BootstrapSingleResource bootstrapSingleResource = new BootstrapSingleResource("", "", "/info/magnolia/module/delta/website.xml");
  // WHEN
  // THEN
  assertThat(() -> bootstrapSingleResource.execute(installContext), throwsAnException(instanceOf(RuntimeException.class)));
}

代码示例来源:origin: apache/jackrabbit-oak

public void testReplaceUUID() throws Exception {
  superuser.save();
  superuser.importXML(siblingPath, getImportStream(), ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
  superuser.save();
  // original node must have been replaced (but no child node added)
  assertTrue(testRootNode.hasNode(nodeName1));
  Node n2 = testRootNode.getNode(nodeName1);
  assertTrue(n2.isNodeType(mixReferenceable));
  assertEquals(uuid, n2.getIdentifier());
  Node sibling = superuser.getNode(siblingPath);
  assertFalse(sibling.hasNode(nodeName1));
}

代码示例来源:origin: apache/jackrabbit-oak

public void testTransientRemoveUUID() throws Exception {
  superuser.importXML(siblingPath, getImportStream(), ImportUUIDBehavior.IMPORT_UUID_COLLISION_REMOVE_EXISTING);
  superuser.save();
  // original node must have been removed
  assertFalse(testRootNode.hasNode(nodeName1));
  Node sibling = superuser.getNode(siblingPath);
  assertTrue(sibling.hasNode(nodeName1));
  Node imported = sibling.getNode(nodeName1);
  assertTrue(imported.isNodeType(mixReferenceable));
  assertEquals(uuid, imported.getIdentifier());
}

代码示例来源:origin: apache/jackrabbit-oak

public void testEmptyValues() throws Exception {
    superuser.importXML(targetPath, getImportStream(), ImportUUIDBehavior.IMPORT_UUID_CREATE_NEW);
    superuser.save();

    Node n = superuser.getNode(targetPath).getNode(nodeName4);
    Property p = n.getNode(nodeName3).getProperty("test:multiProperty");

    assertTrue(p.isMultiple());
    assertTrue(p.getDefinition().isMultiple());
    Assert.assertArrayEquals(new String[0], p.getValues());
  }
}

相关文章

微信公众号

最新文章

更多