/*
 * Copyright 2000-2009 JetBrains s.r.o.
 *
 * 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.intellij.ide.plugins;

import com.intellij.openapi.extensions.PluginId;
import org.jetbrains.annotations.NonNls;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import java.util.ArrayList;
import java.util.Stack;

/**
 * Created by IntelliJ IDEA.
 * User: stathik
 * Date: Mar 28, 2003
 * Time: 12:57:45 AM
 * To change this template use Options | File Templates.
 */
class RepositoryContentHandler extends DefaultHandler {
  @NonNls public static final String CATEGORY = "category";
  @NonNls public static final String IDEA_PLUGIN = "idea-plugin";
  @NonNls public static final String NAME = "name";
  @NonNls public static final String ID = "id";
  @NonNls public static final String DESCRIPTION = "description";
  @NonNls public static final String VERSION = "version";
  @NonNls public static final String VENDOR = "vendor";
  @NonNls public static final String EMAIL = "email";
  @NonNls public static final String URL = "url";
  @NonNls public static final String IDEA_VERSION = "idea-version";
  @NonNls public static final String SINCE_BUILD = "since-build";
  @NonNls private static final String UNTIL_BUILD = "until-build";
  @NonNls public static final String CHNAGE_NOTES = "change-notes";
  @NonNls private static final String DEPENDS = "depends";
  @NonNls private static final String DOWNLOADS = "downloads";
  @NonNls private static final String SIZE = "size";
  @NonNls private static final String RATING = "rating";

  @NonNls private static final String DATE = "date";
  private PluginNode currentPlugin;
  private String currentValue;
  private ArrayList<IdeaPluginDescriptor> plugins;
  private Stack<String> categoriesStack;


  public void startDocument() throws SAXException {
    plugins = new ArrayList<IdeaPluginDescriptor>();
    categoriesStack = new Stack<String>();
  }

  public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    if (qName.equals(CATEGORY)) {
      categoriesStack.push(atts.getValue(NAME));
    }
    else if (qName.equals(IDEA_PLUGIN)) {
      String categoryName = constructCategoryTree();
      currentPlugin = new PluginNode();
      currentPlugin.setCategory(categoryName);
      currentPlugin.setDownloads(atts.getValue(DOWNLOADS));
      currentPlugin.setSize(atts.getValue(SIZE));
      currentPlugin.setUrl(atts.getValue(URL));
      currentPlugin.setDate(atts.getValue(DATE));

      plugins.add(currentPlugin);
    }
    else if (qName.equals(IDEA_VERSION)) {
      currentPlugin.setSinceBuild(atts.getValue(SINCE_BUILD));
      currentPlugin.setUntilBuild(atts.getValue(UNTIL_BUILD));
    }
    else if (qName.equals(VENDOR)) {
      currentPlugin.setVendorEmail(atts.getValue(EMAIL));
      currentPlugin.setVendorUrl(atts.getValue(URL));
    }
    currentValue = "";
  }

  public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
    if (qName.equals(ID)) {
      currentPlugin.setId(currentValue);
    }
    else if (qName.equals(NAME)) {
      currentPlugin.setName(currentValue);
    }
    else if (qName.equals(DESCRIPTION)) {
      currentPlugin.setDescription(currentValue);
    }
    else if (qName.equals(VERSION)) {
      currentPlugin.setVersion(currentValue);
    }
    else if (qName.equals(VENDOR)) {
      currentPlugin.setVendor(currentValue);
    }
    else if (qName.equals(DEPENDS)) {
      currentPlugin.addDepends(PluginId.getId(currentValue));
    }
    else if (qName.equals(CHNAGE_NOTES)) {
      currentPlugin.setChangeNotes(currentValue);
    }
    else if (qName.equals(CATEGORY)) {
      categoriesStack.pop();
    }
    else if (qName.equals(RATING)) {
      currentPlugin.setRating(currentValue);
    }
    currentValue = "";
  }

  public void characters(char[] ch, int start, int length) throws SAXException {
    currentValue += new String(ch, start, length);
  }

  public ArrayList<IdeaPluginDescriptor> getPluginsList() {
    return plugins;
  }

  private String constructCategoryTree() {
    StringBuilder category = new StringBuilder();
    for (int i = 0; i < categoriesStack.size(); i++) {
      String str = categoriesStack.get(i);
      if (str.length() > 0) {
        if (i > 0) category.append("/");
        category.append(str);
      }
    }
    return category.toString();
  }
}

@@ -20,10 +20,10 @@
 0-20
-09
+13
  Jet
@@ -1987,24 +1987,91 @@
 downloads%22;%0A
+  @NonNls public static final String DOWNLOAD_URL = %22downloadUrl%22;%0A
   @NonNls pr
@@ -4427,32 +4427,131 @@
 ntValue);%0A    %7D%0A
+    else if (qName.equals(DOWNLOAD_URL)) %7B%0A      currentPlugin.setDownloadUrl(currentValue);%0A    %7D%0A
     currentValue

support downloadUrl for custom plugin repositories (IDEA-112778)