All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.jena.riot.Lang Maven / Gradle / Ivy

There is a newer version: 5.1.0
Show newest version
/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.jena.riot;

import java.util.ArrayList ;
import java.util.Collections ;
import java.util.List ;

import org.apache.jena.atlas.web.ContentType ;

/** A "language" (syntax).
 *  A language has a name, some alternative names,
 *  a content type,  some alternative content types,
 *  and a list of associated file extensions.
 *  Names, content types and file extensions must be unique to one language. 
 *  To create a Lang constant, use {@link LangBuilder} and 
 *  register with {@link RDFLanguages}.
 */
public class Lang 
{
    //  public static final Lang RDFXML = RDFLanguages.RDFXML ; 
    //  public static final Lang NTRIPLES = RDFLanguages.NTriples ; 
    //  public static final Lang N3 = RDFLanguages.N3 ; 
    //  public static final Lang TURTLE = RDFLanguages.Turtle ; 
    //  public static final Lang RDFJSON = RDFLanguages.RDFJSON ; 
    //  public static final Lang NQUADS = RDFLanguages.NQuads ; 
    //  public static final Lang TRIG = RDFLanguages.TriG ; 

    // To avoid an initialization circularity, these are set by RDFLanguages.
    static { RDFLanguages.init() ; }

    /** RDF/XML */
    public static Lang RDFXML ;
    
    /** Turtle*/
    public static Lang TURTLE ;
    
    /** Alternative constant for {@link #TURTLE} */
    public static Lang TTL ;
    
    /** N3 (treat as Turtle) */
    public static Lang N3 ;
    
    /** N-Triples*/
    public static Lang NTRIPLES ;
    
    /** Alternative constant for {@link #NTRIPLES} */
    public static Lang NT ;

    /** JSON-LD. */
    public static Lang JSONLD ;
    
    /** RDF/JSON.  This is not JSON-LD. */
    public static Lang RDFJSON ;
    
    /** TriG */
    public static Lang TRIG ;
    
    /** N-Quads */
    public static Lang NQUADS ;
    
    /** Alternative constant {@link #NQUADS} */
    public static Lang NQ ;

    //** The RDF syntax "RDF Thrift" : see http://jena.apache.org/documentation/io */ 
    public static Lang RDFTHRIFT ;
    
    /** "CSV" - Used in various ways. */
    public static Lang CSV ;

    /** "TSV" - Used in various ways. */
    public static Lang TSV ;
    
    /** TriX */
    public static Lang TRIX ;

    /** The "null" language */
    public static Lang RDFNULL ;

    private final String label ;                    // Primary name
    private final ContentType contentType ;         // Primary content type.
    private final List altLabels ;
    private final List altContentTypes ;
    private final List fileExtensions ;

    // NOT public. This is to force use of the RDFLanguages which makes
    // languages symbols and so == works
    protected Lang(String langlabel, String mainContentType, List altLangLabels,
                   List otherContentTypes, List fileExt) {
        if ( langlabel == null )
            throw new IllegalArgumentException("Null not allowed for language name") ;
        else
            langlabel = langlabel.intern() ;
        label = langlabel ;

        String mediaType = mainContentType ;

        contentType = mediaType == null ? null : ContentType.create(mediaType) ;

        List _altContentTypes = copy(otherContentTypes) ;
        if ( !_altContentTypes.contains(mainContentType) )
            _altContentTypes.add(mainContentType) ;
        altContentTypes = Collections.unmodifiableList(_altContentTypes) ;

        List _altLabels = copy(altLangLabels) ;
        if ( !_altLabels.contains(label) )
            _altLabels.add(label) ;
        altLabels = Collections.unmodifiableList(_altLabels) ;

        List _fileExtensions = copy(fileExt) ;
        fileExtensions = Collections.unmodifiableList(_fileExtensions) ;
    }
    
    static  List copy(List original) {
        List x = new ArrayList<>() ;
        x.addAll(original) ;
        return x ;
    }
    
    @Override
    public int hashCode() { return label.hashCode() ; } 

    @Override
    public boolean equals(Object other) {
        if ( this == other ) return true ;
        if ( other == null ) return false ;
        if ( ! ( other instanceof Lang ) )
            return false ;

        Lang otherLang = (Lang)other ;
        // Just label should be enough.
        return 
            this.label == otherLang.label &&
            this.contentType.equals(otherLang.contentType) &&
            this.altContentTypes.equals(otherLang.altContentTypes) &&
            this.fileExtensions.equals(otherLang.fileExtensions) ;
        // File extensions and alt 
    }

    public String getName()                     { return label ; }
    public ContentType getContentType()         { return contentType ; }
    
    /** As an HTTP Content-Type field value */ 
    public String getHeaderString()             { return contentType.toHeaderString() ; }
    public String getLabel()                    { return label ; }
    public List getAltNames()           { return altLabels ; }
    public List getAltContentTypes()    { return altContentTypes ; }
    public List getFileExtensions()     { return fileExtensions ; }

    @Override
    public String toString()  { return "Lang:"+label ; }
    
    public String toLongString() { 
        String x = "Lang:" + label + " " + getContentType() ;
        if (getAltNames().size() > 0)
            x = x + " " + getAltNames() ;
        if (getAltContentTypes().size() > 0)
            x = x + " " + getAltContentTypes() ;
        if (getFileExtensions().size() > 0)
            x = x + " " + getFileExtensions() ;

        return x ;
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy