- /** Parse package list with nested directive with ',' */
- private List<String> parsePackages(String str) {
- List<String> res = new ArrayList<String>();
- StringBuffer curr = new StringBuffer("");
- boolean in = false;
- for (char c : str.toCharArray()) {
- if (c == ',') {
- if (!in) {
- res.add(curr.toString());
- curr = new StringBuffer("");
- }
- } else if (c == '\"') {
- in = !in;
- curr.append(c);
- } else {
- curr.append(c);
- }
- }
- res.add(curr.toString());
- log.debug(res);
- return res;
- }
-
- private void addAttr(String key, Node node, Attributes attrs)
- throws RepositoryException {
- addAttr(new Name(key), node, attrs);
- }
-
- private void addAttr(Name key, Node node, Attributes attrs)
- throws RepositoryException {
- if (attrs.containsKey(key)) {
- String value = attrs.getValue(key);
- node.setProperty(SlcNames.SLC_ + key, value);
- }
- }
-
- private void cleanSubNodes(Node node, String name)
- throws RepositoryException {
- if (node.hasNode(name)) {
- NodeIterator nit = node.getNodes(name);
- while (nit.hasNext())
- nit.nextNode().remove();
- }
- }
-
- protected void mapOsgiVersion(Version version, Node versionNode)
- throws RepositoryException {
- versionNode.setProperty(SlcNames.SLC_AS_STRING, version.toString());
- versionNode.setProperty(SlcNames.SLC_MAJOR, version.getMajor());
- versionNode.setProperty(SlcNames.SLC_MINOR, version.getMinor());
- versionNode.setProperty(SlcNames.SLC_MICRO, version.getMicro());
- if (!version.getQualifier().equals(""))
- versionNode.setProperty(SlcNames.SLC_QUALIFIER,
- version.getQualifier());
- }
-
- private String attributeValue(String str) {
- return extractValue(str, "=");
- }
-
- private String directiveValue(String str) {
- return extractValue(str, ":=");
- }
-
- private String extractValue(String str, String eq) {
- String[] tokens = str.split(eq);
- // String key = tokens[0];
- String value = tokens[1].trim();
- // TODO: optimize?
- if (value.startsWith("\""))
- value = value.substring(1);
- if (value.endsWith("\""))
- value = value.substring(0, value.length() - 1);
- return value;
- }
-
- protected Node createFileNode(Node parentNode, File file) {
- Binary binary = null;
- try {
- Node fileNode = parentNode
- .addNode(file.getName(), NodeType.NT_FILE);
- Node contentNode = fileNode.addNode(Node.JCR_CONTENT,
- NodeType.NT_RESOURCE);
- binary = jcrSession.getValueFactory().createBinary(
- new FileInputStream(file));
- contentNode.setProperty(Property.JCR_DATA, binary);
- // jar file
- if (FilenameUtils.isExtension(file.getName(), "jar")) {
- JarInputStream jarIn = null;
- ByteArrayOutputStream bo = null;
- ByteArrayInputStream bi = null;
- Binary manifestBinary = null;
- try {
- jarIn = new JarInputStream(binary.getStream());
- Manifest manifest = jarIn.getManifest();
- bo = new ByteArrayOutputStream();
- manifest.write(bo);
- bi = new ByteArrayInputStream(bo.toByteArray());
- manifestBinary = jcrSession.getValueFactory().createBinary(
- bi);
- fileNode.addMixin(SlcTypes.SLC_JAR_FILE);
- fileNode.setProperty(SlcNames.SLC_MANIFEST, manifestBinary);
- Attributes attrs = manifest.getMainAttributes();
- addAttr(Attributes.Name.MANIFEST_VERSION, fileNode, attrs);
- addAttr(Attributes.Name.SIGNATURE_VERSION, fileNode, attrs);
- addAttr(Attributes.Name.CLASS_PATH, fileNode, attrs);
- addAttr(Attributes.Name.MAIN_CLASS, fileNode, attrs);
- addAttr(Attributes.Name.EXTENSION_NAME, fileNode, attrs);
- addAttr(Attributes.Name.IMPLEMENTATION_VERSION, fileNode,
- attrs);
- addAttr(Attributes.Name.IMPLEMENTATION_VENDOR, fileNode,
- attrs);
- addAttr(Attributes.Name.IMPLEMENTATION_VENDOR_ID, fileNode,
- attrs);
- addAttr(Attributes.Name.SPECIFICATION_TITLE, fileNode,
- attrs);
- addAttr(Attributes.Name.SPECIFICATION_VERSION, fileNode,
- attrs);
- addAttr(Attributes.Name.SPECIFICATION_VENDOR, fileNode,
- attrs);
- addAttr(Attributes.Name.SEALED, fileNode, attrs);
- } finally {
- if (manifestBinary != null)
- manifestBinary.dispose();
- IOUtils.closeQuietly(bi);
- IOUtils.closeQuietly(bo);
- IOUtils.closeQuietly(jarIn);
- }
- }
- return fileNode;
- } catch (Exception e) {
- throw new SlcException("Cannot create file node based on " + file
- + " under " + parentNode, e);
- } finally {
- if (binary != null)
- binary.dispose();
- }