forked from xwiki-contrib/application-diagram
/
DiagramContentHandler.java
188 lines (163 loc) · 7.49 KB
/
DiagramContentHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
/*
* See the NOTICE file distributed with this work for additional
* information regarding copyright ownership.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package com.xwiki.diagram.internal.handlers;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import javax.inject.Inject;
import javax.inject.Provider;
import javax.inject.Singleton;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParserFactory;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import org.xwiki.component.annotation.Component;
import org.xwiki.model.reference.DocumentReference;
import org.xwiki.model.reference.LocalDocumentReference;
import org.xwiki.xml.XMLUtils;
import com.xpn.xwiki.XWikiContext;
import com.xpn.xwiki.XWikiException;
import com.xpn.xwiki.doc.XWikiAttachment;
import com.xpn.xwiki.doc.XWikiDocument;
/**
* Handler for diagram content operations.
*
* @version $Id$
* @since 1.13
*/
@Component(roles = DiagramContentHandler.class)
@Singleton
public class DiagramContentHandler
{
/**
* View action.
*/
public static final String VIEW_ACTION = "view";
/**
* Reference to Diagram's class.
*/
public static final LocalDocumentReference DIAGRAM_CLASS = new LocalDocumentReference("Diagram", "DiagramClass");
@Inject
private Provider<GetDiagramLinksHandler> getDiagramLinksHandlerProvider;
@Inject
private DiagramLinkHandler linkHandler;
@Inject
private Provider<XWikiContext> contextProvider;
@Inject
private Logger logger;
/**
* Update the attachments content and save the new version.
*
* @param backlinkDoc document that has a backlink to diagram
* @param oldDocRef reference of the page before rename
* @param newDocRef reference of the page after rename
* @throws XWikiException when an error occurs while accessing documents.
* @throws IOException if something goes wrong while setting attachment's content.
*/
public void updateAttachment(XWikiDocument backlinkDoc, DocumentReference oldDocRef, DocumentReference newDocRef)
throws XWikiException, IOException
{
XWikiAttachment attachment = backlinkDoc.getAttachment("diagram.svg");
XWikiContext context = contextProvider.get();
if (attachment != null) {
backlinkDoc.setAttachment(getUpdatedAttachment(attachment, oldDocRef, newDocRef));
context.getWiki().saveDocument(backlinkDoc, "Updated attachment after page rename", context);
}
}
/**
* Migrate links inside diagram's attachment to the new name.
*
* @param attachment attachment to be processed
* @param oldDocRef reference of the page before rename
* @param newDocRef reference of the page after rename
* @return the modified attachment
* @throws XWikiException when an error occurs while accessing documents.
* @throws IOException if something goes wrong while setting attachment's content.
*/
private XWikiAttachment getUpdatedAttachment(XWikiAttachment attachment, DocumentReference oldDocRef,
DocumentReference newDocRef) throws XWikiException, IOException
{
XWikiContext context = contextProvider.get();
XWikiDocument oldDoc = context.getWiki().getDocument(oldDocRef, context);
XWikiDocument newDoc = context.getWiki().getDocument(newDocRef, context);
String attachmentContent = IOUtils.toString(attachment.getContentInputStream(context), "UTF-8");
String regex = "\"%s\"";
String oldAbsoluteURL = String.format(regex, oldDoc.getExternalURL(VIEW_ACTION, context));
String newAbsoluteURL = String.format(regex, newDoc.getExternalURL(VIEW_ACTION, context));
String oldURL = String.format(regex, oldDoc.getURL(VIEW_ACTION, context));
String newURL = String.format(regex, newDoc.getURL(VIEW_ACTION, context));
String newContent = attachmentContent.replaceAll(oldAbsoluteURL, newAbsoluteURL).replaceAll(oldURL, newURL);
attachment.setContent(new ByteArrayInputStream(newContent.getBytes()));
return attachment;
}
/**
* Update content of the diagram with new links.
*
* @param backlinkDoc document that has a backlink to the diagram
* @param originalDocRef reference of the document before rename
* @param currentDocRef reference of the document after rename
* @param context context of the execution
* @throws ParserConfigurationException if a DocumentBuilder cannot be created
* @throws SAXException if parsing the document fails
* @throws IOException if any IO errors occur
* @throws XWikiException if saving the document fails
*/
public void updateDiagramContent(XWikiDocument backlinkDoc, DocumentReference originalDocRef,
DocumentReference currentDocRef, XWikiContext context)
throws ParserConfigurationException, SAXException, IOException, XWikiException
{
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
.parse(new ByteArrayInputStream(backlinkDoc.getContent().getBytes()));
NodeList userObjectList = document.getElementsByTagName("UserObject");
for (int i = 0; i < userObjectList.getLength(); i++) {
linkHandler.updateUserObjectNode(userObjectList.item(i), currentDocRef, originalDocRef);
}
NodeList mxCellList = document.getElementsByTagName("mxCell");
for (int i = 0; i < mxCellList.getLength(); i++) {
linkHandler.updateMxCellNode(mxCellList.item(i), currentDocRef, originalDocRef);
}
backlinkDoc.setContent(XMLUtils.serialize(document));
context.getWiki().saveDocument(backlinkDoc, "Updated diagram after page rename", context);
}
/**
* Search referenced pages inside the content of a diagram.
*
* @param content the content of the diagram
* @param diagramReference the reference of current diagram
* @return linkedPages list of pages linked in this content
*/
public List<DocumentReference> getLinkedPages(String content, DocumentReference diagramReference)
{
try {
GetDiagramLinksHandler getDiagramLinksHandler = getDiagramLinksHandlerProvider.get();
SAXParserFactory.newInstance().newSAXParser().parse(new ByteArrayInputStream(content.getBytes()),
getDiagramLinksHandler);
return getDiagramLinksHandler.getLinkedPages(diagramReference);
} catch (ParserConfigurationException | SAXException | IOException e) {
logger.warn("Failed while getting diagram linked pages", e);
}
return Collections.emptyList();
}
}