/* JJT: 0.2.2 */

package w3c.pics.parser;


class PICSPort_Node extends SimpleNode implements URLEvalNode {
  PICSPort_Node(int id) {
    super(id);
  }

  String text = null;
  
  public String getText() {
    return text;
  }
  public boolean isValid(String URL) {
    int doubleslash = URL.indexOf("//");
    String noscheme = URL.substring(doubleslash+2);
    int colon = noscheme.indexOf(":");
    int slash = noscheme.indexOf("/");
    int port = -1;
    if (colon==-1) {
      // URL has no port, match only if rule is "*"
    }
    else {
      if (slash!=-1) {
	if (colon<slash) {
	  String portnum = noscheme.substring(colon+1, slash);
	  port = Integer.valueOf(portnum).intValue();
	}
	else {
	  // URL has no port, match only if rule is "*"
	}
      }
      else {
	String portnum = noscheme.substring(colon+1);
	port = Integer.valueOf(portnum).intValue();
      }
    }
    int dash = text.indexOf("-");
    if (dash==-1) {
      if (text.equals("*")) {
	return true;
      }
      int proport = Integer.valueOf(text).intValue();
      if (proport!=port) {
	return false;
      }
    }
    else {
      if (port==-1) {
	return false;
      }
      String num1 = text.substring(0, dash);
      String num2 = text.substring(dash+1);
      int proport1 = 0;
      int proport2 = 0;
      if (num1.equals("*")) {
	if (num2.equals("*")) {
	  return true;
	}
	else {
	  proport2 = Integer.valueOf(num2).intValue();
	  if (port<=proport2) {
	    return true;
	  }
	  else {
	    return false;
	  }
	}
      }
      else {
	if (num2.equals("*")) {
	  proport1 = Integer.valueOf(num1).intValue();
	  if (port>=proport1) {
	    return true;
	  }
	  else {
	    return false;
	  }
	}
	else {
	  proport1 = Integer.valueOf(num1).intValue();
	  proport2 = Integer.valueOf(num2).intValue();
	  if ((port>proport2) || (port<proport1)) {
	    return false;
	  }
	}
      }
    }
    return true;
  }

}
