Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
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.marmotta.platform.core.rio;
 
 
 import java.io.*;
 import java.util.*;

RDF to HTML Writer

Author(s):
Sebastian Schaffert
Sergio Fernández
 
 public class RDFHtmlWriterImpl implements RDFHtmlWriter {
 
     protected final String TEMPLATE = "rdfhtml.ftl";
 
 
     protected PrefixService prefixService;
     
     protected TemplatingService templatingService;
 
     protected Logger log = LoggerFactory.getLogger(RDFHtmlWriterImpl.class);
 
     protected PrintWriter writer;
     
     protected WriterConfig config;
 
     protected Map<ResourceSortedSet<Statement>> tripleMap = new HashMap<ResourceSortedSet<Statement>>();
 
     protected Map<StringStringnamespaceMap = new HashMap<StringString>();
 
     public RDFHtmlWriterImpl(OutputStream out) {
         this(new OutputStreamWriter(out));
     }
 
     public RDFHtmlWriterImpl(Writer writer) {
         this(new PrintWriter(writer));
     }
 
     public RDFHtmlWriterImpl(PrintWriter writer) {
         super();
         this. = writer;
 
         // FIXME: usage of KiWiContext is not recommened!
          = CDIContext
                 .getInstance(ConfigurationService.class);
          = CDIContext.getInstance(PrefixService.class);
          = CDIContext.getInstance(TemplatingService.class);
     }

    
Gets the RDF format that this RDFWriter uses.
 
     @Override
     public RDFFormat getRDFFormat() {
         return .;
     }

    
Signals the start of the RDF data. This method is called before any data is reported.

Throws:
org.openrdf.rio.RDFHandlerException If the RDF handler has encountered an unrecoverable error.
    @Override
    public void startRDF() throws RDFHandlerException {
    }

    
Signals the end of the RDF data. This method is called when all data has been reported.

Throws:
org.openrdf.rio.RDFHandlerException If the RDF handler has encountered an unrecoverable error.
    @Override
    public void endRDF() throws RDFHandlerException {
        List<Map<StringObject>> resources = new ArrayList<Map<StringObject>>();
        for (Map.Entry<ResourceSortedSet<Statement>> entry : .entrySet()) {
            SortedSet<Statementts = entry.getValue();
            Map<StringObjectresource = new HashMap<StringObject>();
            String subject = ts.first().getSubject().stringValue();
            if (UriUtil.validate(subject)) {
            	resource.put("uri"subject);
	            try {
	                resource.put("encoded_uri", URLEncoder.encode(subject"UTF-8"));
	            } catch (UnsupportedEncodingException e) {
	                .error("Error trying to encode '{}': {}"subjecte.getMessage());
	                resource.put("encoded_uri"subject);
	            }
            } else {
            	resource.put("genid"subject);
	            try {
	                resource.put("encoded_genid", URLEncoder.encode(subject"UTF-8"));
	            } catch (UnsupportedEncodingException e) {
	                .error("Error trying to encode '{}': {}"subjecte.getMessage());
	                resource.put("encoded_genid"subject);
	            }
            }
            List<Map<StringObject>> triples = new ArrayList<Map<StringObject>>();
            for (Statement t : ts) {
                Map<StringObjecttriple = new HashMap<StringObject>();
                //predicate
                Map<StringStringpredicate = new HashMap<StringString>();
                String predicateUri = t.getPredicate().stringValue();
                predicate.put("uri"predicateUri);
                String predicateCurie = .getCurie(predicateUri);
                predicate.put("curie", StringUtils.isNotBlank(predicateCurie) ? predicateCurie : predicateUri);
                triple.put("predicate"predicate);
                predicate = null;
                //object
                Map<StringStringobject = new HashMap<StringString>();
                Value value = t.getObject();
                String objectValue = value.stringValue();
                if (value instanceof URI) { //http uri
                    object.put("uri"objectValue);
                    String objectCurie = .getCurie(objectValue);
                    object.put("curie", StringUtils.isNotBlank(objectCurie) ? objectCurie : objectValue);
                    object.put("cache""true");
                } else if (value instanceof BNode) { //blank node
                    object.put("genid"objectValue);
                    try {
                    	object.put("encoded_genid", URLEncoder.encode(objectValue"UTF-8"));
                    } catch (UnsupportedEncodingException e) {
                        .error("Error trying to encode '{}': {}"subjecte.getMessage());
                        object.put("encoded_genid"objectValue);
                    }                 
                } else if (value instanceof Literal) { //literal
                    Literal literal = (Literalt.getObject();
                    String lang = literal.getLanguage();
                    if (StringUtils.isNotBlank(lang)) {
                        object.put("lang"lang);
                        objectValue = "\"" + objectValue + "\"@" + lang;
                        if (literal.getDatatype() != null) {
                            String datatype = .getCurie(literal.getDatatype().stringValue());
                            object.put("datatype"datatype);
                            objectValue += "^^" + datatype;
                        }
                    } else {
                        if (literal.getDatatype() != null) {
                            String datatype = .getCurie(literal.getDatatype().stringValue());
                            object.put("datatype"datatype);
                            objectValue = "\"" + objectValue + "\"^^"  + datatype;
                        }
                    }
                    object.put("value"objectValue);
                } else { //should not arrive here...
                    object.put("value"objectValue);
                }
                triple.put("object"object);
                object = null;
                if(t.getContext() != null) {
                    Map<StringStringcontext = new HashMap<StringString>();
                    String contextUri = t.getContext().stringValue();
                    context.put("uri"contextUri);
                    String contextCurie = .getCurie(contextUri);
                    context.put("curie", StringUtils.isNotBlank(contextCurie) ? contextCurie : contextUri);
                    triple.put("context"context);
                    context = null;
                } else {
                    triple.put("context", ImmutableMap.of("uri","","curie",""));
                }
                if (ResourceUtils.isInferred(t)) {
                    triple.put("info", ResourceUtils.getId(t));
                } else {
                    triple.put("info""-");
                }
                triples.add(triple);
            }
            resource.put("triples"triples);
            resources.add(resource);
        }
        try {
            Map<StringObjectdata = new HashMap<StringObject>();
            data.put("baseUri".getServerUri());
            data.put("resources"resources);
            data.put("prefixMappings".serializePrefixMapping());
            //set timemap link
            if(.getBooleanConfiguration("versioning.enabled")) {
                data.put("timemaplink".getStringConfiguration("versioning.memento.timemap"));
            }
            .process(data);
        } catch (Exception e) {
            .error(e.getMessage(), e);
            throw new RDFHandlerException(e);
        }
    }

    
Handles a namespace declaration/definition. A namespace declaration associates a (short) prefix string with the namespace's URI. The prefix for default namespaces, which do not have an associated prefix, are represented as empty strings.

Parameters:
prefix The prefix for the namespace, or an empty string in case of a default namespace.
uri The URI that the prefix maps to.
Throws:
org.openrdf.rio.RDFHandlerException If the RDF handler has encountered an unrecoverable error.
    @Override
    public void handleNamespace(String prefixString uri)
            throws RDFHandlerException {
        .put(uriprefix);
    }

    
Handles a statement.

Parameters:
st The statement.
Throws:
org.openrdf.rio.RDFHandlerException If the RDF handler has encountered an unrecoverable error.
    @Override
    public void handleStatement(Statement stthrows RDFHandlerException {
        if (.containsKey(st.getSubject())) {
            SortedSet<Statementts = .get(st.getSubject());
            ts.add(st);
        } else {
            SortedSet<Statementts = new TreeSet<Statement>(
                    new Comparator<Statement>() {
                        @Override
                        public int compare(Statement o1Statement o2) {
                            if (o1.getPredicate().toString()
                                    .equals(o2.getPredicate().toString()))
                                return -1; // FIXME: jfrank: why this?
                            return o1.getPredicate().toString()
                                    .compareTo(o2.getPredicate().toString());
                        }
                    });
            ts.add(st);
            .put(st.getSubject(), ts);
        }
    }

    
Handles a comment.

Parameters:
comment The comment.
Throws:
org.openrdf.rio.RDFHandlerException If the RDF handler has encountered an unrecoverable error.
    @Override
    public void handleComment(String commentthrows RDFHandlerException {
        // To change body of implemented methods use File | Settings | File
        // Templates.
    }
    private String createInfo(long id) { // TODO: move to freemarker too
        StringBuilder sb = new StringBuilder();
        String closer = "<button style='position:absolute;top:5px;right:5px;z-index:2;' onclick='document.getElementById(\"info"
                + id + "\").style.display = \"none\"'>X</button>";
        String style = "border:1px solid black; width:50%; position:absolute;top:100px;left:25%;background-color:white;z-index:2;display:none;padding-top:10px;min-height:100px;overflow:auto;";
        String iframe = "<iframe id='iframe" + id
                + "' src='' style='border:none;width:100%'></iframe>";
        sb.append("<a href='#' onclick='document.getElementById(\"iframe")
                .append(id).append("\").src=\"")
                .append(.getServerUri())
                .append("core/public/html/reasoning.html#").append(id)
                .append("\";document.getElementById(\"info").append(id)
                .append("\").style.display = \"block\";'>info</a>");
        sb.append("<div style='").append(style).append("' id='info").append(id)
                .append("'>");
        sb.append(iframe).append(closer);
        sb.append("</div>");
        return sb.toString();
    }
    @Override
    public RDFWriterPriority getPriority() {
        return .;
    }
    
    

Returns:
A collection of org.openrdf.rio.RioSettings that are supported by this RDFWriter.
Since:
2.7.0
		return new ArrayList<RioSetting<?>>();
	}

    
Retrieves the current writer configuration as a single object.

Returns:
a writer configuration object representing the current configuration of the writer.
Since:
2.7.0
		return ;
	}

    
Sets all supplied writer configuration options.

Parameters:
config a writer configuration object.
Since:
2.7.0
	public void setWriterConfig(WriterConfig config) {
		this. = config;
	}    
New to GrepCode? Check out our FAQ X