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