diff -r f1112f777ce9 -r 96906a986c3b testdev/ite/src/com.nokia.testfw.codegen.ui/src/com/nokia/testfw/codegen/ui/templates/EclipseTemplateBuilder.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/testdev/ite/src/com.nokia.testfw.codegen.ui/src/com/nokia/testfw/codegen/ui/templates/EclipseTemplateBuilder.java Tue Mar 30 14:39:29 2010 +0800 @@ -0,0 +1,116 @@ +/* +* Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). +* All rights reserved. +* This component and the accompanying materials are made available +* under the terms of "Eclipse Public License v1.0" +* which accompanies this distribution, and is available +* at the URL "http://www.eclipse.org/legal/epl-v10.html". +* +* Initial Contributors: +* Nokia Corporation - initial contribution. +* +* Contributors: +* +* Description: +* +*/ + +package com.nokia.testfw.codegen.ui.templates; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.eclipse.core.internal.content.ContentTypeManager; +import org.eclipse.core.runtime.IStatus; +import org.eclipse.core.runtime.Status; +import org.eclipse.core.runtime.content.IContentType; +import org.eclipse.jface.text.templates.Template; +import org.eclipse.jface.text.templates.persistence.TemplatePersistenceData; +import org.eclipse.jface.text.templates.persistence.TemplateStore; + +import com.nokia.testfw.codegen.templates.TemplateBuilder; +import com.nokia.testfw.codegen.templates.TemplateLoader; +import com.nokia.testfw.codegen.ui.CodegenUIPlugin; + +@SuppressWarnings("restriction") +public class EclipseTemplateBuilder implements TemplateBuilder { + + private static Map iPath2TemplateMap; + + public static Map getPathTemplateMap(String template) { + Map lPathTemplateMap = new HashMap(); + String lTemplatePath = template + "/"; + + TemplateStore lTemplateStore = CodegenUIPlugin.getDefault() + .getTemplateStore(); + + TemplatePersistenceData[] lTemplateDataArray = lTemplateStore + .getTemplateData(false); + + for (TemplatePersistenceData data : lTemplateDataArray) { + String path = data.getId(); + if (path.startsWith(lTemplatePath)) { + if (data.isEnabled()) { + lPathTemplateMap.put( + path.substring(lTemplatePath.length()), data + .getTemplate()); + } + } + } + + return lPathTemplateMap; + } + + public static Map findAllOriTemplates() { + if (iPath2TemplateMap == null) { + iPath2TemplateMap = new HashMap(); + + try { + Map lPath2PatternMap = TemplateLoader.load(""); + for (String lpath : lPath2PatternMap.keySet()) { + String name = lpath.substring(lpath.lastIndexOf("/") + 1); + String description = lpath; + String pattern = lPath2PatternMap.get(lpath); + + ContentTypeManager lContentTypeManager = ContentTypeManager + .getInstance(); + IContentType type = lContentTypeManager + .findContentTypeFor(name); + String contextTypeId; + if (type != null) { + contextTypeId = (new StringBuilder(String.valueOf(type + .getId()))).append(".contenttype_context") + .toString(); + } else { + contextTypeId = CodeGenTemplateContextType.FILE_TEMPLATE_CONTEXT_TYPE; + } + + Template lTemplate = new Template(name, description, + contextTypeId, pattern, true); + iPath2TemplateMap.put(lpath, lTemplate); + } + } catch (IOException e) { + IStatus lStatus = new Status(IStatus.ERROR, + EclipseTemplateBuilder.class.getName(), + "Exception was thrown while loading templates.", e); + CodegenUIPlugin.getDefault().getLog().log(lStatus); + } + } + return iPath2TemplateMap; + + } + + public Map build(String path) { + Map result = new HashMap(); + Map lTemplatePathMap = getPathTemplateMap(path); + + for (String lpath : lTemplatePathMap.keySet()) { + org.eclipse.jface.text.templates.Template lEclipseTemplate = lTemplatePathMap + .get(lpath); + String pattern = lEclipseTemplate.getPattern(); + result.put(lpath, pattern); + } + return result; + } +}