/*
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.xwiki.extension.xar.internal.handler.packager.xml;

import org.xml.sax.SAXException;
import org.xwiki.component.manager.ComponentManager;
import org.xwiki.extension.xar.internal.handler.packager.XarEntry;
import org.xwiki.model.EntityType;
import org.xwiki.model.reference.EntityReference;

public class XarPageLimitedHandler extends AbstractHandler
{
    private XarEntry xarEntry = new XarEntry();

    private EntityReference pageReference;

    private EntityReference spaceReference;

    public XarPageLimitedHandler(ComponentManager componentManager)
    {
        super(componentManager);

        setCurrentBean(this);

        this.spaceReference = new EntityReference("space", EntityType.SPACE);
        this.pageReference = new EntityReference("page", EntityType.DOCUMENT, this.spaceReference);

        addsupportedElements("name");
        addsupportedElements("web");
        addsupportedElements("language");
        addsupportedElements("defaultLanguage");
    }

    public XarEntry getXarEntry()
    {
        return this.xarEntry;
    }

    @Override
    public void endElementInternal(String uri, String localName, String qName) throws SAXException
    {
        if (qName.equals("language")) {
            if (this.value.length() > 0) {
                this.xarEntry.setLanguage(this.value.toString());
            }
        } else if (qName.equals("defaultLanguage")) {
            if (this.xarEntry.getLanguage() == null) {
                this.xarEntry.setLanguage(this.value.toString());
            }
        } else if (qName.equals("name")) {
            this.xarEntry.setDocumentReference(this.pageReference);
            this.pageReference.setName(this.value.toString());
        } else if (qName.equals("web")) {
            this.xarEntry.setDocumentReference(this.pageReference);
            this.spaceReference.setName(this.value.toString());
        } else {
            super.endElementInternal(uri, localName, qName);
        }
    }
}

@@ -1348,53 +1348,8 @@
 e;%0A%0A
-    private EntityReference spaceReference;%0A%0A
     
@@ -1488,36 +1488,35 @@
 ;%0A%0A        this.
-s
 pa
-c
+g
 eReference = new
@@ -1533,20 +1533,19 @@
 erence(%22
-s
 pa
-c
+g
 e%22, Enti
@@ -1555,44 +1555,29 @@
 ype.
-SPACE);%0A        this.pageReference =
+DOCUMENT,%0A           
  new
@@ -1586,35 +1586,36 @@
 ntityReference(%22
+s
 pa
-g
+c
 e%22, EntityType.D
@@ -1617,37 +1617,14 @@
 ype.
-DOCUMENT, this.spaceReference
+SPACE)
 );%0A%0A
@@ -2408,59 +2408,91 @@
 his.
-xarEntry.setDocumentReference(this.pageReference);%0A
+pageReference = new EntityReference(this.value.toString(), EntityType.DOCUMENT,%0A   
     
@@ -2522,37 +2522,87 @@
 nce.
-setName(this.value.toString()
+getParent());%0A            this.xarEntry.setDocumentReference(this.pageReference
 );%0A 
@@ -2659,36 +2659,61 @@
    this.
-xarEntry.setDocument
+pageReference = new EntityReference(this.page
 Referenc
@@ -2705,33 +2705,34 @@
 is.pageReference
-(
+, 
 this.pageReferen
@@ -2737,72 +2737,165 @@
 ence
-);%0A            this.spaceReference.setName(this.value.toString()
+.getParent(),%0A               new EntityReference(this.value.toString(), EntityType.SPACE));%0A            this.xarEntry.setDocumentReference(this.pageReference
 );%0A 

Make EntityReference immutables, adding parameters to EntityReference and allowing Locale and Version in DocumentReference