sysperfana/perfinvestigator/com.nokia.carbide.cpp.pi.util/src/com/nokia/carbide/cpp/internal/pi/util/config/PIConfigXMLLoader.java
changeset 2 b9ab3b238396
child 12 ae255c9aa552
equal deleted inserted replaced
1:1050670c6980 2:b9ab3b238396
       
     1 /*
       
     2  * Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). 
       
     3  * All rights reserved.
       
     4  * This component and the accompanying materials are made available
       
     5  * under the terms of the License "Eclipse Public License v1.0"
       
     6  * which accompanies this distribution, and is available
       
     7  * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8  *
       
     9  * Initial Contributors:
       
    10  * Nokia Corporation - initial contribution.
       
    11  *
       
    12  * Contributors:
       
    13  *
       
    14  * Description: 
       
    15  *
       
    16  */
       
    17 
       
    18 package com.nokia.carbide.cpp.internal.pi.util.config;
       
    19 
       
    20 import java.io.IOException;
       
    21 import java.net.URL;
       
    22 
       
    23 import org.eclipse.emf.common.util.EList;
       
    24 import org.eclipse.emf.common.util.URI;
       
    25 import org.eclipse.emf.ecore.EObject;
       
    26 import org.eclipse.emf.ecore.resource.Resource;
       
    27 
       
    28 import com.nokia.carbide.cpp.internal.pi.util.config.gen.PIConfig.ButtonEventProfileListType;
       
    29 import com.nokia.carbide.cpp.internal.pi.util.config.gen.PIConfig.DocumentRoot;
       
    30 import com.nokia.carbide.cpp.internal.pi.util.config.gen.PIConfig.PIConfigFactory;
       
    31 import com.nokia.carbide.cpp.internal.pi.util.config.gen.PIConfig.PIConfigPackage;
       
    32 import com.nokia.carbide.cpp.internal.pi.util.config.gen.PIConfig.util.PIConfigResourceFactoryImpl;
       
    33 
       
    34 
       
    35 public class PIConfigXMLLoader {
       
    36 	
       
    37 	static public ButtonEventProfileListType loadPiSettings (URL url) throws IOException {
       
    38 		if (url == null)
       
    39 			return null;
       
    40 
       
    41 		// blank file could cause IOException, which is unnecessary. Just return blank model
       
    42 		if (url.openStream().available() == 0) {
       
    43 			return PIConfigFactory.eINSTANCE.createButtonEventProfileListType();
       
    44 		}
       
    45 
       
    46 		URI xmlURI = URI.createURI(url.toString());
       
    47 
       
    48 		PIConfigResourceFactoryImpl resFactory = new PIConfigResourceFactoryImpl();
       
    49 		Resource r = resFactory.createResource(xmlURI);
       
    50 
       
    51 		r.load(null);
       
    52 		EList<EObject> contents = r.getContents();
       
    53 	
       
    54 		DocumentRoot root = (DocumentRoot) contents.get(0);
       
    55 		ButtonEventProfileListType list = root.getButtonEventProfileList();
       
    56 		
       
    57 		return list;
       
    58 
       
    59 	}
       
    60 	
       
    61 	static public boolean writePiSettings(ButtonEventProfileListType list, URL url) throws IOException {
       
    62 		if (url == null)
       
    63 			return false;
       
    64 		URI xmlURI = URI.createURI(url.toString());
       
    65 	
       
    66 		PIConfigResourceFactoryImpl resFactory = new PIConfigResourceFactoryImpl();
       
    67 		Resource r = resFactory.createResource(xmlURI);
       
    68 		EList<EObject> contents = r.getContents();
       
    69 	
       
    70 		PIConfigFactory factory = PIConfigPackage.eINSTANCE.getPIConfigFactory();
       
    71 		DocumentRoot root = factory.createDocumentRoot();
       
    72 		root.setButtonEventProfileList(list);
       
    73 		contents.add(root);
       
    74 						
       
    75 		// write to disk
       
    76 		r.save(null);
       
    77 		return true;
       
    78 
       
    79 	}
       
    80 }