public Link parse()

in jspwiki-main/src/main/java/org/apache/wiki/parser/LinkParser.java [193:315]


    public Link parse(final String linktext ) throws ParseException
    {
        if( linktext == null )
        {
            throw new ParseException("null value passed to link parser");
        }

        Link link = null;

        try
        {
            // establish link text and link ref
            final int cut1   = linktext.indexOf('|');
            if( cut1 == -1 )
            {
                //  link form 1:  [Acme]
                return new Link( linktext );
            }

            final int cut2 = cut1+1 < linktext.length()
                    ? linktext.indexOf('|', cut1+1 )
                    : -1 ;

            if ( cut2 == -1 )
            {
                // link form 2:  [Acme | http://www.acme.com/]
                // text = Acme
                final String text = linktext.substring( 0, cut1 ).trim();
                // ref = http://www.acme.com/
                final String ref  = linktext.substring( cut1+1 ).trim();
                return new Link( text, ref );
            }

            // link form 3:  [Acme | http://www.acme.com/ | id='foo' rel='Next']
            final String text    = linktext.substring( 0, cut1 ).trim();
            final String ref     = linktext.substring( cut1+1, cut2 ).trim();
            // attribs = id='foo' rel='Next'
            final String attribs = linktext.substring( cut2+1 ).trim();

            link = new Link( text, ref );

            // parse attributes
            // contains "='" that looks like attrib spec
            if(attribs.contains(EQSQUO))
            {
                try
                {
                    final StringTokenizer tok = new StringTokenizer(attribs,DELIMS,true);
                    while ( tok.hasMoreTokens() )
                    {
                        // get attribute name token
                        String token = tok.nextToken(DELIMS).trim();
                        while ( isSpace(token) && tok.hasMoreTokens() )
                        {
                            // remove all whitespace
                            token = tok.nextToken(DELIMS).trim();
                        }

                        // eat '=', break after '='
                        require( tok, EQ );
                        // eat opening delim
                        require( tok, SQUO );
                        // using existing delim
                        final String value = tok.nextToken(SQUO);
                        // eat closing delim
                        require( tok, SQUO );

                        if( token != null && value != null )
                        {
                            if( Arrays.binarySearch( PERMITTED_ATTRIBUTES, token ) >= 0 )
                            {
                                // _blank _self _parent _top
                                if( !token.equals(TARGET)
                                        || Arrays.binarySearch( PERMITTED_TARGET_VALUES, value ) >= 0 )
                                {
                                    final Attribute a = new Attribute(token,value);
                                    link.addAttribute(a);

                                    if( token.equals(TARGET) )
                                    {
                                        final Attribute rel = new Attribute(REL,NOREFERRER);
                                        link.addAttribute(rel);
                                   }

                                }
                                else
                                {
                                    throw new ParseException("unknown target attribute value='"
                                                             + value + "' on link");
                                }
                            }
                            else
                            {
                                throw new ParseException("unknown attribute name '"
                                                         + token + "' on link");
                            }
                        }
                        else
                        {
                            throw new ParseException("unable to parse link attributes '"
                                                     + attribs + "'");

                        }
                    }
                }
                catch( final ParseException pe )
                {
                    LOG.warn("syntax error parsing link attributes '"+attribs+"': " + pe.getMessage());
                }
                catch( final NoSuchElementException nse )
                {
                    LOG.warn("expected more tokens while parsing link attributes '" + attribs + "'");
                }
            }

        }
        catch( final Exception e )
        {
            LOG.warn( e.getClass().getName() + " thrown by link parser: " + e.getMessage() );
        }

        return link;
    }