/*
 *  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 code;
    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() {
        return archive;
    }

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

    public void setArchive(String archive) throws MalformedURLException {
        this.archive = (archive == null)?null:new URL(this.documentBase, archive);
    }

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

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

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = (code.endsWith(".class"))?code.substring(0, code.length()-6):code;
    }

    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() {
    	URL []res = (archive == null)?new URL[1]:new URL[2];
    	switch (res.length) {
    		case 2: res[1] = archive;
    		case 1: res[0] = codeBase;
    	}
    	return res;
    }

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

    public String getTag(){
        return tagName;
    }
}

@@ -1179,24 +1179,55 @@
 RL archive;%0A
+    private String archiveStr;%0A
     private 
@@ -2007,24 +2007,245 @@
 Archive() %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
         retu
@@ -2391,119 +2391,43 @@
 ve) 
-throws MalformedURLException %7B%0A        this.archive = (archive == null)?null:new URL(this.documentBase,
+%7B%0A        this.archiveStr =
  archive
 );%0A 
@@ -2422,17 +2422,16 @@
  archive
-)
 ;%0A    %7D%0A
@@ -3676,24 +3676,46 @@
  (archive ==
+ null && archiveStr ==
  null)?new U
@@ -3782,23 +3782,28 @@
 es%5B1%5D = 
-a
+getA
 rchive
+()
 ;%0A    %09%09
@@ -3819,24 +3819,29 @@
 es%5B0%5D = 
-c
+getC
 odeBase
+()
 ;%0A    %09%7D

Patch for HARMONY-5450 "[jdktools][appletviewer] appletviewer throws java.lang.ClassNotFoundException while applet class placed in jar archive"