/*
 * Copyright 2009-2011 Collaborative Research Centre SFB 632 
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package annis.service.objects;

import com.google.common.base.Splitter;
import java.io.Serializable;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Represents a single match of an AQL query.
 * 
 * @author Thomas Krause <krauseto@hu-berlin.de>
 */
@XmlRootElement
public class Match implements Serializable
{
  
  private final static Logger log = LoggerFactory.getLogger(Match.class);

  private final static Splitter matchSplitter = Splitter.on(" ").trimResults().omitEmptyStrings();
  
  private List<URI> saltIDs;

  public Match()
  {
    saltIDs = new ArrayList<URI>();
  }
  
  public Match(Collection<URI> original)
  {
    saltIDs = new ArrayList<URI>(original);
  }

  public void addSaltId(URI id)
  {
    if(id != null)
    {
      saltIDs.add(id);
    }
  }

  @XmlElement(name="id")
  public List<URI> getSaltIDs()
  {
    return saltIDs;
  }

  public void setSaltIDs(List<URI> saltIDs)
  {
    this.saltIDs = saltIDs;
  }
  
  public static Match parseFromString(String raw)
  {
    Match match = new Match();

    for (String id : matchSplitter.split(raw))
    {
      URI uri;
      try
      {
        uri = new java.net.URI(id);

        if (!"salt".equals(uri.getScheme()) || uri.getFragment() == null)
        {
          throw new URISyntaxException("not a Salt id", uri.toString());
        }
      }
      catch (URISyntaxException ex)
      {
        log.error("Invalid syntax for ID " + id, ex);
        continue;
      }
      match.addSaltId(uri);
    }

    return match;
  }

  /**
   * Returns a space seperated list of all Salt IDs.
   * @return 
   */
  @Override
  public String toString()
  {
    StringBuilder sb = new StringBuilder();
    Iterator<URI> it = saltIDs.iterator();
    while(it.hasNext())
    {
      URI u = it.next();
      sb.append(u.toASCIIString());
      if(it.hasNext())
      {
        sb.append(" ");
      }
    }
    return sb.toString();
  }
  
  
  
}

@@ -651,16 +651,54 @@
 jects;%0A%0A
+import com.google.common.base.Joiner;%0A
 import c
@@ -899,16 +899,45 @@
 erator;%0A
+import java.util.LinkedList;%0A
 import j
@@ -2651,234 +2651,228 @@
     
-StringBuilder sb = new StringBuilder();%0A    Iterator%3CURI%3E it = saltIDs.iterator();%0A    while(it.hasNext())%0A    %7B%0A      URI u = it.next();%0A      sb.append(u.toASCIIString());%0A      if(it.hasNext())%0A      %7B%0A        sb.append(%22 %22
+Iterator%3CURI%3E it = saltIDs.iterator();%0A    LinkedList%3CString%3E asString = new LinkedList%3CString%3E();%0A    while(it.hasNext())%0A    %7B%0A      URI u = it.next();%0A      if(u != null)%0A      %7B%0A        asString.add(u.toASCIIString()
 );%0A 
@@ -2899,20 +2899,36 @@
 urn 
-sb.to
+Joiner.on(%22 %22).join(as
 String
-(
 );%0A 

make Match.toString null safe