]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.graphviz/src/org/simantics/graphviz/AbstractAttributeContainer.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.graphviz / src / org / simantics / graphviz / AbstractAttributeContainer.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.graphviz;\r
13 \r
14 import java.io.PrintStream;\r
15 import java.util.HashMap;\r
16 import java.util.Map;\r
17 \r
18 /**\r
19  * Default implementation for IAttributeContainer.\r
20  * \r
21  * @author Hannu Niemistö\r
22  */\r
23 public class AbstractAttributeContainer implements IAttributeContainer {\r
24 \r
25     Map<String,String> attributes = new HashMap<String,String>();\r
26     \r
27     public void set(String key, String value) {\r
28         attributes.put(key, value);\r
29     }\r
30     \r
31     public String get(String key) {\r
32         return attributes.get(key);\r
33     }\r
34     \r
35     protected void writeAttributes(PrintStream s) {\r
36         s.print(" [");\r
37         boolean first = true;\r
38         for(Map.Entry<String, String> ent : attributes.entrySet()) {\r
39             String key = ent.getKey();\r
40             String value = ent.getValue();\r
41             if(first)\r
42                 first = false;\r
43             else\r
44                 s.print(' ');\r
45             s.print(key);\r
46             if(!value.isEmpty() && value.charAt(0) == '<' && value.charAt(value.length()-1) == '>') {\r
47                 s.print('=');\r
48                 s.print(value);\r
49             }\r
50             else {\r
51                     s.print("=\"");\r
52                     s.print(escape(value));\r
53                     s.print('\"');\r
54             }\r
55         }\r
56         s.println("];");\r
57     }\r
58     \r
59     static String escape(String str) {\r
60         str = str.replace("\"", "\\\"");\r
61         return str;\r
62     }\r
63     \r
64 }\r