/*
 *  Licensed to the Apache Software Foundation (ASF) under one or more
 *  contributor license agreements.  See the NOTICE file distributed with
 *  this work for additional information regarding copyright ownership.
 *  The ASF licenses this file to You 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 org.apache.harmony.tools.appletviewer;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;

import javax.swing.JLabel;

public class AppletInfo {
    private static final int DEFAULT_WIDTH=300;
    private static final int DEFAULT_HEIGHT=200;    
    
    private URL documentBase;
    private URL codeBase;
    private URL archive;
    private String archiveStr;
    private String tagName;
    private int width;
    private int height;
    private HashMap<String, String> params;
    private JLabel statusLabel = null;
    
    public AppletInfo() {
        params = new HashMap<String, String>();
    }

    public URL getDocumentBase() {
        return documentBase;
    }

    public void setDocumentBase(URL documentBase) {
        this.documentBase = documentBase;
    }

    public URL getCodeBase() {
        return codeBase;
    }

    public void setCodeBase(URL codeBase) {
        this.codeBase = codeBase;
    }

    public void setCodeBase(String codeBaseStr) throws MalformedURLException {
        this.codeBase = new URL(this.documentBase, (codeBaseStr == null)?"./":codeBaseStr);
    }

    public URL getArchive() {
        if (archive == null && archiveStr != null) {
            try {
                archive = new URL(getCodeBase(), archiveStr);
            } catch (MalformedURLException _) {                
            }
        }
        return archive;
    }

    public void setArchive(URL archive) {
        this.archive = archive;
    }

    public String getParameter(String name) {
        return params.get(name.toUpperCase());
    }

    public void setParameter(String name, String value) {
        params.put(name.toUpperCase(), value);
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public void setWidth(String widthStr) {     
        this.width = (widthStr == null)?DEFAULT_WIDTH:Integer.parseInt(widthStr);
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }   

    public void setHeight(String heightStr) {       
        this.height = (heightStr == null)?DEFAULT_HEIGHT:Integer.parseInt(heightStr);
    }

    public void setStatusLabel(JLabel statusLabel) {
        this.statusLabel  = statusLabel;
    }

    public void setStatus(String text) {
        if (statusLabel != null)
            statusLabel.setText(text);
    }
    
    public URL []getClassLoaderURLs() {
        archiveStr = getParameter("ARCHIVE");
    	URL []res = (archive == null && archiveStr == null)?new URL[1]:new URL[2];
    	switch (res.length) {
    		case 2: res[1] = getArchive();
    		case 1: res[0] = getCodeBase();
    	}
    	return res;
    }

    public void setTag(String tagName){
        this.tagName = tagName;
    }

    public String getTag(){
        return tagName;
    }
}

@@ -940,16 +940,50 @@
 HashMap;
+%0Aimport java.util.StringTokenizer;
 %0A%0Aimport
@@ -1207,24 +1207,25 @@
 vate URL
- archive
+%5B%5D clURLs
 ;%0A    pr
@@ -1995,371 +1995,8 @@
  %7D%0A%0A
-    public URL getArchive() %7B%0A        if (archive == null && archiveStr != null) %7B%0A            try %7B%0A                archive = new URL(getCodeBase(), archiveStr);%0A            %7D catch (MalformedURLException _) %7B                %0A            %7D%0A        %7D%0A        return archive;%0A    %7D%0A%0A    public void setArchive(URL archive) %7B%0A        this.archive = archive;%0A    %7D%0A%0A
     
@@ -2976,20 +2976,16 @@
 ;%0A    %7D%0A
-    
 %0A    pub
@@ -2991,19 +2991,19 @@
 blic URL
- 
 %5B%5D
+ 
 getClass
@@ -3013,24 +3013,59 @@
 derURLs() %7B%0A
+%0A        if (clURLs == null) %7B%0A    
         arch
@@ -3102,24 +3102,23 @@
 %22);%0A
-    %09URL %5B%5Dres =
+%0A            if
  (ar
@@ -3122,24 +3122,27 @@
 (archive
+Str
  == null
  && arch
@@ -3137,123 +3137,423 @@
 null
- && archiveStr == null)?new URL%5B1%5D:new URL%5B2%5D;%0A    %09switch (res.length) %7B%0A    %09%09case 2: res%5B1%5D = getArchive
+) %7B%0A                clURLs = new URL%5B%5D %7B getCodeBase() %7D;%0A            %7D else %7B%0A                StringTokenizer st = new StringTokenizer(archiveStr, %22, %22);%0A                int k = 0;%0A%0A                clURLs = new URL%5Bst.countTokens() + 1%5D;%0A                clURLs%5Bk++%5D = getCodeBase();%0A%0A                try %7B%0A                    while (st.hasMoreTokens()) %7B%0A                        String token = st.nextToken
 ();%0A    
 %09%09ca
@@ -3552,63 +3552,344 @@
     
+   
-%09%09case 1: res%5B0%5D = getCodeBase();%0A    %09%7D%0A
+                 clURLs%5Bk++%5D = new URL(getCodeBase(), token);%0A                    %7D%0A                %7D catch (MalformedURLException e) %7B%0A                    //TODO add exception handler%0A                %7D%0A            %7D%0A%0A        %7D%0A%0A 
     
-%09
+   
 return 
-re
+clURLs;%0A    %7D%0A%0A    public void setClassLoaderURLs(URL%5B%5D urls) %7B%0A        this.clURLs = url
 s;%0A 
@@ -3886,32 +3886,33 @@
  = urls;%0A    %7D%0A%0A
+%0A
     public void 

Fix for HARMONY-5577 was applied. Now it is possible to specify several jar files in "archive" parameter of "applet" tag.