/*
 * Part of the CCNx Java Library.
 *
 * Copyright (C) 2010, 2011 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation. 
 * This library 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 library;
 * if not, write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301 USA.
 */
package org.ccnx.ccn.profiles.namespace;

import org.ccnx.ccn.impl.encoding.CCNProtocolDTags;
import org.ccnx.ccn.impl.encoding.GenericXMLEncodable;
import org.ccnx.ccn.impl.encoding.XMLDecoder;
import org.ccnx.ccn.impl.encoding.XMLEncoder;
import org.ccnx.ccn.io.content.ContentDecodingException;
import org.ccnx.ccn.io.content.ContentEncodingException;
import org.ccnx.ccn.protocol.ContentName;

public class ParameterizedName extends GenericXMLEncodable {
	
	
	public static class PrefixName extends ContentName {
		
		private static final long serialVersionUID = 5093471560639087706L;

		public PrefixName(ContentName other) {
			super(other);
		}
		
		public PrefixName() {}
		
		@Override
		public long getElementLabel() {
			return CCNProtocolDTags.Prefix;
		}
	}

	public static class SuffixName extends ContentName {
		
		private static final long serialVersionUID = 3252505381608872546L;

		public SuffixName(ContentName other) {
			super(other);
		}
		
		public SuffixName() {}
		
		@Override
		public long getElementLabel() {
			return CCNProtocolDTags.Suffix;
		}
	}

	protected String _label;
	protected PrefixName _prefix;
	protected SuffixName _suffix;
	
	public ParameterizedName(String label, ContentName prefix, ContentName suffix) {
		_label = label;
		_prefix = new PrefixName(prefix);
		_suffix = (null != suffix) ? new SuffixName(suffix) : null;
	}
	
	public ParameterizedName() {
	}
	
	public String label() { return _label; }
	
	public ContentName prefix() { return new ContentName(_prefix); }
	
	public ContentName suffix() { return (null != _suffix) ? new ContentName(_suffix) : null; }
	
	public boolean emptyLabel() { return (null == label()); }
	
	public boolean emptySuffix() { return (null == _suffix); }

	@Override
	public void decode(XMLDecoder decoder) throws ContentDecodingException {
		decoder.readStartElement(getElementLabel());
		
		if (decoder.peekStartElement(CCNProtocolDTags.Label)) {
			_label = decoder.readUTF8Element(CCNProtocolDTags.Label);
		}
		
		_prefix = new PrefixName();
		_prefix.decode(decoder);

		if (decoder.peekStartElement(CCNProtocolDTags.Suffix)) {
			_suffix = new SuffixName();
			_suffix.decode(decoder);
		}
		decoder.readEndElement();
	}

	@Override
	public void encode(XMLEncoder encoder) throws ContentEncodingException {
		if (!validate()) {
			throw new ContentEncodingException("Cannot encode " + this.getClass().getName() + ": field values missing.");
		}
		encoder.writeStartElement(getElementLabel());
		
		if (!emptyLabel()) {
			encoder.writeElement(CCNProtocolDTags.Label, label());
		}
		
		_prefix.encode(encoder);

		if (!emptySuffix()) {
			_suffix.encode(encoder);
		}

		encoder.writeEndElement();   		
	}

	@Override
	public long getElementLabel() {
		return CCNProtocolDTags.ParameterizedName;
	}

	@Override
	public boolean validate() {
		return (null != _prefix);
	}
	
	@Override
	public String toString() {
		return label() + ": prefix: " + prefix() + ", suffix: " + suffix();
	}

}

@@ -2389,130 +2389,8 @@
 %7D%0A%09%0A
-%09public boolean emptyLabel() %7B return (null == label()); %7D%0A%09%0A%09public boolean emptySuffix() %7B return (null == _suffix); %7D%0A%0A
 %09@Ov
@@ -3142,21 +3142,22 @@
 if (
-!emptyLabel()
+_label != null
 ) %7B%0A
@@ -3204,23 +3204,22 @@
 .Label, 
+_
 label
-()
 );%0A%09%09%7D%0A%09
@@ -3258,22 +3258,23 @@
 if (
-!emptySuffix()
+_suffix != null
 ) %7B%0A
@@ -3558,23 +3558,22 @@
 %09return 
+_
 label
-()
  + %22: pr
@@ -3582,24 +3582,23 @@
 ix: %22 + 
+_
 prefix
-()
  + %22, su
@@ -3607,24 +3607,23 @@
 ix: %22 + 
+_
 suffix
-()
 ;%0A%09%7D%0A%0A%7D%0A

Fix encoding of ParameterizedNames. refs #100370