source: osm/applications/editors/josm/plugins/opendata/includes/org/jopendocument/dom/ChildCreator.java@ 28000

Last change on this file since 28000 was 28000, checked in by donvip, 12 years ago

Import new "opendata" JOSM plugin

File size: 4.4 KB
Line 
1/*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2008 jOpenDocument, by ILM Informatique. All rights reserved.
5 *
6 * The contents of this file are subject to the terms of the GNU
7 * General Public License Version 3 only ("GPL").
8 * You may not use this file except in compliance with the License.
9 * You can obtain a copy of the License at http://www.gnu.org/licenses/gpl-3.0.html
10 * See the License for the specific language governing permissions and limitations under the License.
11 *
12 * When distributing the software, include this License Header Notice in each file.
13 *
14 */
15
16package org.jopendocument.dom;
17
18import java.util.ArrayList;
19import java.util.List;
20import java.util.ListIterator;
21
22import org.jdom.Element;
23import org.jdom.Namespace;
24
25/**
26 * A helper to create children in the schema order.
27 *
28 * @author Sylvain CUAZ
29 */
30public class ChildCreator {
31
32 private final Element content;
33 private final List<Element> elemsOrder;
34
35 protected ChildCreator(final Element content, final List<Element> children) {
36 if (content == null)
37 throw new NullPointerException("null content");
38 this.content = content;
39 this.elemsOrder = new ArrayList<Element>(children);
40 }
41
42 public final Element getElement() {
43 return this.content;
44 }
45
46 // *** children
47
48 public final Element getChild(Namespace childNS, String childName) {
49 return this.getChild(childNS, childName, false);
50 }
51
52 private final int indexOf(Namespace childNS, String childName) {
53 for (int i = 0; i < this.elemsOrder.size(); i++) {
54 final Element elem = this.elemsOrder.get(i);
55 if (elem.getNamespace().equals(childNS) && elem.getName().equals(childName))
56 return i;
57 }
58 return -1;
59 }
60
61 private final int indexOf(final Element elem) {
62 return this.indexOf(elem.getNamespace(), elem.getName());
63 }
64
65 /**
66 * Trouve l'index ou il faut insérer le fils dans ce document.
67 *
68 * @param childName le nom du fils que l'on veut insérer.
69 * @return l'index ou il faut l'insérer (s'il est déjà présent son index actuel +1).
70 * @throws IllegalArgumentException if childName is not in {@link #getChildren()}.
71 */
72 @SuppressWarnings("unchecked")
73 private final int findInsertIndex(Namespace childNS, String childName) {
74 // eg 6, for "master-styles"
75 final int idealIndex = indexOf(childNS, childName);
76 if (idealIndex == -1)
77 throw new IllegalArgumentException(childName + " is unknown.");
78 final Element existingChild = this.getChild(childNS, childName);
79 if (existingChild != null)
80 return this.getElement().getChildren().indexOf(existingChild) + 1;
81 // eg [scripts, font-decls, styles, font-face-decls, automatic-styles, body]
82 final List<Element> children = this.getElement().getChildren();
83 final ListIterator<Element> iter = children.listIterator();
84 while (iter.hasNext()) {
85 final Element elem = iter.next();
86 if (indexOf(elem) > idealIndex)
87 // eg indexOf("body") == 7 > 6
88 // eg return 5
89 return iter.previousIndex();
90 }
91 return children.size();
92 }
93
94 /**
95 * Insère cet élément à la bonne place. The child should not be already present.
96 *
97 * @param child l'élément à insérer, doit être dans TOP_ELEMENTS.
98 */
99 @SuppressWarnings("unchecked")
100 private final void insertChild(Element child) {
101 // on ajoute au bon endroit
102 this.getElement().getChildren().add(this.findInsertIndex(child.getNamespace(), child.getName()), child);
103 }
104
105 /**
106 * Return the asked child, optionally creating it.
107 *
108 * @param childNS the namespace of the child.
109 * @param childName the name of the child.
110 * @param create whether it should be created in case it doesn't exist.
111 * @return the asked child or <code>null</code> if it doesn't exist and create is
112 * <code>false</code>
113 */
114 public final Element getChild(Namespace childNS, String childName, boolean create) {
115 Element child = this.getElement().getChild(childName, childNS);
116 if (create && child == null) {
117 child = new Element(childName, childNS);
118 this.insertChild(child);
119 }
120 return child;
121 }
122}
Note: See TracBrowser for help on using the repository browser.