Android Open Source - classy_apps Alfresco File Plan Xml Parser






From Project

Back to project page classy_apps.

License

The source code is released under:

GNU General Public License

If you think the Android project classy_apps listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/**
    Copyright (C) 2014  www.cybersearch2.com.au
//from w  w  w. ja  va  2s .  c  o m
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/> */
package au.com.cybersearch2.classyfy.data.alfresco;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import au.com.cybersearch2.classyfy.data.DataStreamParser;
import au.com.cybersearch2.classyfy.data.RecordField;
import au.com.cybersearch2.classynode.Node;
import au.com.cybersearch2.classyfy.data.Model;

/**
 * AlfrescoFilePlanXmlParser
 * @author Andrew Bowley
 * 14/04/2014
 */
public class AlfrescoFilePlanXmlParser implements DataStreamParser
{
    private XmlPullParser xpp;
 
    
    private String[] SKIP_LIST = 
    {
            "dispositionSchedule",
            "dispositionAction",
            "aspects",
            "acl"
    };

    public AlfrescoFilePlanXmlParser()
    {
        XmlPullParserFactory factory;
        try
        {
            factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            xpp = factory.newPullParser();
        }
        catch (XmlPullParserException e)
        {
            e.printStackTrace();
        }
        
    }

    @Override
    public Node parseDataStream(InputStream stream) 
    {
        Reader r = new BufferedReader(new InputStreamReader(stream));
        return parseDocument(r);
    }

    public Node parseDocument(Reader reader)
    {
        Node root = Node.rootNodeNewInstance();
        try
        {
            xpp.setInput(reader);
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) 
            {
                if (eventType == XmlPullParser.START_DOCUMENT) 
                {
                    System.out.println("Start document");
                } 
                else if (eventType == XmlPullParser.START_TAG) 
                {
                    if ("recordCategory".equals(xpp.getName()))
                        if (!addNode(new Node(Model.recordCategory.ordinal(), root)))
                            break;
                    else if ("recordFolder".equals(xpp.getName()))
                        if (!addNode(new Node(Model.recordFolder.ordinal(), root)))
                            break;
                } 
                /*
                else if (eventType == XmlPullParser.END_TAG) 
                {
                    System.out.println("End tag "+xpp.getName());
                } 
                else if (eventType == XmlPullParser.TEXT) 
                {
                    System.out.println("Text "+xpp.getText());
                }
                */
                eventType = xpp.next();
            }
        }
        catch (XmlPullParserException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return root;
    }
    
