1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.xar.internal

File XarUtils.java

 

Coverage histogram

../../../../img/srcFileCovDistChart7.png
66% of files have more coverage

Code metrics

42
63
3
1
194
126
36
0.57
21
3
12

Classes

Class Line # Actions
XarUtils 47 63 0% 36 35
0.675925967.6%
 

Contributing tests

This file is covered by 23 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software 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 GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package org.xwiki.xar.internal;
21   
22    import java.io.File;
23    import java.io.FileInputStream;
24    import java.io.FileNotFoundException;
25    import java.io.IOException;
26    import java.io.InputStream;
27    import java.util.Locale;
28   
29    import javax.xml.stream.XMLInputFactory;
30    import javax.xml.stream.XMLStreamException;
31    import javax.xml.stream.XMLStreamReader;
32   
33    import org.apache.commons.lang3.LocaleUtils;
34    import org.xwiki.model.EntityType;
35    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
36    import org.xwiki.model.internal.reference.RelativeStringEntityReferenceResolver;
37    import org.xwiki.model.reference.EntityReference;
38    import org.xwiki.model.reference.LocalDocumentReference;
39    import org.xwiki.xar.XarException;
40    import org.xwiki.xar.internal.model.XarDocumentModel;
41    import org.xwiki.xml.stax.StAXUtils;
42   
43    /**
44    * @version $Id: e4d6d080d73f24d09f692b2ad7b8d5fe94e4351e $
45    * @since 5.4M1
46    */
 
47    public final class XarUtils
48    {
49    public static final RelativeStringEntityReferenceResolver RESOLVER =
50    new RelativeStringEntityReferenceResolver(new DefaultSymbolScheme());
51   
52    private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory.newInstance();
53   
 
54  0 toggle private XarUtils()
55    {
56    // Utility class
57    }
58   
59    /**
60    * Extract {@link LocalDocumentReference} from a XAR document XML file.
61    *
62    * @param documentFile the file to parse
63    * @return the reference extracted from the stream
64    * @throws XarException when failing to parse the document stream
65    * @since 9.5RC1
66    */
 
67  0 toggle public static LocalDocumentReference getReference(File documentFile) throws XarException
68    {
69  0 try (FileInputStream stream = new FileInputStream(documentFile)) {
70  0 return getReference(stream);
71    } catch (FileNotFoundException e) {
72  0 throw new XarException("Cannot find file", e);
73    } catch (IOException e) {
74  0 throw new XarException("Failed close file stream", e);
75    }
76    }
77   
78    /**
79    * Extract {@link LocalDocumentReference} from a XAR document XML stream.
80    *
81    * @param documentStream the stream to parse
82    * @return the reference extracted from the stream
83    * @throws XarException when failing to parse the document stream
84    * @since 5.4M1
85    */
 
86  28385 toggle public static LocalDocumentReference getReference(InputStream documentStream) throws XarException
87    {
88  28385 XMLStreamReader xmlReader;
89  28385 try {
90  28385 xmlReader = XML_INPUT_FACTORY.createXMLStreamReader(documentStream);
91    } catch (XMLStreamException e) {
92  0 throw new XarException("Failed to create a XML read", e);
93    }
94   
95  28385 EntityReference reference = null;
96  28385 Locale locale = null;
97   
98  28385 String legacySpace = null;
99  28385 String legacyPage = null;
100   
101  28385 try {
102    // <xwikidoc>
103   
104  28385 xmlReader.nextTag();
105   
106  28385 xmlReader.require(XMLStreamReader.START_ELEMENT, null, XarDocumentModel.ELEMENT_DOCUMENT);
107   
108    // Reference
109  28385 String referenceString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE);
110  28385 if (referenceString != null) {
111  22875 reference = RESOLVER.resolve(referenceString, EntityType.DOCUMENT);
112    }
113   
114    // Locale
115  28385 String localeString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE);
116  28385 if (localeString != null) {
117  22875 if (localeString.isEmpty()) {
118  13440 locale = Locale.ROOT;
119    } else {
120  9435 locale = LocaleUtils.toLocale(localeString);
121    }
122    }
123   
124    // Legacy fallback
125  28385 if (reference == null || locale == null) {
126  16530 for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
127  16530 String elementName = xmlReader.getLocalName();
128   
129  16530 if (XarDocumentModel.ELEMENT_NAME.equals(elementName)) {
130  5510 if (reference == null) {
131  5510 legacyPage = xmlReader.getElementText();
132   
133  5510 if (legacySpace != null && locale != null) {
134  0 break;
135    }
136  0 } else if (locale != null) {
137  0 break;
138    }
139  11020 } else if (XarDocumentModel.ELEMENT_SPACE.equals(elementName)) {
140  5510 if (reference == null) {
141  5510 legacySpace = xmlReader.getElementText();
142   
143  5510 if (legacyPage != null && locale != null) {
144  0 break;
145    }
146  0 } else if (locale != null) {
147  0 break;
148    }
149  5510 } else if (XarDocumentModel.ELEMENT_LOCALE.equals(elementName)) {
150  5510 if (locale == null) {
151  5510 String value = xmlReader.getElementText();
152  5510 if (value.length() == 0) {
153  5239 locale = Locale.ROOT;
154    } else {
155  271 locale = LocaleUtils.toLocale(value);
156    }
157    }
158   
159  5510 if (reference != null || (legacySpace != null && legacyPage != null)) {
160  5510 break;
161    }
162    } else {
163  0 StAXUtils.skipElement(xmlReader);
164    }
165    }
166    }
167    } catch (XMLStreamException e) {
168  0 throw new XarException("Failed to parse document", e);
169    } finally {
170  28385 try {
171  28385 xmlReader.close();
172    } catch (XMLStreamException e) {
173  0 throw new XarException("Failed to close XML reader", e);
174    }
175    }
176   
177  28385 if (reference == null) {
178  5510 if (legacySpace == null) {
179  0 throw new XarException("Missing space element");
180    }
181  5510 if (legacyPage == null) {
182  0 throw new XarException("Missing page element");
183    }
184   
185  5510 reference = new LocalDocumentReference(legacySpace, legacyPage);
186    }
187   
188  28385 if (locale == null) {
189  0 throw new XarException("Missing locale element");
190    }
191   
192  28385 return new LocalDocumentReference(reference, locale);
193    }
194    }