~francis-giraldeau/noesis/xsugar-strict

« back to all changes in this revision

Viewing changes to src/dk/brics/xsugar/xml/NamespaceAdder.java

  • Committer: Francis Giraldeau
  • Date: 2009-08-05 13:07:07 UTC
  • Revision ID: francis.giraldeau@revolutionlinux.com-20090805130707-b8p8x13khol5p9o5
ajout des sources

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package dk.brics.xsugar.xml;
 
2
 
 
3
import java.util.Map;
 
4
 
 
5
import dk.brics.xsugar.stylesheet.*;
 
6
 
 
7
/**
 
8
 * Inserts XML namespace declarations in the first start tag in an output string.
 
9
 */
 
10
public class NamespaceAdder {
 
11
        
 
12
        private Stylesheet stylesheet;
 
13
 
 
14
        /**
 
15
         * Constructs a new namespace adder.
 
16
         * @param stylesheet stylesheet
 
17
         */
 
18
        public NamespaceAdder(Stylesheet stylesheet) {
 
19
                this.stylesheet = stylesheet;
 
20
        }
 
21
 
 
22
        /**
 
23
         * Fixes the given output string.
 
24
         * @param s string
 
25
         * @return fixed string
 
26
         */
 
27
        public String fix(String s) {
 
28
                StringBuilder b = new StringBuilder();
 
29
                int d = 0;
 
30
                boolean insert = false;
 
31
                for (int i = 0; i < s.length(); i++) {
 
32
                        char c = s.charAt(i);
 
33
                        if (c == '<') { 
 
34
                                char c2 = s.charAt(i + 1);
 
35
                                insert = d == 0 && c2 != '/';
 
36
                                if (c2 == '/')
 
37
                                        d--;
 
38
                                else
 
39
                                        d++;
 
40
                        } else if (c == '>') 
 
41
                                if (insert)
 
42
                                        for (Map.Entry<String,String> ns : stylesheet.getNamespaces().entrySet()) {
 
43
                                                String prefix = ns.getKey();
 
44
                                                String uri = ns.getValue();
 
45
                                                String name;
 
46
                                                if (prefix != null || !uri.equals("")) {
 
47
                                                        if (prefix == null)
 
48
                                                                name = "xmlns";
 
49
                                                        else
 
50
                                                                name = "xmlns:" + prefix;
 
51
                                                        b.append(' ').append(name).append("=\"").append(uri).append("\"");
 
52
                                                }
 
53
                                        }
 
54
                        b.append(c);
 
55
                }
 
56
                return b.toString();
 
57
        }
 
58
}