示例#1
0
文件: URL.cs 项目: sailesh341/JavApi
        /**
         * Creates a new URL to the specified resource {@code spec}. This URL is
         * relative to the given {@code context}. The {@code handler} will be used
         * to parse the URL string representation. If this argument is {@code null}
         * the default {@code URLStreamHandler} will be used. If the protocol of the
         * parsed URL does not match with the protocol of the context URL, then the
         * newly created URL is absolute and bases only on the given URL represented
         * by {@code spec}. Otherwise the protocol is defined by the context URL.
         *
         * @param context
         *            the URL which is used as the context.
         * @param spec
         *            the URL string representation which has to be parsed.
         * @param handler
         *            the specific stream handler to be used by this URL.
         * @throws MalformedURLException
         *             if the given string {@code spec} could not be parsed as a URL
         *             or an invalid protocol has been found.
         */
        public URL(URL context, String spec, URLStreamHandler handler)
        {
            //throws MalformedURLException {
            if (handler != null)
            {
                java.lang.SecurityManager sm = java.lang.SystemJ.getSecurityManager();
                if (sm != null)
                {
                    sm.checkPermission(specifyStreamHandlerPermission);
                }
                strmHandler = handler;
            }

            if (spec == null)
            {
                throw new MalformedURLException();
            }
            spec = spec.trim();

            // The spec includes a protocol if it includes a colon character
            // before the first occurrence of a slash character. Note that,
            // "protocol" is the field which holds this URLs protocol.
            int index;
            try
            {
                index = spec.indexOf(':');
            }
            catch (java.lang.NullPointerException e)
            {
                throw new MalformedURLException(e.toString());
            }
            int startIPv6Addr = spec.indexOf('[');
            if (index >= 0)
            {
                if ((startIPv6Addr == -1) || (index < startIPv6Addr))
                {
                    protocol = spec.substring(0, index);
                    // According to RFC 2396 scheme part should match
                    // the following expression:
                    // alpha *( alpha | digit | "+" | "-" | "." )
                    char c = protocol.charAt(0);
                    bool valid = ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z');
                    for (int i = 1; valid && (i < protocol.length()); i++)
                    {
                        c = protocol.charAt(i);
                        valid = ('a' <= c && c <= 'z') ||
                                ('A' <= c && c <= 'Z') ||
                                ('0' <= c && c <= '9') ||
                                (c == '+') ||
                                (c == '-') ||
                                (c == '.');
                    }
                    if (!valid)
                    {
                        protocol = null;
                        index = -1;
                    }
                    else
                    {
                        // Ignore case in protocol names.
                        // Scheme is defined by ASCII characters.
                        protocol = Util.toASCIILowerCase(protocol);
                    }
                }
            }

            if (protocol != null)
            {
                // If the context was specified, and it had the same protocol
                // as the spec, then fill in the receiver's slots from the values
                // in the context but still allow them to be over-ridden later
                // by the values in the spec.
                if (context != null && protocol.equals(context.getProtocol()))
                {
                    String cPath = context.getPath();
                    if (cPath != null && cPath.startsWith("/"))
                    { //$NON-NLS-1$
                        set(protocol, context.getHost(), context.getPort(), context
                                .getAuthority(), context.getUserInfo(), cPath,
                                context.getQuery(), null);
                    }
                    if (strmHandler == null)
                    {
                        strmHandler = context.strmHandler;
                    }
                }
            }
            else
            {
                // If the spec did not include a protocol, then the context
                // *must* be specified. Fill in the receiver's slots from the
                // values in the context, but still allow them to be over-ridden
                // by the values in the ("relative") spec.
                if (context == null)
                {
                    throw new MalformedURLException("Protocol not found: " + spec); //$NON-NLS-1$
                }
                set(context.getProtocol(), context.getHost(), context.getPort(),
                        context.getAuthority(), context.getUserInfo(), context
                                .getPath(), context.getQuery(), null);
                if (strmHandler == null)
                {
                    strmHandler = context.strmHandler;
                }
            }

            // If the stream handler has not been determined, set it
            // to the default for the specified protocol.
            if (strmHandler == null)
            {
                setupStreamHandler();
                if (strmHandler == null)
                {
                    throw new MalformedURLException("Unknown protocol: " + protocol); //$NON-NLS-1$
                }
            }

            // Let the handler parse the URL. If the handler throws
            // any exception, throw MalformedURLException instead.
            //
            // Note: We want "index" to be the index of the start of the scheme
            // specific part of the URL. At this point, it will be either
            // -1 or the index of the colon after the protocol, so we
            // increment it to point at either character 0 or the character
            // after the colon.
            try
            {
                strmHandler.parseURL(this, spec, ++index, spec.length());
            }
            catch (Exception e)
            {
                throw new MalformedURLException(e.toString());
            }

            if (port < -1)
            {
                throw new MalformedURLException("Port out of range: " + port); //$NON-NLS-1$
            }
        }
示例#2
0
        public void set(String uri)
        {
            clear();

            uri = uri.trim();
            if (uri.Length == 0)
                return;

            int iColon = uri.indexOf(':');
            if (iColon >= 0) {
                // Make sure the colon came before a '/'.
                int iFirstSlash = uri.indexOf('/');
                if (iFirstSlash < 0 || iColon < iFirstSlash)
                    // Omit the leading protocol such as ndn:
                    uri = uri.Substring(iColon + 1).trim();
            }

            // Trim the leading slash and possibly the authority.
            if (uri[0] == '/') {
                if (uri.Length >= 2 && uri[1] == '/') {
                    // Strip the authority following "//".
                    int iAfterAuthority = uri.indexOf('/', 2);
                    if (iAfterAuthority < 0)
                        // Unusual case: there was only an authority.
                        return;
                    else
                        uri = uri.Substring(iAfterAuthority + 1).trim();
                } else
                    uri = uri.Substring(1).trim();
            }

            int iComponentStart = 0;

            // Unescape the components.
            String sha256digestPrefix = "sha256digest=";
            while (iComponentStart < uri.Length) {
                int iComponentEnd = ILOG.J2CsMapping.Util.StringUtil.IndexOf(uri,"/",iComponentStart);
                if (iComponentEnd < 0)
                    iComponentEnd = uri.Length;

                Name.Component  component;
                if (sha256digestPrefix.regionMatches(0, uri, iComponentStart,
                        sha256digestPrefix.Length)) {
                    try {
                        component = net.named_data.jndn.Name.Component.fromImplicitSha256Digest(fromHex(uri,
                                iComponentStart + sha256digestPrefix.Length,
                                iComponentEnd));
                    } catch (EncodingException ex) {
                        throw new Exception(ex.Message);
                    }
                } else
                    component = new Name.Component (fromEscapedString(uri,
                            iComponentStart, iComponentEnd));

                // Ignore illegal components.  This also gets rid of a trailing '/'.
                if (!component.getValue().isNull())
                    append(component);

                iComponentStart = iComponentEnd + 1;
            }
        }
示例#3
0
        /**
         * Returns the string representing this permission's actions. It must be of
         * the form "read,write,execute,delete", all lower case and in the correct
         * order if there is more than one action.
         *
         * @param action
         *            the action name
         * @return the string representing this permission's actions
         */
        private String toCanonicalActionString(String action)
        {
            actions = action.trim().toLowerCase();

            // get the numerical representation of the action list
            mask = getMask(actions);

            // convert the mask to a canonical action list.
            int len = actionList.Length;
            // the test mask - shift the 1 to the leftmost position of the
            // actionList
            int highestBitMask = 1 << (len - 1);

            // if a bit of mask is set, append the corresponding action to result
            java.lang.StringBuilder result = new java.lang.StringBuilder();
            bool addedItem = false;
            for (int i = 0; i < len; i++) {
            if ((highestBitMask & mask) != 0) {
                if (addedItem) {
                    result.append(","); //$NON-NLS-1$
                }
                result.append(actionList[i]);
                addedItem = true;
            }
            highestBitMask = highestBitMask >> 1;
            }
            return result.toString();
        }
