/*
 * Copyright 2011 Splunk, Inc.
 *
 * 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 com.splunk;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import java.util.ArrayList;
import java.util.List;

/**
 * Parent class that all other endpoints classes will derive from
 */

public class Client {

    // UNDONE -- make inner class to support more than one iterator per client.
    public Service service = null;

    public Client() {
        // default constructor
    }
    public Client(Service serv) {
        service = serv;
    }

    public List<String> getList(Document doc, String path) {
        List<String> outlist = new ArrayList<String>();
        NodeList nl = doc.getElementsByTagName("id");

        // get everything in ID after base path
        for (int idx=1; idx < nl.getLength(); idx++) {
            String [] parts = nl.item(idx).getTextContent().split(path);
            if (parts.length >= 2) {
                // UNDONE: recode to non-url-safe?
                outlist.add(parts[1]);
            }
        }

        return outlist;
    }

    public Entity get(String path) throws Exception {
        Convert converter = new Convert();
        return converter.convertXMLData(service.get(path).getContent());
    }

    public Entity delete(String path) throws Exception {
        Convert converter = new Convert();
        // place holder -- service needs a delete method
        return converter.convertXMLData(service.get/*deletegoeshere*/(path).getContent());
    }

    public List<String> nameList(String path) throws Exception {
        Document doc = service.parseXml(service.get(path));
        return getList(doc, path);
    }
}

@@ -1230,16 +1230,157 @@
 e(%22id%22);
+%0A        // extract last element of base path%0A        String %5B%5D bits = path.split(%22/%22);%0A        String delimiter = bits%5Bbits.length-1%5D + %22/%22;
 %0A%0A      
@@ -1411,25 +1411,40 @@
 D after 
-base
+last
  path
+ prefix element
 %0A       
@@ -1514,19 +1514,20 @@
 ing %5B%5D p
-art
+iece
 s = nl.i
@@ -1562,56 +1562,20 @@
 lit(
-path);%0A            if (parts.length %3E= 2) %7B%0A    
+delimiter);%0A
     
@@ -1599,34 +1599,56 @@
 : re
+-en
 code 
-to non-
 url
--
+ 
 safe
-?%0A    
+ string back to regular string?%0A
     
@@ -1672,32 +1672,19 @@
 dd(p
-art
+iece
 s%5B1%5D);%0A
-            %7D%0A
     

Fix id grabber to normalize different path from request to response