/* * Jakelib2 - General purpose C++ library * Copyright (C) 2001 Florian Wolff (florian@donuz.de) * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * $Id: URL.jlc,v 1.3 2003/09/27 08:10:28 florian Exp $ */ #include "jakelib2.h" #include "jakelib2/net/URL.h" using namespace jakelib::net; using namespace jakelib::lang; JAKELIB_IMPLEMENT_CLASS("jakelib.net.URL", URL, Object) /*****************************************************************************\ * URL | *****************************************************************************/ URL::URL(char* url) { set(url); } URL::URL(String* url) { set(url->latin1()); } URL::URL(URL* url) { set(url); } URL::URL() { port = 0; scheme = String::emptyString; host = String::emptyString; file = String::emptyString; } URL::~URL() { } void URL::set(URL* url) { port = url->port; scheme = url->scheme; host = url->host; file = url->file; extractFName(); } void URL::set(char* url) { // int stdPort = 80; // try { // String urlStr(url); // urlStr.trim(); // String upper = urlStr; // upper.toUpperCase(); // if (upper.startsWith("HTTP://")) { // scheme = "http"; // stdPort = 80; // urlStr.remove(0, 7); // } // else if (upper.startsWith("HTTPS://")) { // scheme = "https"; // stdPort = 1443; // urlStr.remove(0, 8); // } // else if (upper.startsWith("FTP://")) { // scheme = "ftp"; // stdPort = 21; // urlStr.remove(0, 6); // } // else if (upper.startsWith("CDDB://")) { // scheme = "cddb"; // urlStr.remove(0, 7); // } // else if (upper.startsWith("FILE://")) { // scheme = "file"; // urlStr.remove(0, 7); // } // else if (upper.startsWith("NEWS://")) { // scheme = "news"; // urlStr.remove(0, 7); // } // int pos = urlStr.indexOf(":"); // if (pos >= 0) { // port = urlStr.substring(pos +1)->toInt(); // host = urlStr.substring(0, pos); // } // else { // port = 0; // host = urlStr; // } // int slashPos = urlStr.indexOf("/"); // if (slashPos > -1) { // file = urlStr.substring(slashPos); // host.remove(slashPos); // } // else { // file = "/"; // } // if (port == 0) { // port = stdPort; // } // //printf("Scheme: %s\nHost: %s\nPort: %u\nFile: %s\n", scheme.latin1(), host.latin1(), port, file.latin1()); // extractFName(); // } // catch (Exception* ex) { // throw new MalformedURLException(url); // } } /*****************************************************************************\ * getPort | *****************************************************************************/ int URL::getPort() { return port; } /*****************************************************************************\ * getHost | *****************************************************************************/ String* URL::getHost() { return host; } /*****************************************************************************\ * getScheme | *****************************************************************************/ String* URL::getScheme() { return scheme; } /*****************************************************************************\ * getFile | *****************************************************************************/ String* URL::getFile() { return file; } /*****************************************************************************\ * operator = | *****************************************************************************/ URL& URL::operator = (URL* url) { set(url); return *this; } URL& URL::operator = (char* urlString) { set(urlString); return *this; } void URL::setFile(String* file) { this->file = file; extractFName(); } void URL::setFile(char* file) { this->file = new String(file); extractFName(); } void URL::append(String* filename) { // if (filename->charAt(0) == '/') { // setFile(filename); // } // else { // int slashPos = file.lastIndexOf("/"); // if (slashPos != -1) { // file.remove(slashPos); // while (filename->startsWith("../")) { // slashPos = file.lastIndexOf("/"); // if (slashPos != -1) { // file.remove(slashPos); // } // filename->remove(0, 3); // } // while (filename->startsWith("./")) { // filename->remove(0, 2); // } // file.append('/'); // } // else // file = "/"; // file.append(filename); // } // extractFName(); } void URL::append(char* filename) { String fname(filename); append(&fname); } String* URL::toString() { String* str = scheme->plus("://")->plus(host)->plus(file); return str; } /*****************************************************************************\ * hashCode | *****************************************************************************/ int URL::hashCode() { urlString = toString(); return urlString->hashCode(); } String* URL::getDir() { return dir; } String* URL::getFName() { return fname; } String* URL::getExt() { return ext; } void URL::extractFName() { int lastSlash = file->lastIndexOf("/"); int lastDot = file->lastIndexOf("."); if (lastDot > lastSlash) { // We have an extension: ext = file->substring(lastDot); } else { // We don't have an extension: ext = String::emptyString; lastDot = file->length(); } if (lastSlash > -1) { dir = file->substring(0, lastSlash +1); fname = file->substring(lastSlash + 1, lastDot); } else { dir = new String("/"); fname = file; } }