示例#4
0
        /**
         * Creates a {@code Timestamp} object with a time value equal to the time
         * specified by a supplied String holding the time in JDBC timestamp escape
         * format, which is {@code "yyyy-mm-dd hh:mm:ss.nnnnnnnnn}"
         *
         * @param s
         *            the {@code String} containing a time in JDBC timestamp escape
         *            format.
         * @return A {@code Timestamp} object with time value as defined by the
         *         supplied {@code String}.
         * @throws IllegalArgumentException
         *             if the provided string is {@code null}.
         */
        public static Timestamp valueOf(String s)
        {
            if (s == null)
            {
                // sql.3=Argument cannot be null
                throw new java.lang.IllegalArgumentException("Argument cannot be null"); //$NON-NLS-1$
            }

            // omit trailing whitespaces
            s = s.trim();
            if (!java.util.regex.Pattern.matches(TIME_FORMAT_REGEX, (java.lang.CharSequence)new java.lang.StringJ(s)))
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            java.text.SimpleDateFormat df = new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); //$NON-NLS-1$
            java.text.ParsePosition pp = new java.text.ParsePosition(0);

            /*
             * First parse out the yyyy-MM-dd HH:mm:ss component of the String into
             * a Date object using the SimpleDateFormat. This should stop after the
             * seconds value, according to the definition of SimpleDateFormat.parse,
             * with the ParsePosition indicating the index of the "." which should
             * precede the nanoseconds value
             */
            java.util.Date theDate;
            try
            {
                theDate = df.parse(s, pp);
            }
            catch (Exception e)
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            if (theDate == null)
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            /*
             * If we get here, the Date part of the string was OK - now for the
             * nanoseconds value. Strictly, this requires the remaining part of the
             * String to look like ".nnnnnnnnn". However, we accept anything with a
             * '.' followed by 1 to 9 digits - we also accept nothing (no fractions
             * of a second). Anything else is interpreted as incorrect format which
             * will generate an IllegalArgumentException
             */
            int position = pp.getIndex();
            int remaining = s.length() - position;
            int theNanos;

            if (remaining == 0)
            {
                // First, allow for the case where no fraction of a second is given:
                theNanos = 0;
            }
            else
            {
                /*
                 * Case where fraction of a second is specified: Require 1 character
                 * plus the "." in the remaining part of the string...
                 */
                if ((s.length() - position) < ".n".length())
                { //$NON-NLS-1$
                    throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
                }

                /*
                 * If we're strict, we should not allow any EXTRA characters after
                 * the 9 digits
                 */
                if ((s.length() - position) > ".nnnnnnnnn".length())
                { //$NON-NLS-1$
                    throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
                }

                // Require the next character to be a "."
                if (s.charAt(position) != '.')
                {
                    // sql.4=Bad input string format: expected '.' not {0}
                    throw new java.lang.NumberFormatException("Bad input string format: expected '.' not " + s.charAt(position)); //$NON-NLS-1$
                }
                // Get the length of the number string - need to account for the '.'
                int nanoLength = s.length() - position - 1;

                // Get the 9 characters following the "." as an integer
                String theNanoString = s.substring(position + 1, position + 1
                        + nanoLength);
                /*
                 * We must adjust for the cases where the nanos String was not 9
                 * characters long by padding out with zeros
                 */
                theNanoString = theNanoString + "000000000"; //$NON-NLS-1$
                theNanoString = theNanoString.substring(0, 9);

                try
                {
                    theNanos = java.lang.Integer.parseInt(theNanoString);
                }
                catch (Exception e)
                {
                    // If we get here, the string was not a number
                    throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
                }
            }

            if (theNanos < 0 || theNanos > 999999999)
            {
                throw new java.lang.IllegalArgumentException("Timestamp format must be yyyy-mm-dd hh:mm:ss.fffffffff"); //$NON-NLS-1$
            }

            Timestamp theTimestamp = new Timestamp(theDate.getTime());
            theTimestamp.setNanos(theNanos);

            return theTimestamp;
        }
示例#5
0
 private void newDelegateInstance(String language, String country)
 {
     if (language == null || language.trim().length() == 0)
     {
         this.delegateInstance = CultureInfo.InvariantCulture;
     }
     else
     {
         this.delegateInstance = new CultureInfo(language + (null == country ? "" : "-" + country));
     }
 }