/* * (c) Copyright 2009 Hewlett-Packard Development Company, LP * All rights reserved. * [See endLog of file] */ package riot.io; import java.util.ArrayList ; import java.util.HashMap ; import java.util.Iterator ; import java.util.List ; import java.util.Map ; import java.util.NoSuchElementException ; import org.openjena.atlas.logging.Log ; import org.openjena.riot.tokens.PrintTokenizer ; import org.openjena.riot.tokens.Token ; import static org.openjena.riot.tokens.TokenType.* ; import org.openjena.riot.tokens.Tokenizer ; import org.slf4j.Logger ; import org.slf4j.LoggerFactory ; import riot.comms.CommsException ; /** Tokenizer that sorts out prefixes and groups into sequences of token */ public class TokenInputStreamBase implements TokenInputStream { private static Logger log = LoggerFactory.getLogger(TokenInputStreamBase.class) ; private boolean finished = false ; private final Tokenizer tokens ; private List list ; private Map map = new HashMap() ; private String label ; public TokenInputStreamBase(String label, Tokenizer tokens) { if ( false ) tokens = new PrintTokenizer("InputStream: ", tokens) ; this.tokens = tokens ; this.label = label ; } @Override public boolean hasNext() { if ( finished ) return false ; if ( list != null ) // Already got the reply. return true ; try { if ( ! tokens.hasNext() ) { finished = true ; return false ; } list = buildOneLine() ; if ( false && log.isDebugEnabled() ) log.debug("Tokens: "+list) ; if ( list == null ) finished = true ; return list != null ; } catch (Exception ex) { finished = true ; return false ; } } private List buildOneLine() { List tuple = new ArrayList() ; boolean isDirective = false ; for( ; tokens.hasNext() ; ) { Token token = tokens.next() ; if ( token.hasType(DIRECTIVE) ) isDirective = true ; if ( token.hasType(DOT) ) { if ( tuple.size() > 0 && tuple.get(0).hasType(DIRECTIVE)) { directive(tuple) ; tuple.clear(); isDirective = false ; // Start again. continue ; } return tuple ; } // Fixup prefix names. if ( !isDirective && token.hasType(PREFIXED_NAME) ) { String ns = map.get(token.getImage()) ; String iri ; if ( ns == null) { Log.warn("Can'transformTokenToTuple resolve '"+token.toString(false)+"'", ns) ; iri = "unresolved:"+token.getImage()+":"+token.getImage2() ; } else iri = ns+token.getImage2() ; token.setType(IRI) ; token.setImage(iri) ; token.setImage2(null) ; } tuple.add(token) ; } // No final DOT return tuple ; } private void directive(List tuple) { if ( tuple.size() != 3 ) throw new CommsException("Bad directive: "+tuple) ; String x = tuple.get(0).getImage() ; if ( x.equals("prefix") ) { // Raw - unresolved prefix name. if ( ! tuple.get(1).hasType(PREFIXED_NAME) ) throw new CommsException("@prefix requires a prefix (found '"+tuple.get(1)+"')") ; if ( tuple.get(1).getImage2().length() != 0 ) throw new CommsException("@prefix requires a prefix and no suffix (found '"+tuple.get(1)+"')") ; String prefix = tuple.get(1).getImage() ; if ( ! tuple.get(2).hasType(IRI) ) throw new CommsException("@prefix requires an IRI (found '"+tuple.get(1)+"')") ; String iriStr = tuple.get(2).getImage() ; map.put(prefix, iriStr) ; return ; } throw new CommsException("Unregcognized directive: "+x) ; } @Override public List next() { if ( ! hasNext() ) throw new NoSuchElementException() ; List r = list ; if ( log.isDebugEnabled() ) { if ( label != null ) log.debug("<< "+label+": "+r) ; else log.debug("<< "+r.toString()) ; } list = null ; return r ; } @Override public void remove() { throw new UnsupportedOperationException() ; } @Override public Iterator> iterator() { return this ; } @Override public void close() {} } /* * (c) Copyright 2009 Hewlett-Packard Development Company, LP * All rights reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products * derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */