Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright: (c) 2004-2011 Mayo Foundation for Medical Education and 
   * Research (MFMER). All rights reserved. MAYO, MAYO CLINIC, and the
   * triple-shield Mayo logo are trademarks and service marks of MFMER.
   *
   * Except as contained in the copyright notice above, or as used to identify 
   * MFMER as the author of this software, the trade names, trademarks, service
   * marks, or product names of the copyright holder shall not be used in
   * advertising, promotion or otherwise in connection with this software without
  * prior written authorization of the copyright holder.
  *
  * Licensed 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.twinkql.context;
 
 import java.util.Set;
 import java.util.UUID;
 
 import  org.springframework.core.io.Resource;
 import  org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 import  org.springframework.util.Assert;
 
A factory for creating TwinkqlContext objects.
 
 public class TwinkqlContextFactory {
 	
 	protected final Log log = LogFactory.getLog(getClass());
 
 	private String mappingFiles = "classpath:twinkql/**/*Map.xml";
 	
 	private String configurationFile = "classpath:twinkql/configuration.xml";
 
 	
 	
The Constructor.
 
 	public TwinkqlContextFactory() {
 		super();
 	}

The Constructor.

Parameters:
queryExecutionProvider the query execution provider
 
 	public TwinkqlContextFactory(QueryExecutionProvider queryExecutionProvider) {
 		this(queryExecutionProvidernull);
 	}

Instantiates a new twinkql context factory.

Parameters:
sparqlEndpointUrl the sparql endpoint url
 
 	public TwinkqlContextFactory(String sparqlEndpointUrl) {
 		this(sparqlEndpointUrlnull);
 	}

Instantiates a new twinkql context factory.

Parameters:
sparqlEndpointUrl the sparql endpoint url
mappingFiles the mapping files
	public TwinkqlContextFactory(String sparqlEndpointUrlString mappingFiles) {
		this(new JenaHttpQueryExecutionProvider(sparqlEndpointUrl), mappingFiles);
	}

The Constructor.

Parameters:
queryExecutionProvider the query execution provider
mappingFiles the mapping files
	public TwinkqlContextFactory(QueryExecutionProvider queryExecutionProvider,
			String mappingFiles) {
		this. = queryExecutionProvider;
		if (StringUtils.isNotBlank(mappingFiles)) {
			this. = mappingFiles;
		}
	}

Gets the twinkql context.

Returns:
the twinkql context
Throws:
Exception the exception
		Assert.notNull(this.,
				"Please provide a 'QueryExecutionProvider'");
		return this.doCreateTwinkqlContext();
	}

Do create twinkql context.

Returns:
the twinkql context
		TwinkqlConfig twinkqlConfig;
		if(this. != null){
			twinkqlConfig = this..build();
else {
			twinkqlConfig = this.loadConfigurationFile();
		}
		context.setTwinkqlConfig(twinkqlConfig);
		return context;
	}
	}

Load mapping files.

Returns:
the iterable
	protected Set<SparqlMaploadMappingFiles() {
		PathMatchingResourcePatternResolver resolver = this.createPathMatchingResourcePatternResolver();
		Set<SparqlMapreturnList = new HashSet<SparqlMap>();
		try {
			for (org.springframework.core.io.Resource resource : resolver
					.getResources(this.)) {
				returnList.add(this.loadSparqlMap(resource));
			}
catch (IOException e) {
				"There was a problem loading the Twinkql Mapping Files specified by the pattern '" + this. +
				".' Set the 'mappingFiles' property to specify a different mapping files location. Exception was: " +
		}
		if(returnList.size() == 0){
					"No mapping files found at: '" + this. +
					".' Set the 'mappingFiles' property to specify a different mapping files location.");
		}
		return returnList;
	}

Load configuration file.

Returns:
the twinkql config
		PathMatchingResourcePatternResolver resolver = this.createPathMatchingResourcePatternResolver();
		Resource configFile = resolver.getResource(this.);
		TwinkqlConfig config = null;
		if(! configFile.exists()){
			this..warn("No Twinql Configuration File specified. Using defaults.");
else {
			config = this.loadTwinkqlConfig(configFile);
		}
		return config;
	}

Creates a new TwinkqlContext object.

Returns:
the path matching resource pattern resolver
	protected PathMatchingResourcePatternResolver createPathMatchingResourcePatternResolver(){
		return new PathMatchingResourcePatternResolver();
	}

Load sparql mappings.

Parameters:
resource the resource
Returns:
the sparql mappings
	protected SparqlMap loadSparqlMap(Resource resource) {
		try {
			String xml = IOUtils.toString(resource.getInputStream());
			SparqlMap map = SparqlMap.unmarshalSparqlMap(new StringReader(this.decorateXml(xml)));
			for(SparqlMapItem item : map.getSparqlMapItem()){
				if(choice == null){
					continue;
				}
				for(SparqlMapChoiceItem choiceItem : choice.getSparqlMapChoiceItem()){
					Select select = choiceItem.getSelect();
						public void addToSelectItem(Object object,
								SelectItem selectItem) {
							selectItem.setIterator((Iterator)object);
						}
						public void setId(Object objectString id) {
							((Iterator)object).setId(id);
						}
					}, Iterator.classselect"<iterator ""</iterator>""{iteratorMarker}");
						public void addToSelectItem(Object object,
								SelectItem selectItem) {
							selectItem.setIsNotNull((IsNotNull)object);
						}
						public void setId(Object objectString id) {
							((IsNotNull)object).setId(id);
						}
					}, IsNotNull.classselect"<isNotNull ""</isNotNull>""{isNotNullMarker}");
						public void addToSelectItem(Object object,
								SelectItem selectItem) {
							selectItem.setTest((Test)object);
						}
						public void setId(Object objectString id) {
							((Test)object).setId(id);
						}
					}, Test.classselect"<test ""</test>""{testMarker}");
				}
			}
			return map;
catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

The Interface AddToSelectItem.

Author(s):
Kevin Peterson
	private interface AddToSelectItem {

Adds the to select item.

Parameters:
object the object
selectItem the select item
		public void addToSelectItem(Object objectSelectItem selectItem);

Sets the id.

Parameters:
object the object
id the id
		public void setId(Object objectString id);
	}

Replace markers.

Parameters:
adder the adder
clazz the clazz
select the select
xmlStart the xml start
xmlEnd the xml end
marker the marker
	private void replaceMarkers(
			Class<?> clazz
			Select select
			String xmlStart
			String xmlEnd
			String marker){
		String content = select.getContent();
		String[] newContents = 
				StringUtils.substringsBetween(contentmarkermarker);
		if(newContents != null){
			for(String newContent : newContents){
				String excapedContent = this.excapeInnerXml(newContent);
				String adjustedContent = xmlStart + excapedContent + xmlEnd;
				Object obj;
				try {
					obj = Unmarshaller.unmarshal(clazznew StringReader(adjustedContent));
catch (Exception e) {
					throw new RuntimeException(e);
				SelectItem newSelectItem = new SelectItem();
				adder.addToSelectItem(objnewSelectItem);
				select.addSelectItem(newSelectItem);
				String uuid = "{"+UUID.randomUUID().toString()+"}";
				content = StringUtils.replaceOnce(contentmarker + newContent + markeruuid);
				adder.setId(objuuid);
			}
			select.setContent(content);
		}
	}

Excape inner xml.

Parameters:
xml the xml
Returns:
the string
	private String excapeInnerXml(String xml){
		String newXml = StringUtils.replace(xml"&""&amp;");
		newXml = StringUtils.replace(newXml"<""&lt;");
		char[] chars = newXml.toCharArray();
		boolean pastFirst = false;
		for(int i=0;i<chars.length;i++){
			if(chars[i] == '>'){
				if(!pastFirst){
					sb.append(chars[i]);
					pastFirst = true;
else {
					sb.append("&gt;");
				}
else {
				sb.append(chars[i]);
			}
		}
		newXml = sb.toString();
		return newXml;
	}

Load twinkql config.

Parameters:
resource the resource
Returns:
the twinkql config
	protected TwinkqlConfig loadTwinkqlConfig(Resource resource) {
		try {
			String xml = IOUtils.toString(resource.getInputStream());
			return TwinkqlConfig.unmarshalTwinkqlConfig(new StringReader(this.decorateXml(xml)));
catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

Decorate xml.

Parameters:
xml the xml
Returns:
the string
	protected String decorateXml(String xml){
		xml = xml.replaceAll("<iterator""{iteratorMarker}");
		xml = xml.replaceAll("</iterator>""{iteratorMarker}");
		xml = xml.replaceAll("<isNotNull""{isNotNullMarker}");
		xml = xml.replaceAll("</isNotNull>""{isNotNullMarker}");
		xml = xml.replaceAll("<test""{testMarker}");
		xml = xml.replaceAll("</test>""{testMarker}");
		return xml;
	}

Gets the mapping files.

Returns:
the mapping files
	public String getMappingFiles() {
		return ;
	}

Gets the query execution provider.

Returns:
the query execution provider
	}

Sets the query execution provider.

Parameters:
queryExecutionProvider the new query execution provider
			QueryExecutionProvider queryExecutionProvider) {
		this. = queryExecutionProvider;
	}

Sets the mapping files.

Parameters:
mappingFiles the new mapping files
	public void setMappingFiles(String mappingFiles) {
		this. = mappingFiles;
	}

Gets the configuration file.

Returns:
the configuration file
	}

Sets the configuration file.

Parameters:
configurationFile the new configuration file
	public void setConfigurationFile(String configurationFile) {
		this. = configurationFile;
	}
	}
	public void setInstantiators(Set<Instantiatorinstantiators) {
		this. = instantiators;
	}
	}
	public void setConfigBuilder(ConfigBuilder configBuilder) {
		this. = configBuilder;
	}
New to GrepCode? Check out our FAQ X