package votorola.a.position; // Copyright 2013, Michael Allan. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Votorola Software"), to deal in the Votorola Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicence, and/or sell copies of the Votorola Software, and to permit persons to whom the Votorola Software is furnished to do so, subject to the following conditions: The preceding copyright notice and this permission notice shall be included in all copies or substantial portions of the Votorola Software. THE VOTOROLA SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE VOTOROLA SOFTWARE OR THE USE OR OTHER DEALINGS IN THE VOTOROLA SOFTWARE. import java.io.*; import java.net.*; import java.util.regex.*; import javax.mail.internet.*; import votorola.a.*; import votorola.a.voter.*; import votorola.g.*; import votorola.g.lang.*; /** An identifier of a position, including a paired identifier for the person. This is * factored out as a subclass in order to preserve GWT compatibility in the base class. */ public @ThreadSafe final class PositionIDPair extends PositionID { private PositionIDPair( final IDPair person, String _pollName ) { super( person.username(), _pollName ); this.person = person; } /** Constructs a PositionIDPair. * * @param pageName the name of the position page. It will automatically be * normalized if necessary. * @param content0 the wikitext of the position page including at least section 0. * @param rev the revision identifier of the content. */ static PositionIDPair newID( final PollwikiVS wiki, final String pageName, final int rev, final String content0 ) throws MalformedPageName, PipeRevision.MalformedContent { final MatchResult mR = MediaWiki.parsePageNameS( pageName ); if( mR == null ) throw new IllegalArgumentException( "malformed page name:" + pageName ); final String username = mR.group( 2 ); final String subpageName = mR.group( 3 ); final String pollName; if( subpageName == null ) { if( !wiki.pipeRecognizer().isPipeName( username )) { throw new MalformedPageName( "page is not a pipe and should therefore be a subpage, but is not: " + pageName, wiki.scriptURI(), rev ); } final Matcher m = PipeRevision.TEMPLATE_CALL_PATTERN.matcher( content0 ); if( !m.find() ) { throw new PipeRevision.MalformedContent( "missing or malformed pipe template call", wiki.scriptURI(), rev ); } pollName = m.group( 1 ); } else pollName = subpageName; final IDPair person; try{ person = IDPair.fromUsername( username ); } catch( final AddressException x ) { throw new MalformedPageName( "username is not mailish: " + username, x, wiki.scriptURI(), rev ); } return new PositionIDPair( person, pollName ); } // ------------------------------------------------------------------------------------ /** The identifier of the person. */ public IDPair person() { return person; } private final IDPair person; // ==================================================================================== /** Thrown when a request cannot be met because a page name is not in the form * required for a position page. */ static final @ThreadSafe class MalformedPageName extends IOException implements UserInformative { /** @param wikiScriptURI the base URL for script execution in the wiki, without a * trailing slash (/). */ MalformedPageName( final String message, Throwable _cause, final URI wikiScriptURI, final int rev ) { super( message + " | " + MediaWiki.revLoc(wikiScriptURI,rev), _cause ); } /** @param wikiScriptURI the base URL for script execution in the wiki, without a * trailing slash (/). */ MalformedPageName( final String message, final URI wikiScriptURI, final int rev ) { super( message + " | " + MediaWiki.revLoc(wikiScriptURI,rev) ); } } }