]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.server.jcr/src/main/java/org/argeo/jcr/tabular/JcrTabularRowIterator.java
2 * Copyright (C) 2007-2012 Argeo GmbH
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.jcr
.tabular
;
18 import java
.io
.InputStream
;
19 import java
.util
.ArrayList
;
20 import java
.util
.List
;
21 import java
.util
.concurrent
.ArrayBlockingQueue
;
23 import javax
.jcr
.Binary
;
24 import javax
.jcr
.Node
;
25 import javax
.jcr
.NodeIterator
;
26 import javax
.jcr
.Property
;
27 import javax
.jcr
.PropertyType
;
28 import javax
.jcr
.RepositoryException
;
30 import org
.apache
.commons
.io
.IOUtils
;
31 import org
.argeo
.ArgeoException
;
32 import org
.argeo
.jcr
.ArgeoTypes
;
33 import org
.argeo
.util
.CsvParser
;
34 import org
.argeo
.util
.tabular
.ArrayTabularRow
;
35 import org
.argeo
.util
.tabular
.TabularColumn
;
36 import org
.argeo
.util
.tabular
.TabularRow
;
37 import org
.argeo
.util
.tabular
.TabularRowIterator
;
39 /** Iterates over the rows of a {@link ArgeoTypes#ARGEO_TABLE} node. */
40 public class JcrTabularRowIterator
implements TabularRowIterator
{
41 private Boolean hasNext
= null;
42 private Boolean parsingCompleted
= false;
44 private Long currentRowNumber
= 0l;
46 private List
<TabularColumn
> header
= new ArrayList
<TabularColumn
>();
48 /** referenced so that we can close it */
49 private Binary binary
;
50 private InputStream in
;
52 private CsvParser csvParser
;
53 private ArrayBlockingQueue
<List
<String
>> textLines
;
55 public JcrTabularRowIterator(Node tableNode
) {
57 for (NodeIterator it
= tableNode
.getNodes(); it
.hasNext();) {
58 Node node
= it
.nextNode();
59 if (node
.isNodeType(ArgeoTypes
.ARGEO_COLUMN
)) {
60 Integer type
= PropertyType
.valueFromName(node
.getProperty(
61 Property
.JCR_REQUIRED_TYPE
).getString());
62 TabularColumn tc
= new TabularColumn(node
.getProperty(
63 Property
.JCR_TITLE
).getString(), type
);
67 Node contentNode
= tableNode
.getNode(Property
.JCR_CONTENT
);
68 if (contentNode
.isNodeType(ArgeoTypes
.ARGEO_CSV
)) {
69 textLines
= new ArrayBlockingQueue
<List
<String
>>(1000);
70 csvParser
= new CsvParser() {
71 protected void processLine(Integer lineNumber
,
72 List
<String
> header
, List
<String
> tokens
) {
74 textLines
.put(tokens
);
75 } catch (InterruptedException e
) {
76 // TODO Auto-generated catch block
79 // textLines.add(tokens);
80 if (hasNext
== null) {
82 synchronized (JcrTabularRowIterator
.this) {
83 JcrTabularRowIterator
.this.notifyAll();
88 csvParser
.setNoHeader(true);
89 binary
= contentNode
.getProperty(Property
.JCR_DATA
).getBinary();
90 in
= binary
.getStream();
91 Thread thread
= new Thread(contentNode
.getPath() + " reader") {
96 parsingCompleted
= true;
97 IOUtils
.closeQuietly(in
);
103 } catch (RepositoryException e
) {
104 throw new ArgeoException("Cannot read table " + tableNode
, e
);
108 public synchronized boolean hasNext() {
109 // we don't know if there is anything available
110 // while (hasNext == null)
113 // } catch (InterruptedException e) {
115 // // FIXME better deal with interruption
116 // Thread.currentThread().interrupt();
121 if (!textLines
.isEmpty())
124 // maybe the parsing is finished but the flag has not been set
125 while (!parsingCompleted
&& textLines
.isEmpty())
128 } catch (InterruptedException e
) {
130 // FIXME better deal with interruption
131 Thread
.currentThread().interrupt();
136 if (!textLines
.isEmpty())
139 // (parsingCompleted && textLines.isEmpty())
142 // if (!hasNext && textLines.isEmpty()) {
144 // IOUtils.closeQuietly(in);
147 // if (binary != null) {
148 // JcrUtils.closeQuietly(binary);
156 public synchronized TabularRow
next() {
158 List
<String
> tokens
= textLines
.take();
159 List
<Object
> objs
= new ArrayList
<Object
>(tokens
.size());
160 for (String token
: tokens
) {
161 // TODO convert to other formats using header
165 return new ArrayTabularRow(objs
);
166 } catch (InterruptedException e
) {
168 // FIXME better deal with interruption
173 public void remove() {
174 throw new UnsupportedOperationException();
177 public Long
getCurrentRowNumber() {
178 return currentRowNumber
;
181 public List
<TabularColumn
> getHeader() {