JSR 170 node types 分為 Mixin types 和 Primary types。
這篇文章主要讨論Primary types,我參考了Jackrabbit Wiki 和 jcr2.0Api 将所有的Primary types節點整理如下圖所示。
可以看到所有的節點都是基于
nt:base
産生的,并且**
nt:base
**是一個
abstract
節點,如果你直接将
nt:base
節點當作節點類型建立新節點會得到一個抽象節點的異常提示。
Node ntbase = rootNode.addNode("ntbase", NodeType.NT_BASE);
javax.jcr.nodetype.ConstraintViolationException: Unable to add a node with an abstract node type: nt:base
jackrabbit wiki 上隻針對
nt:base,
nt:unstructured,
nt:hierarchyNode,
nt:file,
nt:folder,
nt:resource
這幾個NodeType做了說明,其他的節點沒有任何提示。是以本文隻對這些NodeType做使用說明,其他的節點cleanup123會在嘗試之後補充更新。
上述6個節點類型一共分為
-
nt:base 基礎類型
此類型是抽象節點類型不能直接用來建立新的節點,但是可以用SQL-2文法來查詢。
-
nt:unstructured 非結構化節點
可以設定所有類型的屬性和子節點。
原文為: The nt:unstructured node type permits all kinds of properties and child nodes to be added to a node
-
nt:hierarchyNode 結構化節點
nt:hierarchyNode也是一個抽象節點,他的具體實作節點有三個:
-
nt:file
可以新增一個jcr:content 的節點,通常使用nt:resources類型的節點。
-
nt:folder
可以新增一個
類型的節點nt:hierarchyNode
-
nt:linkedFile
可以添加一個jcr:content 的屬性,通常使用nt:resources類型的節點。
-
-
nt:resource
可以設定
,Property.JCR_ENCODING
,Property.JCR_MIMETYPE
,Property.JCR_DATA
這四個屬性。Property.JCR_LAST_MODIFIED
以下是具體使用這些節點的代碼:
//在基礎節點後增加非結構化
Node unstructured = rootNode.addNode("unstructured", NodeType.NT_UNSTRUCTURED);
//非結構化節點可以自定義自己的屬性
unstructured.setProperty("p1","abc");
unstructured.setProperty("p2","1234");
//在基礎節點下新增nt:hierarchyNode類型的nt:folder
Node picfolder = rootNode.addNode("picfolder", NodeType.NT_FOLDER);
//在folder下面建立nt:file節點
Node mypic1 = picfolder.addNode("mypic1", NodeType.NT_FILE);
//在mypic1檔案下可建立Node.JCR_CONTENT的節點可以是任何類型,但是通常會選擇nt:resource節點
Node sky = mypic1.addNode(Node.JCR_CONTENT, NodeType.NT_RESOURCE);
sky.setProperty(Property.JCR_ENCODING,"utf-8");
sky.setProperty(Property.JCR_MIMETYPE,"application/json");
sky.setProperty(Property.JCR_DATA, new BinaryImpl("{'id':001,'msg':'sky is blue!'}".getBytes()));
sky.setProperty(Property.JCR_LAST_MODIFIED,Calendar.getInstance().getTimeInMillis());