001package 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. 002 003import java.io.*; 004import java.util.regex.*; 005import javax.mail.internet.*; 006import votorola.a.*; 007import votorola.a.voter.*; 008import votorola.g.*; 009import votorola.g.lang.*; 010 011 012/** A particular revision of a component pipe. 013 * 014 * @see <a href='http://reluk.ca/w/Category:Component_pipe' target='_top' 015 * >Category:Component pipe</a> 016 */ 017public @ThreadSafe abstract class ComponentPipeRevision extends PageRevision1 implements PipeRevision 018{ 019 020 021 /** Constructs a ComponentPipeRevision. 022 */ 023 ComponentPipeRevision( final PollwikiVS wiki, int _pageID, String _pageName, int _rev, 024 int _revLatest, final String nomineeName, final String personName, String _pollName ) 025 throws MalformedContent 026 { 027 super( wiki.scriptURI(), _pageID, _pageName, _rev, _revLatest, wiki.uri().toASCIIString(), 028 wiki.maybeUgly() ); 029 if( "Anonymous".equals( nomineeName )) 030 { 031 throw new MalformedContent( "anonymous nominee disallowed", wiki, rev() ); 032 // unlike office pipe; no point allowing here till its clear what kind of 033 // behaviour would be expected 034 } 035 036 try{ nominee = IDPair.fromUsername( nomineeName ); } 037 catch( final AddressException x ) 038 { 039 throw new MalformedContent( "nominee username is not mailish: " + nomineeName, x, 040 wiki, rev() ); 041 } 042 043 try{ person = IDPair.fromUsername( personName ); } 044 catch( final AddressException x ) 045 { 046 throw new MalformedContent( "pipe name is not mailish: " + personName, x, wiki, rev() ); 047 } 048 049 pollName = _pollName; 050 } 051 052 053 054 /** Constructs a ComponentPipeRevision as a copy of another. 055 */ 056 ComponentPipeRevision( final ComponentPipeRevision rCP ) 057 { 058 super( rCP ); 059 nominee = rCP.nominee; 060 person = rCP.person; 061 pollName = rCP.pollName; 062 } 063 064 065 066 // ------------------------------------------------------------------------------------ 067 068 069 /** The search pattern for the definitive template call in a component pipe page. If 070 * the pattern matches, then it splits the wikitext of the call into groups based on 071 * the template parameter values (1) nominee username and (2) poll name. This method 072 * of extracting properties from a revision is required because SemanticMediawiki's 073 * data store does not attach properties to revisions, only to pages. 074 * 075 * @see <a href='http://reluk.ca/w/Category:Component_pipe' target='_top' 076 * >Category:Component_pipe</a> 077 * @see PipeRevision#TEMPLATE_CALL_PATTERN 078 */ 079 static final Pattern TEMPLATE_CALL_PATTERN = Pattern.compile( 080 "\\{\\s*\\{\\s*[Cc]omponent pipe\\s*" 081 + "(?:\\|\\s*.+?\\s*=\\s*.+?\\s*)*" // * 082 + "\\|\\s*nominee\\s*=\\s*(.+?)\\s*" // NOMINEE USERNAME 083 + "(?:\\|\\s*.+?\\s*=\\s*.+?\\s*)*" // * 084 + "\\|\\s*poll\\s*=\\s*(.+?)\\s*" // POLL NAME 085 + "(?:\\|\\s*.+?\\s*=\\s*.+?\\s*)*" // * 086 + "\\}\\s*\\}" ); // * any other parameters are currently ignored 087 088 089 090 // - C o m p o n e n t - P i p e - R e v i s i o n ------------------------------------ 091 092 093 /** The nominee drafter. 094 * 095 * @see <a href='http://reluk.ca/w/Property:Nominee' target='_top' 096 * >Property:Nominee</a> 097 */ 098 public final IDPair nominee() { return nominee; } 099 100 101 private final IDPair nominee; 102 103 104 105 // - P o s i t i o n a l - R e v i s i o n -------------------------------------------- 106 107 108 public final IDPair person() { return person; } 109 110 111 private final IDPair person; 112 113 114 115 public final String pollName() { return pollName; } 116 117 118 private final String pollName; 119 120 121 122 // ==================================================================================== 123 124 125 /** Thrown when a request cannot be met because the content of a page is not in the 126 * form required for a component pipe. 127 */ 128 static @ThreadSafe final class MalformedContent extends IOException implements UserInformative 129 { 130 131 MalformedContent( final String message, final PollwikiVS wiki, final int rev ) 132 { 133 super( message + " | " + MediaWiki.revLoc(wiki.scriptURI(),rev) ); 134 } 135 136 MalformedContent( final String message, Throwable _cause, final PollwikiVS wiki, 137 final int rev ) 138 { 139 super( message + " | " + MediaWiki.revLoc(wiki.scriptURI(),rev), _cause ); 140 } 141 142 } 143 144 145}