KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > services > jcr > impl > util > DocNodeExporter


1 /***************************************************************************
2  * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
3  * Please look at license.txt in info directory for more license detail. *
4  **************************************************************************/

5
6 package org.exoplatform.services.jcr.impl.util;
7
8 import javax.jcr.RepositoryException;
9
10
11 import java.util.Properties JavaDoc;
12 import java.util.List JavaDoc;
13
14
15 import javax.jcr.PropertyType;
16
17 import org.apache.commons.codec.binary.Base64;
18 import org.exoplatform.services.jcr.impl.Constants;
19 import org.exoplatform.services.jcr.impl.core.NodeImpl;
20 import org.exoplatform.services.jcr.impl.core.PropertyImpl;
21 import org.exoplatform.services.jcr.impl.core.WorkspaceImpl;
22 import org.exoplatform.services.jcr.storage.Container;
23
24 import javax.jcr.PropertyIterator;
25
26 /**
27  * Created by The eXo Platform SARL .
28  *
29  * @author <a HREF="mailto:geaz@users.sourceforge.net">Gennady Azarenkov</a>
30  * @version $Id: DocNodeExporter.java,v 1.5 2004/08/23 10:31:39 geaz Exp $
31  */

32
33 public class DocNodeExporter {
34
35
36   public static void export(Container container, NodeImpl node, XMLWriter writer,
37                             boolean binaryAsLink, boolean noRecurse) throws RepositoryException {
38
39     String JavaDoc name = node.getName();
40
41     if (name.length() == 0) // root node
42
name = "root";
43
44 // buffer.append("<"+name);
45
Properties JavaDoc attrs = new Properties JavaDoc();
46 // List props = container.getChildren(node.getPath(), Constants.PROPS);
47
// for(int i = 0; i<props.size(); i++) {
48
// PropertyImpl prop = (PropertyImpl)props.get(i);
49

50     PropertyIterator props = node.getProperties();
51     while (props.hasNext()) {
52       PropertyImpl prop = (PropertyImpl) props.next();
53       String JavaDoc strPropVal = getStrPropValue(prop, binaryAsLink);
54 // buffer.append(" "+prop.getName()+"=\""+strPropVal+"\"");
55
attrs.setProperty(prop.getName(), strPropVal);
56     }
57     writer.startElement(name, attrs);
58
59     List JavaDoc nodes = container.getChildren(node.getPath());
60     for (int i = 0; i < nodes.size(); i++) {
61       NodeImpl child = (NodeImpl) nodes.get(i);
62 // child.setWorkspace((WorkspaceImpl)node.getWorkspace());
63
if (!noRecurse)
64         export(container, child, writer, binaryAsLink, noRecurse);
65     }
66
67     writer.endElement();
68
69   }
70 /*
71     public static void export( Container container, String path, XMLWriter writer,
72                           boolean binaryAsLink, boolean noRecurse) throws RepositoryException {
73
74         String name = PathUtil.getName(path);
75         System.out.println("Name---"+name);
76
77         if(name.length() == 0) // root node
78              name = "root";
79
80 // buffer.append("<"+name);
81         Properties attrs = new Properties();
82
83         PropertyIterator props = container.getRootNode().getProperties();
84         System.out.println("Props---"+props);
85         while(props.hasNext()) {
86             PropertyImpl prop = (PropertyImpl)props.next();
87             String strPropVal = getStrPropValue(prop, binaryAsLink);
88             attrs.setProperty(prop.getName(), strPropVal);
89         }
90         writer.startElement(name, attrs);
91
92         List nodes = container.getChildren(path);
93         System.out.println("Nodes---"+nodes);
94         if(nodes.size() > 0) {
95            for(int i = 0; i<nodes.size(); i++) {
96                NodeImpl child = (NodeImpl)nodes.get(i);
97                if(!noRecurse)
98                    export(container, child, writer, binaryAsLink, noRecurse);
99            }
100         }
101
102         writer.endElement();
103
104     }
105 */

106   public static String JavaDoc getStrPropValue(PropertyImpl prop, boolean binaryAsLink) {
107
108     if (prop.getType() == PropertyType.BINARY) {
109       if (binaryAsLink)
110         return prop.getPath();
111       else {
112         String JavaDoc str = new String JavaDoc(Base64.encodeBase64(prop.toString().getBytes()));
113         return str;
114       }
115
116     } else
117       return StringConverter.normalizeString(prop.toString(), false);
118   }
119
120 }
121
Popular Tags