View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.frontend.xeditor.tracker;
20  
21  import java.io.IOException;
22  import java.io.StringReader;
23  
24  import org.jdom2.Attribute;
25  import org.jdom2.Element;
26  import org.jdom2.JDOMException;
27  import org.jdom2.ProcessingInstruction;
28  import org.jdom2.Text;
29  import org.jdom2.input.SAXBuilder;
30  import org.jdom2.output.Format;
31  import org.jdom2.output.XMLOutputter;
32  import org.mycore.common.MCRException;
33  
34  public class MCRChangeData {
35  
36      protected String type;
37  
38      protected int pos;
39  
40      protected Element context;
41  
42      protected String text;
43  
44      protected ProcessingInstruction pi;
45  
46      private static final XMLOutputter RAW_OUTPUTTER = new XMLOutputter(Format.getRawFormat().setEncoding("UTF-8"));
47  
48      public MCRChangeData(String type, String text, int pos, Element context) {
49          this.type = type;
50          this.text = text;
51          this.pos = pos;
52          this.context = context;
53      }
54  
55      public MCRChangeData(String type, Attribute attribute) {
56          this(type, attribute2text(attribute), 0, attribute.getParent());
57      }
58  
59      public MCRChangeData(String type, Element data, int pos, Element context) {
60          this(type, element2text(data), pos, context);
61      }
62  
63      public ProcessingInstruction getProcessingInstruction() {
64          if (pi == null) {
65              String data = RAW_OUTPUTTER.outputString(new Text(text));
66              this.pi = new ProcessingInstruction(type, data);
67          }
68          return pi;
69      }
70  
71      public MCRChangeData(ProcessingInstruction pi, String prefix) {
72          this.pi = pi;
73          this.context = pi.getParentElement();
74          this.pos = context.indexOf(pi);
75          this.type = pi.getTarget().substring(prefix.length());
76  
77          String xml = "<x>" + pi.getData() + "</x>";
78          this.text = text2element(xml).getText();
79      }
80  
81      public String getType() {
82          return type;
83      }
84  
85      public Element getContext() {
86          return context;
87      }
88  
89      public int getPosition() {
90          return pos;
91      }
92  
93      public String getText() {
94          return text;
95      }
96  
97      public Element getElement() {
98          return text2element(text);
99      }
100 
101     public Attribute getAttribute() {
102         return text2attribute(text);
103     }
104 
105     private static String element2text(Element element) {
106         return RAW_OUTPUTTER.outputString(element);
107     }
108 
109     private Element text2element(String text) {
110         try {
111             return new SAXBuilder().build(new StringReader(text)).detachRootElement();
112         } catch (JDOMException | IOException ex) {
113             throw new MCRException("Exception in text2element: " + text, ex);
114         }
115     }
116 
117     private static String attribute2text(Attribute attribute) {
118         Element x = new Element("x").setAttribute(attribute.clone());
119         String text = element2text(x);
120         return text.substring(3, text.length() - 2).trim();
121     }
122 
123     public Attribute text2attribute(String text) {
124         String xtext = "<x " + text + " />";
125         return text2element(xtext).getAttributes().get(0).detach();
126     }
127 }