001/*
002 *  Copyright 2011 Anyware Services
003 *
004 *  Licensed under the Apache License, Version 2.0 (the "License");
005 *  you may not use this file except in compliance with the License.
006 *  You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 *  Unless required by applicable law or agreed to in writing, software
011 *  distributed under the License is distributed on an "AS IS" BASIS,
012 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 *  See the License for the specific language governing permissions and
014 *  limitations under the License.
015 */
016
017package org.ametys.core.util.dom;
018
019import java.io.File;
020import java.util.HashMap;
021import java.util.Map;
022
023import org.w3c.dom.Element;
024import org.w3c.dom.Node;
025
026/**
027 * DOM layer on top if a {@link File} hierarchy.
028 */
029public class FileElement extends AbstractWrappingAmetysElement<File>
030{
031    /**
032     * Constructor.
033     * @param file the underlying {@link File}.
034     */
035    public FileElement(File file)
036    {
037        this(file, null);
038    }
039
040    /**
041     * Constructor.
042     * @param file the underlying {@link File}.
043     * @param parent the parent {@link Element}.
044     */
045    public FileElement(File file, FileElement parent)
046    {
047        super(file, parent);
048    }
049    
050    @Override
051    public String getTagName()
052    {
053        return _object.isDirectory() ? "collection" : "resource";
054    }
055    
056    @Override
057    protected Map<String, AmetysAttribute> _lookupAttributes()
058    {
059        Map<String, AmetysAttribute> result = new HashMap<>();
060        
061        result.put("name", new AmetysAttribute("name", "name", null, _object.getName(), this));
062        
063        return result;
064    }
065    
066    @Override
067    public boolean hasChildNodes()
068    {
069        File[] files = _object.listFiles();
070        return files != null ? files.length > 0 : false;
071    }
072    
073    @Override
074    public Node getFirstChild()
075    {
076        File[] files = _object.listFiles();
077        
078        if (files != null && files.length > 0)
079        {
080            return new FileElement(files[0], this);
081        }
082        
083        return null;
084    }
085    
086    @Override
087    public Node getNextSibling()
088    {
089        if (_parent == null)
090        {
091            return null;
092        }
093        
094        File parent = (File) ((AbstractWrappingAmetysElement) _parent).getWrappedObject();
095        
096        File[] children = parent.listFiles();
097        
098        boolean isNext = false;
099        File nextSibling = null;
100        int i = 0;
101        
102        while (nextSibling == null && i < children.length)
103        {
104            File child = children[i++];
105            
106            if (isNext)
107            {
108                nextSibling = child;
109            }
110            else if (_object.getAbsolutePath().equals(child.getAbsolutePath()))
111            {
112                isNext = true;
113            }
114        }
115        
116        return nextSibling == null ? null : new FileElement(nextSibling, (FileElement) _parent);
117    }
118}