    private boolean addNode(Node node) 
    {
        try
        {
            int eventType = xpp.next();
            while (eventType != XmlPullParser.END_DOCUMENT) 
            {
                if (eventType == XmlPullParser.START_TAG) 
                {
                    if ("recordCategory".equals(xpp.getName()))
                        addNode(new Node(Model.recordCategory.ordinal(), node));
                    else if ("recordFolder".equals(xpp.getName()))
                        addNode(new Node(Model.recordFolder.ordinal(), node));
                    else if (!skipList(xpp.getName(), SKIP_LIST))
                        break;
                    else if ("properties".equals(xpp.getName()) && !addProperties(node))
                        break;
                } 
                else if (eventType == XmlPullParser.END_TAG) 
                {
                    if ((node.getModel() == Model.recordCategory.ordinal()) && "recordCategory".equals(xpp.getName()))
                        return true;
                    if ((node.getModel() == Model.recordFolder.ordinal()) && "recordFolder".equals(xpp.getName()))
                        return true;
                } 
                else if (eventType == XmlPullParser.TEXT) 
                {
                }
                eventType = xpp.next();
            }
        }
        catch (XmlPullParserException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        return false; // Should exit from loop if no error
    }
  
    boolean skipList(final String currentElement, final String[] elementNames) throws XmlPullParserException, IOException
    {
        for (String skipElement: elementNames)
        {
            if (currentElement.equals(skipElement))
            {
                return skipElements(skipElement);
            }
        }
        return true;
    }
    
    boolean skipElements(String enclosingTagName) throws XmlPullParserException, IOException
    {
        int nestCount = 0;
        int eventType = xpp.next();
        while (eventType != XmlPullParser.END_DOCUMENT) 
        {
            if (eventType == XmlPullParser.END_TAG) 
            {
                if (enclosingTagName.equals(xpp.getName()) && (nestCount-- == 0))
                     return true;
            }
            if (eventType == XmlPullParser.START_TAG) 
            {
                if (enclosingTagName.equals(xpp.getName()))
                    ++nestCount;
            }
            eventType = xpp.next();
        }
        return false;
   }
    
    private boolean addProperties(Node node) throws XmlPullParserException, IOException 
    {
        boolean inMlvalue = false;
        RecordField currentField = RecordField.OTHER;
        int eventType = xpp.next();
        while (eventType != XmlPullParser.END_DOCUMENT) 
        {
            if (eventType == XmlPullParser.START_TAG) 
            {
                if (!hasAttribute("isNull", "true"))
                {
                    RecordField recordField = RecordField.getRecordField(xpp.getName());
                    if (recordField != null)
                        currentField = recordField;
                    else if ("mlvalue".equals(xpp.getName()))
                        inMlvalue = true;
                }
            }
            else if (eventType == XmlPullParser.END_TAG) 
            {
                if ("properties".equals(xpp.getName()))
                    return true;
                RecordField recordField = RecordField.getRecordField(xpp.getName());
                if (currentField == recordField)
                    currentField = RecordField.OTHER;
                else if (inMlvalue && "mlvalue".equals(xpp.getName()))
                    inMlvalue = false;
            } 
            else if (eventType == XmlPullParser.TEXT) 
            {
                String value = xpp.getText();
                value.trim();
                if (currentField == RecordField.name)
                {
                    node.setName(value);
                }
                else if (currentField == RecordField.title)
                {    
                    if (inMlvalue)
                    {
                        node.setTitle(value);
                    //(currentField == RecordField.description)
                        inMlvalue = false;
                    }
                }
                else if (currentField != RecordField.OTHER)
                {
                    node.getProperties().put(currentField.toString(), value);
                    currentField = RecordField.OTHER;
                }
            }
            eventType = xpp.next();
       }
       return false;
    }
    
    boolean hasAttribute(String name, String value)
    {
        for (int i = 0; i < xpp.getAttributeCount(); i++)
            if (xpp.getAttributeName(i).equals(name) && xpp.getAttributeValue(i).equals(value))
                return true;
        return false;
    }
}




Java Source Code List

au.com.cybersearch2.classyfy.ClassyFyApplicationModule.java
au.com.cybersearch2.classyfy.ClassyFyApplication.java
au.com.cybersearch2.classyfy.ClassyFyEnvironmentModule.java
au.com.cybersearch2.classyfy.ClassyFyResourceEnvironment.java
au.com.cybersearch2.classyfy.ClassyFyStartup.java
au.com.cybersearch2.classyfy.ClassyFyThreadHelper.java
au.com.cybersearch2.classyfy.MainActivityTest.java
au.com.cybersearch2.classyfy.MainActivity.java
au.com.cybersearch2.classyfy.NodeDetailsDialog.java
au.com.cybersearch2.classyfy.NodeDetailsFragment.java
au.com.cybersearch2.classyfy.ProgressFragment.java
au.com.cybersearch2.classyfy.TitleSearchResultsActivityTest.java
au.com.cybersearch2.classyfy.TitleSearchResultsActivity.java
au.com.cybersearch2.classyfy.TitleSearchResultsFragment.java
au.com.cybersearch2.classyfy.data.DataLoader.java
au.com.cybersearch2.classyfy.data.DataStreamParser.java
au.com.cybersearch2.classyfy.data.FieldDescriptor.java
au.com.cybersearch2.classyfy.data.FilePlanNodeType.java
au.com.cybersearch2.classyfy.data.ManagedRecord.java
au.com.cybersearch2.classyfy.data.Model.java
au.com.cybersearch2.classyfy.data.RecordCategory.java
au.com.cybersearch2.classyfy.data.RecordField.java
au.com.cybersearch2.classyfy.data.RecordFolder.java
au.com.cybersearch2.classyfy.data.SqlFromNodeGenerator.java
au.com.cybersearch2.classyfy.data.alfresco.AlfrescoFilePlanLoaderModule.java
au.com.cybersearch2.classyfy.data.alfresco.AlfrescoFilePlanLoader.java
au.com.cybersearch2.classyfy.data.alfresco.AlfrescoFilePlanXmlParser.java
au.com.cybersearch2.classyfy.helper.FileUtils.java
au.com.cybersearch2.classyfy.provider.ClassyFyProviderTest.java
au.com.cybersearch2.classyfy.provider.ClassyFyProvider.java
au.com.cybersearch2.classyfy.provider.ClassyFySearchEngine.java
au.com.cybersearch2.classyjpa.entity.LoaderPersistenceContainerTest.java
au.com.cybersearch2.classyjpa.entity.TestPersistenceWork.java
au.com.cybersearch2.classyjpa.entity.UserPersistenceContainerTest.java
au.com.cybersearch2.classyutil.Transcript.java
au.com.cybersearch2.example.AndroidHelloTwoDbsModule.java
au.com.cybersearch2.example.AndroidHelloTwoDbs.java
au.com.cybersearch2.example.AppThreadHelper.java
au.com.cybersearch2.example.AppThreadHelper.java
au.com.cybersearch2.example.HelloTwoDbsEnvironmentModule.java
au.com.cybersearch2.example.HelloTwoDbsEnvironmentModule.java
au.com.cybersearch2.example.v2.AndroidHelloTwoDbsModule.java
au.com.cybersearch2.example.v2.AndroidHelloTwoDbs.java
au.com.cybersearch2.example.v2.HelloTwoDbsEnvironmentModule.java
com.example.hellotwodbs.HelloTwoDbsApplication.java
com.example.hellotwodbs.HelloTwoDbsApplication.java
com.example.hellotwodbs.HelloTwoDbs.java
com.example.hellotwodbs.HelloTwoDbs.java