Expand my Community achievements bar.

SOLVED

Problem reading a text file

Avatar

Former Community Member

I'm having problem accessing a text file within the dam. my file's path is content/dam/testing.txt. I'm trying to read and create a node for each line read.

Do I need to access the file under renditions? ie: /content/dam/testing.txt/jcr:content/renditions/original

Here is my code:

 

public void execute(WorkItem item, WorkflowSession session, MetaDataMap args) throws WorkflowException {
        
        WorkflowData workflowData = item.getWorkflowData();
        if (workflowData.getPayloadType().equals(TYPE_JCR_PATH)) {
            

            String path = workflowData.getPayload().toString();
            String nodepath = workflowData.getPayload().toString() + "/jcr:content";
          
                    
                    ResourceResolver resourceResolver = resolverFactory.getAdministrativeResourceResolver(null);
                    
                    Session jcrSession = session.adaptTo(Session.class);
                    
                    Node jcrNode = (Node) jcrSession.getItem(path);
                
                try
                {
                    Resource rs = resourceResolver.getResource(path);
                    
                    File file  = rs.adaptTo(File.class);
                    
                    
                    BufferedReader br = new BufferedReader(new FileReader(file));
                    String sCurrentLine;
                    
                    int r = 2;
                    String[] readarray = new String[r];
                    
                    while ((sCurrentLine = br.readLine()) != null && r!=2) {
                        
                        readarray[r] = sCurrentLine;
                        Node propNode = jcrNode.addNode("txtITEM", "nt:unstructured");
                        

                          if (jcrNode != null) {
        
                              propNode.setProperty("line", readarray[r]);

                              jcrSession.save();
        
                          }
             
                        r++;
            
                    }

 

I took out the try statements for easy reading. Do I have to read this text file as a binary instead?..I'm adapting the resource as a file class.

Thanks

1 Accepted Solution

Avatar

Correct answer by
Level 9

File should have an nt:file node, which has a jcr:content node with jcr:data property. You can read the xml from jcr:data i.e: jcrContent.getProperty("jcr:data").getBinary().getStream();

Your code could be similar to this

InputStream is = jcrContent.getProperty("jcr:data").getBinary()
                    .getStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            int resultNumber = bis.read();
            while (resultNumber != -1) {
                byte b = (byte) resultNumber;
                buf.write(b);
                resultNumber = bis.read();
            }

View solution in original post

3 Replies

Avatar

Correct answer by
Level 9

File should have an nt:file node, which has a jcr:content node with jcr:data property. You can read the xml from jcr:data i.e: jcrContent.getProperty("jcr:data").getBinary().getStream();

Your code could be similar to this

InputStream is = jcrContent.getProperty("jcr:data").getBinary()
                    .getStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            int resultNumber = bis.read();
            while (resultNumber != -1) {
                byte b = (byte) resultNumber;
                buf.write(b);
                resultNumber = bis.read();
            }

Avatar

Former Community Member

Mshajiahmed wrote...

File should have an nt:file node, which has a jcr:content node with jcr:data property. You can read the xml from jcr:data i.e: jcrContent.getProperty("jcr:data").getBinary().getStream();

Your code could be similar to this

InputStream is = jcrContent.getProperty("jcr:data").getBinary()
                    .getStream();

            BufferedInputStream bis = new BufferedInputStream(is);
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            int resultNumber = bis.read();
            while (resultNumber != -1) {
                byte b = (byte) resultNumber;
                buf.write(b);
                resultNumber = bis.read();
            }

 


Hey thanks this work well, is there no way to get the resource as a file though? It'll be easier to do XML parsing with a file type instead of a XML file in binary.

Avatar

Employee Advisor

Hi,

a node isn't a file, so using the File interface doesn't make sense.

kind regards,
Jörg