package jetbrains.mps.persistence.xml; /*Generated by MPS */ import org.jetbrains.mps.openapi.model.SNode; import org.jdom.Document; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SConceptOperations; import jetbrains.mps.smodel.adapter.structure.MetaAdapterFactory; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SPropertyOperations; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SLinkOperations; import jetbrains.mps.smodel.SNodeId; import org.jdom.Element; import org.jdom.Attribute; import jetbrains.mps.internal.collections.runtime.ListSequence; import java.util.List; import org.jdom.Namespace; import org.jdom.Content; import jetbrains.mps.internal.collections.runtime.Sequence; import org.jdom.Comment; import jetbrains.mps.internal.collections.runtime.ISelector; import org.jdom.ProcessingInstruction; import org.jdom.CDATA; import org.jdom.Text; import org.jdom.EntityRef; import java.util.ArrayList; import jetbrains.mps.lang.smodel.generator.smodelAdapter.SNodeOperations; import org.jetbrains.mps.openapi.persistence.PersistenceFacade; import jetbrains.mps.smodel.SModelUtil_new; public class XmlConverter { public static SNode convertDocument(String name, Document document) { // TODO replace dom-based implementation with a good XML parser SNode file = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54c94c0L, "jetbrains.mps.core.xml.structure.XmlFile")); SPropertyOperations.set(file, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"), name); SLinkOperations.setNewChild(file, MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54c94c0L, 0x5c842a42c54c94c1L, "document"), MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5e2f66f285946ac9L, "jetbrains.mps.core.xml.structure.XmlDocument")); SLinkOperations.setTarget(SLinkOperations.getTarget(file, MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54c94c0L, 0x5c842a42c54c94c1L, "document")), MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5e2f66f285946ac9L, 0x5c842a42c549486fL, "rootElement"), convertElement(document.getRootElement())); ((jetbrains.mps.smodel.SNode) file).setId(SNodeId.fromString("~" + name)); return file; } private static SNode convertElement(Element elem) { SNode result = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b10b2L, "jetbrains.mps.core.xml.structure.XmlElement")); String namespacePrefix = elem.getNamespacePrefix(); SPropertyOperations.set(result, MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b10b2L, 0x5c842a42c54b10b6L, "tagName"), ((namespacePrefix == null || namespacePrefix.length() == 0) ? elem.getName() : namespacePrefix + ":" + elem.getName())); for (Attribute a : ListSequence.fromList((List<Attribute>) elem.getAttributes())) { ListSequence.fromList(SLinkOperations.getChildren(result, MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b10b2L, 0x5c842a42c54b10b5L, "attributes"))).addElement(convertAttribute(a)); } List<Namespace> additionalNamespaces = (List<Namespace>) elem.getAdditionalNamespaces(); for (Namespace ns : ListSequence.fromList(additionalNamespaces)) { ListSequence.fromList(SLinkOperations.getChildren(result, MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b10b2L, 0x5c842a42c54b10b5L, "attributes"))).addElement(createXmlAttribute_h7fa2c_a0a0a5a3("xmlns:" + ns.getPrefix(), convertAttributeText(ns.getURI()))); } List<Content> list = (List<Content>) elem.getContent(); Content[] contents = ListSequence.fromList(list).toGenericArray(Content.class); for (int i = 0; i < contents.length; i++) { Iterable<SNode> content = convertContent((i > 0 ? contents[i - 1] : null), contents[i], (i + 1 < contents.length ? contents[i + 1] : null)); if (content != null) { ListSequence.fromList(SLinkOperations.getChildren(result, MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b10b2L, 0x16838b3fce9a4922L, "content"))).addSequence(Sequence.fromIterable(content)); } } SPropertyOperations.set(result, MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b10b2L, 0x61218fae7b61b5d5L, "shortEmptyNotation"), "" + (elem.getContentSize() == 0)); return result; } private static Iterable<SNode> convertContent(Content prev, Content c, Content next) { if (c instanceof Element) { return Sequence.<SNode>singleton(convertElement((Element) c)); } else if (c instanceof Comment) { String commentText = ((Comment) c).getText(); SNode res = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c5494878L, "jetbrains.mps.core.xml.structure.XmlComment")); ListSequence.fromList(SLinkOperations.getChildren(res, MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c5494878L, 0x16838b3fce9bec77L, "lines"))).addSequence(Sequence.fromIterable(Sequence.fromArray(commentText.split("\r?\n"))).select(new ISelector<String, SNode>() { public SNode select(String it) { return createXmlCommentLine_h7fa2c_a0a0a0a0c0a0a4(it); } })); return Sequence.<SNode>singleton(res); } else if (c instanceof ProcessingInstruction) { ProcessingInstruction pi = (ProcessingInstruction) c; return Sequence.<SNode>singleton(createXmlProcessingInstruction_h7fa2c_a0a1a1a0e(pi.getTarget(), pi.getData())); } else if (c instanceof CDATA) { String cdata = ((Text) c).getText(); return Sequence.<SNode>singleton(createXmlCDATA_h7fa2c_a0a1a2a0e(cdata)); } else if (c instanceof Text) { String text = ((Text) c).getText(); int index = 0; boolean nlSeen = !((prev == null || prev instanceof Element)); for (; index < text.length(); index++) { char ch = text.charAt(index); if (ch == '\n') { if (!(nlSeen)) { nlSeen = true; continue; } } if (ch != ' ' && ch != '\t' && ch != '\r') { break; } } text = text.substring(index); index = text.length(); for (; index > 0; index--) { char ch = text.charAt(index - 1); if (ch != ' ' && ch != '\t') { break; } } text = text.substring(0, index); if (text.isEmpty()) { return null; } if (text.endsWith("\n") && (next == null || next instanceof Element)) { text = text.substring(0, text.length() - 1); } return convertText(Sequence.fromIterable(Sequence.fromArray((" " + text + " ").split("\n"))).select(new ISelector<String, String>() { public String select(String it) { return ((it == null ? null : it.trim())); } }).toGenericArray(String.class)); } else if (c instanceof EntityRef) { String name = ((EntityRef) c).getName(); return Sequence.<SNode>singleton(createXmlEntityRef_h7fa2c_a0a1a4a0e(name)); } return null; } private static Iterable<SNode> convertText(String[] lines) { List<SNode> result = ListSequence.fromList(new ArrayList<SNode>()); for (int i = 0; i < lines.length; i++) { String s = lines[i]; int len = s.length(); int start = 0; for (int e = 0; e < len; e++) { char c = s.charAt(e); if (c < 0x20 || c == '\'' || c == '"' || c == '<' || c == '>' || c == '&') { if (e > start) { ListSequence.fromList(result).addElement(createXmlText_h7fa2c_a0a0a0a1a3a1a5(s.substring(start, e))); } start = e + 1; if (c < 0x20) { ListSequence.fromList(result).addElement(createXmlCharRef_h7fa2c_a0a0a2a1a3a1a5(toHex4(c))); } else { String name = "quot"; switch (c) { case '\'': name = "apos"; break; case '<': name = "lt"; break; case '>': name = "gt"; break; case '&': name = "amp"; break; default: } ListSequence.fromList(result).addElement(createXmlEntityRef_h7fa2c_a0a2a0c0b0d0b0f(name)); } } } if (len > start) { ListSequence.fromList(result).addElement(createXmlText_h7fa2c_a0a0a4a1a5(s.substring(start, len))); } if (!(SNodeOperations.isInstanceOf(ListSequence.fromList(result).last(), MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9aa513L, "jetbrains.mps.core.xml.structure.XmlText"))) && i + 1 < lines.length || len == 0) { ListSequence.fromList(result).addElement(createXmlText_h7fa2c_a0a0a5a1a5()); } } return result; } private static Iterable<SNode> convertAttributeText(String s) { List<SNode> result = ListSequence.fromList(new ArrayList<SNode>()); int len = s.length(); int start = 0; for (int e = 0; e < len; e++) { char c = s.charAt(e); if (c < 0x20 || c == '\'' || c == '"' || c == '<' || c == '>' || c == '&') { if (e > start) { ListSequence.fromList(result).addElement(createXmlTextValue_h7fa2c_a0a0a0a1a3a6(s.substring(start, e))); } start = e + 1; if (c < 0x20) { ListSequence.fromList(result).addElement(createXmlCharRefValue_h7fa2c_a0a0a2a1a3a6(toHex4(c))); } else { String name = "quot"; switch (c) { case '\'': name = "apos"; break; case '<': name = "lt"; break; case '>': name = "gt"; break; case '&': name = "amp"; break; default: } ListSequence.fromList(result).addElement(createXmlEntityRefValue_h7fa2c_a0a2a0c0b0d0g(name)); } } } if (len > start) { ListSequence.fromList(result).addElement(createXmlTextValue_h7fa2c_a0a0a4a6(s.substring(start, len))); } return result; } private static String toHex4(char c) { String r = Integer.toHexString(c); return "0000".substring(r.length()) + r; } private static SNode convertAttribute(Attribute elem) { SNode result = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b8df3L, "jetbrains.mps.core.xml.structure.XmlAttribute")); SPropertyOperations.set(result, MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b8df3L, 0x5c842a42c54b8df6L, "attrName"), elem.getName()); ListSequence.fromList(SLinkOperations.getChildren(result, MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b8df3L, 0x5c842a42c54cfd1eL, "value"))).addSequence(Sequence.fromIterable(convertAttributeText(elem.getValue()))); return result; } public static SNode newDocument(String name) { SNode file = SConceptOperations.createNewNode(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54c94c0L, "jetbrains.mps.core.xml.structure.XmlFile")); SPropertyOperations.set(file, MetaAdapterFactory.getProperty(0xceab519525ea4f22L, 0x9b92103b95ca8c0cL, 0x110396eaaa4L, 0x110396ec041L, "name"), name); SLinkOperations.setTarget(file, MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54c94c0L, 0x5c842a42c54c94c1L, "document"), createXmlDocument_h7fa2c_a0a2a11(name)); return file; } private static SNode createXmlAttribute_h7fa2c_a0a0a5a3(Object p0, Object p1) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b8df3L, "jetbrains.mps.core.xml.structure.XmlAttribute"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b8df3L, 0x5c842a42c54b8df6L, "attrName"), p0 + ""); for (SNode n : (Iterable<SNode>) p1) { n1.addChild(MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b8df3L, 0x5c842a42c54cfd1eL, "value"), n); } return n1; } private static SNode createXmlCommentLine_h7fa2c_a0a0a0a0c0a0a4(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9bec5eL, "jetbrains.mps.core.xml.structure.XmlCommentLine"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9bec5eL, 0x16838b3fce9bec5fL, "text"), p0 + ""); return n1; } private static SNode createXmlProcessingInstruction_h7fa2c_a0a1a1a0e(Object p0, Object p1) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c5494875L, "jetbrains.mps.core.xml.structure.XmlProcessingInstruction"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c5494875L, 0x5c842a42c5494876L, "target"), p0 + ""); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c5494875L, 0x5c842a42c5494877L, "rawData"), p1 + ""); return n1; } private static SNode createXmlCDATA_h7fa2c_a0a1a2a0e(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c549487aL, "jetbrains.mps.core.xml.structure.XmlCDATA"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c549487aL, 0x16838b3fce9b2633L, "content"), p0 + ""); return n1; } private static SNode createXmlEntityRef_h7fa2c_a0a1a4a0e(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9c6f4dL, "jetbrains.mps.core.xml.structure.XmlEntityRef"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9c6f4dL, 0x16838b3fce9c6f8fL, "entityName"), p0 + ""); return n1; } private static SNode createXmlText_h7fa2c_a0a0a0a1a3a1a5(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9aa513L, "jetbrains.mps.core.xml.structure.XmlText"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9aa513L, 0x16838b3fce9aaa68L, "value"), p0 + ""); return n1; } private static SNode createXmlCharRef_h7fa2c_a0a0a2a1a3a1a5(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x2abf08504ffe9886L, "jetbrains.mps.core.xml.structure.XmlCharRef"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x2abf08504ffe9886L, 0x2abf08504ffe9887L, "charCode"), p0 + ""); return n1; } private static SNode createXmlEntityRef_h7fa2c_a0a2a0c0b0d0b0f(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9c6f4dL, "jetbrains.mps.core.xml.structure.XmlEntityRef"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9c6f4dL, 0x16838b3fce9c6f8fL, "entityName"), p0 + ""); return n1; } private static SNode createXmlText_h7fa2c_a0a0a4a1a5(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9aa513L, "jetbrains.mps.core.xml.structure.XmlText"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9aa513L, 0x16838b3fce9aaa68L, "value"), p0 + ""); return n1; } private static SNode createXmlText_h7fa2c_a0a0a5a1a5() { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9aa513L, "jetbrains.mps.core.xml.structure.XmlText"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x16838b3fce9aa513L, 0x16838b3fce9aaa68L, "value"), ""); return n1; } private static SNode createXmlTextValue_h7fa2c_a0a0a0a1a3a6(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd1fL, "jetbrains.mps.core.xml.structure.XmlTextValue"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd1fL, 0x5c842a42c54cfd20L, "text"), p0 + ""); return n1; } private static SNode createXmlCharRefValue_h7fa2c_a0a0a2a1a3a6(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x2abf08504ffed7feL, "jetbrains.mps.core.xml.structure.XmlCharRefValue"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x2abf08504ffed7feL, 0x2abf08504ffed806L, "charCode"), p0 + ""); return n1; } private static SNode createXmlEntityRefValue_h7fa2c_a0a2a0c0b0d0g(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd21L, "jetbrains.mps.core.xml.structure.XmlEntityRefValue"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd21L, 0x5c842a42c54d0258L, "entityName"), p0 + ""); return n1; } private static SNode createXmlTextValue_h7fa2c_a0a0a4a6(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd1fL, "jetbrains.mps.core.xml.structure.XmlTextValue"), null, null, false); n1.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54cfd1fL, 0x5c842a42c54cfd20L, "text"), p0 + ""); return n1; } private static SNode createXmlDocument_h7fa2c_a0a2a11(Object p0) { PersistenceFacade facade = PersistenceFacade.getInstance(); SNode n1 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5e2f66f285946ac9L, "jetbrains.mps.core.xml.structure.XmlDocument"), null, null, false); { SNode n2 = SModelUtil_new.instantiateConceptDeclaration(MetaAdapterFactory.getConcept(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b10b2L, "jetbrains.mps.core.xml.structure.XmlElement"), null, null, false); n2.setProperty(MetaAdapterFactory.getProperty(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5c842a42c54b10b2L, 0x5c842a42c54b10b6L, "tagName"), p0 + ""); n1.addChild(MetaAdapterFactory.getContainmentLink(0x479c7a8c02f943b5L, 0x9139d910cb22f298L, 0x5e2f66f285946ac9L, 0x5c842a42c549486fL, "rootElement"), n2); } return n1; } }