1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.xar.internal

File XarUtils.java

 

Coverage histogram

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

Code metrics

42
58
2
1
171
112
31
0.53
29
2
15.5

Classes

Class Line # Actions
XarUtils 43 58 0% 31 31
0.696078469.6%
 

Contributing tests

This file is covered by 21 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.InputStream;
23    import java.util.Locale;
24   
25    import javax.xml.stream.XMLInputFactory;
26    import javax.xml.stream.XMLStreamException;
27    import javax.xml.stream.XMLStreamReader;
28   
29    import org.apache.commons.lang3.LocaleUtils;
30    import org.xwiki.model.EntityType;
31    import org.xwiki.model.internal.reference.DefaultSymbolScheme;
32    import org.xwiki.model.internal.reference.RelativeStringEntityReferenceResolver;
33    import org.xwiki.model.reference.EntityReference;
34    import org.xwiki.model.reference.LocalDocumentReference;
35    import org.xwiki.xar.XarException;
36    import org.xwiki.xar.internal.model.XarDocumentModel;
37    import org.xwiki.xml.stax.StAXUtils;
38   
39    /**
40    * @version $Id: 5a22efe6a9ae616a997e928913f3a162de599ce1 $
41    * @since 5.4M1
42    */
 
43    public final class XarUtils
44    {
45    public static final RelativeStringEntityReferenceResolver RESOLVER =
46    new RelativeStringEntityReferenceResolver(new DefaultSymbolScheme());
47   
48    private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory.newInstance();
49   
 
50  0 toggle private XarUtils()
51    {
52    // Utility class
53    }
54   
55    /**
56    * Extract {@link LocalDocumentReference} from a XAR document XML stream.
57    *
58    * @param documentStream the stream to parse
59    * @return the reference extracted from the stream
60    * @throws XarException when failing to parse the document stream
61    * @since 5.4M1
62    */
 
63  3714 toggle public static LocalDocumentReference getReference(InputStream documentStream) throws XarException
64    {
65  3714 XMLStreamReader xmlReader;
66  3714 try {
67  3714 xmlReader = XML_INPUT_FACTORY.createXMLStreamReader(documentStream);
68    } catch (XMLStreamException e) {
69  0 throw new XarException("Failed to create a XML read", e);
70    }
71   
72  3714 EntityReference reference = null;
73  3714 Locale locale = null;
74   
75  3714 String legacySpace = null;
76  3714 String legacyPage = null;
77   
78  3714 try {
79    // <xwikidoc>
80   
81  3714 xmlReader.nextTag();
82   
83  3714 xmlReader.require(XMLStreamReader.START_ELEMENT, null, XarDocumentModel.ELEMENT_DOCUMENT);
84   
85    // Reference
86  3714 String referenceString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_REFERENCE);
87  3714 if (referenceString != null) {
88  1720 reference = RESOLVER.resolve(referenceString, EntityType.DOCUMENT);
89    }
90   
91    // Locale
92  3714 String localeString = xmlReader.getAttributeValue(null, XarDocumentModel.ATTRIBUTE_DOCUMENT_LOCALE);
93  3714 if (localeString != null) {
94  1116 if (localeString.isEmpty()) {
95  0 locale = Locale.ROOT;
96    } else {
97  1116 locale = LocaleUtils.toLocale(localeString);
98    }
99    }
100   
101    // Legacy fallback
102  3714 if (reference == null || locale == null) {
103  5982 for (xmlReader.nextTag(); xmlReader.isStartElement(); xmlReader.nextTag()) {
104  5982 String elementName = xmlReader.getLocalName();
105   
106  5982 if (XarDocumentModel.ELEMENT_NAME.equals(elementName)) {
107  1994 if (reference == null) {
108  1994 legacyPage = xmlReader.getElementText();
109   
110  1994 if (legacySpace != null && locale != null) {
111  0 break;
112    }
113  0 } else if (locale != null) {
114  0 break;
115    }
116  3988 } else if (XarDocumentModel.ELEMENT_SPACE.equals(elementName)) {
117  1994 if (reference == null) {
118  1994 legacySpace = xmlReader.getElementText();
119   
120  1994 if (legacyPage != null && locale != null) {
121  0 break;
122    }
123  0 } else if (locale != null) {
124  0 break;
125    }
126  1994 } else if (XarDocumentModel.ELEMENT_LOCALE.equals(elementName)) {
127  1994 if (locale == null) {
128  1994 String value = xmlReader.getElementText();
129  1994 if (value.length() == 0) {
130  1917 locale = Locale.ROOT;
131    } else {
132  77 locale = LocaleUtils.toLocale(value);
133    }
134    }
135   
136  1994 if (reference != null || (legacySpace != null && legacyPage != null)) {
137  1994 break;
138    }
139    } else {
140  0 StAXUtils.skipElement(xmlReader);
141    }
142    }
143    }
144    } catch (XMLStreamException e) {
145  0 throw new XarException("Failed to parse document", e);
146    } finally {
147  3714 try {
148  3714 xmlReader.close();
149    } catch (XMLStreamException e) {
150  0 throw new XarException("Failed to close XML reader", e);
151    }
152    }
153   
154  3714 if (reference == null) {
155  1994 if (legacySpace == null) {
156  0 throw new XarException("Missing space element");
157    }
158  1994 if (legacyPage == null) {
159  0 throw new XarException("Missing page element");
160    }
161   
162  1994 reference = new LocalDocumentReference(legacySpace, legacyPage);
163    }
164   
165  3714 if (locale == null) {
166  0 throw new XarException("Missing locale element");
167    }
168   
169  3714 return new LocalDocumentReference(reference, locale);
170    }
171    }